Skip to content

Commit

Permalink
Return Error Ptr (#6792)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomer-StarkWare authored Dec 3, 2024
1 parent 299469c commit 3564ece
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 70 deletions.
4 changes: 2 additions & 2 deletions crates/cairo-lang-lowering/src/test_data/tests
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,9 @@ foo

//! > semantic_diagnostics
error: Unexpected return type. Expected: "core::integer::u128", found: "core::option::Option::<core::integer::u128>".
--> lib.cairo:1:43
--> lib.cairo:1:38
fn foo(mut data: Span::<felt252>) -> u128 {
^
^**^

//! > lowering_diagnostics

Expand Down
12 changes: 11 additions & 1 deletion crates/cairo-lang-semantic/src/expr/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,17 @@ pub fn compute_root_expr(
if let Err((err_set, actual_ty, expected_ty)) =
inference.conform_ty_for_diag(res_ty, return_type)
{
let diag_added = ctx.diagnostics.report(syntax, WrongReturnType { expected_ty, actual_ty });
let diag_added = ctx.diagnostics.report(
ctx.signature
.map(|s| match s.stable_ptr.lookup(ctx.db.upcast()).ret_ty(ctx.db.upcast()) {
OptionReturnTypeClause::Empty(_) => syntax.stable_ptr().untyped(),
OptionReturnTypeClause::ReturnTypeClause(return_type_clause) => {
return_type_clause.ty(ctx.db.upcast()).stable_ptr().untyped()
}
})
.unwrap_or_else(|| syntax.stable_ptr().untyped()),
WrongReturnType { expected_ty, actual_ty },
);
inference.consume_reported_error(err_set, diag_added);
}

Expand Down
12 changes: 6 additions & 6 deletions crates/cairo-lang-semantic/src/expr/test_data/coupon
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ foo

//! > expected_diagnostics
error: Unexpected return type. Expected: "test::bar::<S, T>::Coupon", found: "()".
--> lib.cairo:8:46
--> lib.cairo:8:26
fn get_coupon<S, T>() -> bar::<S, T>::Coupon {}
^^
^*****************^

error: Unexpected return type. Expected: "test::bar2::<S, T>::Coupon", found: "()".
--> lib.cairo:9:48
--> lib.cairo:9:27
fn get_coupon2<S, T>() -> bar2::<S, T>::Coupon {}
^^
^******************^

error: Unexpected return type. Expected: "test::MyStruct::<S>", found: "()".
--> lib.cairo:10:35
--> lib.cairo:10:23
fn get_struct<S>() -> MyStruct<S> {}
^^
^*********^

error: Type "test::bar::<core::integer::u8, core::integer::u16>::Coupon" has no members.
--> lib.cairo:15:7
Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/expr/test_data/error_propagate
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ error: Return type "core::result::Result::<core::felt252, core::integer::u128>"
^***************^

error: Unexpected return type. Expected: "core::result::Result::<core::felt252, core::integer::u128>", found: "core::result::Result::<core::felt252, core::felt252>".
--> lib.cairo:4:37
--> lib.cairo:4:13
fn foo() -> Result::<felt252, u128> {
^
^*********************^

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

Expand Down
100 changes: 100 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 @@ -181,3 +181,103 @@ error: Trait has no implementation in context: core::traits::Add::<test::MyStruc
--> lib.cairo:13:5
d + 0;
^***^

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

//! > Return type should be empty

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function
fn foo() -> () {
4_u8
}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics
error: Unexpected return type. Expected: "()", found: "core::integer::u8".
--> lib.cairo:1:13
fn foo() -> () {
^^

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

//! > Return type should not be empty

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function
fn foo() -> u8 {}

//! > function_name
foo

//! > module_code

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::u8", found: "()".
--> lib.cairo:1:13
fn foo() -> u8 {}
^^

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

//! > Trait return wrong return type

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function
fn foo() -> () {}

//! > function_name
foo

//! > module_code
trait MyTrait {
fn bar() -> u8 {
1_u16
}
}

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::u8", found: "core::integer::u16".
--> lib.cairo:2:17
fn bar() -> u8 {
^^

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

//! > Impl return wrong return type

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function
fn foo() -> () {}

//! > function_name
foo

//! > module_code
trait MyTrait {
fn bar() -> u8;
}

impl MyImpl of MyTrait {
fn bar() -> u8 {
1_u16
}
}

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::u8", found: "core::integer::u16".
--> lib.cairo:6:17
fn bar() -> u8 {
^^
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/expr/test_data/generics
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,9 @@ struct S<const N: felt252> {

//! > expected_diagnostics
error: Unexpected return type. Expected: "test::S::<1>", found: "test::S::<N>".
--> lib.cairo:1:44
--> lib.cairo:1:31
fn bar<const N: felt252>() -> S<{ 3 - 2 }> {
^
^**********^

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

Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/expr/test_data/inference
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@ foo

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::never", found: "core::felt252".
--> lib.cairo:1:19
--> lib.cairo:1:13
fn foo() -> never {
^
^***^

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

Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/expr/test_data/loop
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ foo

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::bool", found: "()".
--> lib.cairo:1:18
--> lib.cairo:1:13
fn foo() -> bool {
^
^**^

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

Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/expr/test_data/return
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ foo

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::felt252", found: "()".
--> lib.cairo:1:21
--> lib.cairo:1:13
fn foo() -> felt252 {}
^^
^*****^
8 changes: 4 additions & 4 deletions crates/cairo-lang-semantic/src/items/tests/trait
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ error: The signature of function `param_test` is incompatible with trait `MyTrai
^******************************************^

error: Unexpected return type. Expected: "core::integer::u128", found: "()".
--> lib.cairo:26:63
--> lib.cairo:26:58
fn param_test(a: felt252, b: felt252, c: felt252) -> u128 {
^
^**^

error: Parameter of impl function MyImpl2::no_ret_ty is incompatible with MyTrait::no_ret_ty. It should not be a reference.
--> lib.cairo:30:18
Expand Down Expand Up @@ -996,9 +996,9 @@ impl MyImpl of MyTrait {

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::u32", found: "core::integer::u16".
--> lib.cairo:6:22
--> lib.cairo:6:18
fn foo1() -> u32 {
^
^*^

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

Expand Down
4 changes: 2 additions & 2 deletions crates/cairo-lang-semantic/src/items/tests/trait_const
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,9 @@ impl MyImpl of MyTrait {

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::u16", found: "core::integer::u32".
--> lib.cairo:11:21
--> lib.cairo:11:17
fn foo() -> u16 {
^
^*^

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

Expand Down
16 changes: 8 additions & 8 deletions crates/cairo-lang-semantic/src/items/tests/trait_impl
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,9 @@ error: Return type of impl function `MyImpl::foo1` is incompatible with `MyTrait
^***************************^

error: Unexpected return type. Expected: "core::integer::u16", found: "core::integer::u32".
--> lib.cairo:18:48
--> lib.cairo:18:18
fn foo1() -> AnotherTrait::Impl::InputType {
^
^***************************^

error: Parameter type of impl function `MyImpl::foo2` is incompatible with `MyTrait::foo2`. Expected: `core::integer::u32`, actual: `core::integer::u16`.
--> lib.cairo:21:16
Expand Down Expand Up @@ -516,9 +516,9 @@ impl MyImpl of MyTrait {

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::u16", found: "core::integer::u32".
--> lib.cairo:14:40
--> lib.cairo:14:18
fn foo1() -> Self::Impl::InputType {
^
^*******************^

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

Expand Down Expand Up @@ -646,9 +646,9 @@ fn complex(

//! > expected_diagnostics
error: Unexpected return type. Expected: "@(core::integer::u32, core::option::Option::<@core::result::Result::<[core::option::Option::<core::integer::u32>; 3], (@core::integer::u32, core::integer::u32)>>)", found: "@(core::integer::u16, core::option::Option::<@core::result::Result::<[core::option::Option::<core::integer::u16>; 3], (@core::integer::u16, core::integer::u16)>>)".
--> lib.cairo:44:61
--> lib.cairo:44:6
) -> @(u32, Option<@Result<[Option<u32>; 3], (@u32, u32)>>) {
^
^****************************************************^

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

Expand Down Expand Up @@ -720,9 +720,9 @@ impl MyImpl of MyTrait {

//! > expected_diagnostics
error: Unexpected return type. Expected: "core::integer::u32", found: "core::integer::u16".
--> lib.cairo:15:46
--> lib.cairo:15:42
fn bar(x: Self::Impl1::InputType) -> u32 {
^
^*^

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

Expand Down
Loading

0 comments on commit 3564ece

Please sign in to comment.