Skip to content

Commit

Permalink
Error ptr for closure return type. (#6795)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomer-StarkWare authored Dec 3, 2024
1 parent 3564ece commit e669a7b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
14 changes: 9 additions & 5 deletions crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1712,11 +1712,15 @@ fn compute_expr_closure_semantic(
if let Err((err_set, actual_ty, expected_ty)) =
inference.conform_ty_for_diag(new_ctx.arenas.exprs[body].ty(), return_type)
{
let diag_added =
new_ctx.diagnostics.report(syntax.expr(syntax_db).stable_ptr(), WrongReturnType {
expected_ty,
actual_ty,
});
let diag_added = new_ctx.diagnostics.report(
match syntax.ret_ty(ctx.db.upcast()).stable_ptr().lookup(ctx.db.upcast()) {
OptionReturnTypeClause::Empty(_) => syntax.expr(syntax_db).stable_ptr(),
OptionReturnTypeClause::ReturnTypeClause(return_type_clause) => {
return_type_clause.ty(ctx.db.upcast()).stable_ptr()
}
},
WrongReturnType { expected_ty, actual_ty },
);
inference.consume_reported_error(err_set, diag_added);
}
(params, return_type, body)
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/expr/test_data/closure
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ foo

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::i32", found: "core::integer::u32".
--> lib.cairo:2:15
--> lib.cairo:2:11
|| -> i32 {
^
^*^

//! > ==========================================================================

Expand Down
25 changes: 25 additions & 0 deletions crates/cairo-lang-semantic/src/expr/test_data/function_call
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,28 @@ error: Unexpected return type. Expected: "core::integer::u8", found: "core::inte
--> lib.cairo:6:17
fn bar() -> u8 {
^^

//! > ==========================================================================

//! > Closure wrong return type

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function
fn foo() -> () {
|| -> u8 {
1_u16
};
}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::u8", found: "core::integer::u16".
--> lib.cairo:2:11
|| -> u8 {
^^

0 comments on commit e669a7b

Please sign in to comment.