diff --git a/crates/cairo-lang-lowering/src/test_data/tests b/crates/cairo-lang-lowering/src/test_data/tests index 7dbd528a7c2..4e4b68779a1 100644 --- a/crates/cairo-lang-lowering/src/test_data/tests +++ b/crates/cairo-lang-lowering/src/test_data/tests @@ -375,9 +375,9 @@ foo //! > semantic_diagnostics error: Unexpected return type. Expected: "core::integer::u128", found: "core::option::Option::". - --> lib.cairo:1:43 + --> lib.cairo:1:38 fn foo(mut data: Span::) -> u128 { - ^ + ^**^ //! > lowering_diagnostics diff --git a/crates/cairo-lang-semantic/src/expr/compute.rs b/crates/cairo-lang-semantic/src/expr/compute.rs index 33661995363..a6554de8048 100644 --- a/crates/cairo-lang-semantic/src/expr/compute.rs +++ b/crates/cairo-lang-semantic/src/expr/compute.rs @@ -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); } diff --git a/crates/cairo-lang-semantic/src/expr/test_data/coupon b/crates/cairo-lang-semantic/src/expr/test_data/coupon index 7b2cc1fff83..a4250ced687 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/coupon +++ b/crates/cairo-lang-semantic/src/expr/test_data/coupon @@ -91,19 +91,19 @@ foo //! > expected_diagnostics error: Unexpected return type. Expected: "test::bar::::Coupon", found: "()". - --> lib.cairo:8:46 + --> lib.cairo:8:26 fn get_coupon() -> bar::::Coupon {} - ^^ + ^*****************^ error: Unexpected return type. Expected: "test::bar2::::Coupon", found: "()". - --> lib.cairo:9:48 + --> lib.cairo:9:27 fn get_coupon2() -> bar2::::Coupon {} - ^^ + ^******************^ error: Unexpected return type. Expected: "test::MyStruct::", found: "()". - --> lib.cairo:10:35 + --> lib.cairo:10:23 fn get_struct() -> MyStruct {} - ^^ + ^*********^ error: Type "test::bar::::Coupon" has no members. --> lib.cairo:15:7 diff --git a/crates/cairo-lang-semantic/src/expr/test_data/error_propagate b/crates/cairo-lang-semantic/src/expr/test_data/error_propagate index 47d01657d26..f2b530df21c 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/error_propagate +++ b/crates/cairo-lang-semantic/src/expr/test_data/error_propagate @@ -128,9 +128,9 @@ error: Return type "core::result::Result::" ^***************^ error: Unexpected return type. Expected: "core::result::Result::", found: "core::result::Result::". - --> lib.cairo:4:37 + --> lib.cairo:4:13 fn foo() -> Result:: { - ^ + ^*********************^ //! > ========================================================================== diff --git a/crates/cairo-lang-semantic/src/expr/test_data/function_call b/crates/cairo-lang-semantic/src/expr/test_data/function_call index 0433c349d78..dc421e2a539 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/function_call +++ b/crates/cairo-lang-semantic/src/expr/test_data/function_call @@ -181,3 +181,103 @@ error: Trait has no implementation in context: core::traits::Add:: 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 { + ^^ diff --git a/crates/cairo-lang-semantic/src/expr/test_data/generics b/crates/cairo-lang-semantic/src/expr/test_data/generics index 9a2628855f7..3418ce0c1af 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/generics +++ b/crates/cairo-lang-semantic/src/expr/test_data/generics @@ -671,9 +671,9 @@ struct S { //! > expected_diagnostics error: Unexpected return type. Expected: "test::S::<1>", found: "test::S::". - --> lib.cairo:1:44 + --> lib.cairo:1:31 fn bar() -> S<{ 3 - 2 }> { - ^ + ^**********^ //! > ========================================================================== diff --git a/crates/cairo-lang-semantic/src/expr/test_data/inference b/crates/cairo-lang-semantic/src/expr/test_data/inference index b27390700c1..04aac0f23d8 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/inference +++ b/crates/cairo-lang-semantic/src/expr/test_data/inference @@ -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 { - ^ + ^***^ //! > ========================================================================== diff --git a/crates/cairo-lang-semantic/src/expr/test_data/loop b/crates/cairo-lang-semantic/src/expr/test_data/loop index 776878214fb..90e45e5e43b 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/loop +++ b/crates/cairo-lang-semantic/src/expr/test_data/loop @@ -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 { - ^ + ^**^ //! > ========================================================================== diff --git a/crates/cairo-lang-semantic/src/expr/test_data/return b/crates/cairo-lang-semantic/src/expr/test_data/return index 3aa07fc0c9a..c64f2213b8a 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/return +++ b/crates/cairo-lang-semantic/src/expr/test_data/return @@ -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 {} - ^^ + ^*****^ diff --git a/crates/cairo-lang-semantic/src/items/tests/trait b/crates/cairo-lang-semantic/src/items/tests/trait index dbd1d9518a4..7e1ea10be41 100644 --- a/crates/cairo-lang-semantic/src/items/tests/trait +++ b/crates/cairo-lang-semantic/src/items/tests/trait @@ -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 @@ -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 { - ^ + ^*^ //! > ========================================================================== diff --git a/crates/cairo-lang-semantic/src/items/tests/trait_const b/crates/cairo-lang-semantic/src/items/tests/trait_const index 23132ab5869..c91559db0fb 100644 --- a/crates/cairo-lang-semantic/src/items/tests/trait_const +++ b/crates/cairo-lang-semantic/src/items/tests/trait_const @@ -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 { - ^ + ^*^ //! > ========================================================================== diff --git a/crates/cairo-lang-semantic/src/items/tests/trait_impl b/crates/cairo-lang-semantic/src/items/tests/trait_impl index f1d7429feda..ede22057dac 100644 --- a/crates/cairo-lang-semantic/src/items/tests/trait_impl +++ b/crates/cairo-lang-semantic/src/items/tests/trait_impl @@ -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 @@ -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 { - ^ + ^*******************^ //! > ========================================================================== @@ -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::; 3], (@core::integer::u32, core::integer::u32)>>)", found: "@(core::integer::u16, core::option::Option::<@core::result::Result::<[core::option::Option::; 3], (@core::integer::u16, core::integer::u16)>>)". - --> lib.cairo:44:61 + --> lib.cairo:44:6 ) -> @(u32, Option<@Result<[Option; 3], (@u32, u32)>>) { - ^ + ^****************************************************^ //! > ========================================================================== @@ -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 { - ^ + ^*^ //! > ========================================================================== diff --git a/crates/cairo-lang-semantic/src/items/tests/trait_type b/crates/cairo-lang-semantic/src/items/tests/trait_type index 190a73aa4cc..6662f94bc0f 100644 --- a/crates/cairo-lang-semantic/src/items/tests/trait_type +++ b/crates/cairo-lang-semantic/src/items/tests/trait_type @@ -1140,9 +1140,9 @@ error: Parameter type of impl function `MyImpl::foo2` is incompatible with `MyTr ^***********************^ error: Unexpected return type. Expected: "core::integer::u16", found: "core::integer::u32". - --> lib.cairo:19:30 + --> lib.cairo:19:14 fn bar1() -> MyTrait::MyType { - ^ + ^*************^ error: Unexpected argument type. Expected: "core::integer::u32", found: "core::integer::u16". --> lib.cairo:23:18 @@ -1385,9 +1385,9 @@ impl MyImpl of MyTrait { //! > expected_diagnostics error: Unexpected return type. Expected: "core::integer::u32", found: "core::integer::u16". - --> lib.cairo:8:31 + --> lib.cairo:8:18 fn foo1() -> Self::MyType { - ^ + ^**********^ //! > ========================================================================== @@ -1520,9 +1520,9 @@ error: Unexpected return type. Expected: "core::integer::u32", found: "core::int ^ error: Unexpected return type. Expected: "core::integer::u32", found: "core::integer::u16". - --> lib.cairo:12:54 + --> lib.cairo:12:37 fn ret_expr(x: MyTrait::MyType1) -> MyTrait::MyType2 { - ^ + ^**************^ error: Type mismatch: `core::integer::u32` and `core::integer::u16`. --> lib.cairo:16:21 @@ -1545,9 +1545,9 @@ error: If blocks have incompatible types: "core::integer::u16" and "core::intege ^*******^ error: Unexpected return type. Expected: "core::option::Option::", found: "core::option::Option::". - --> lib.cairo:44:79 + --> lib.cairo:44:54 fn error_propagation(x: Option) -> Option { - ^ + ^**********************^ //! > ========================================================================== @@ -1669,39 +1669,39 @@ fn complex2( //! > expected_diagnostics error: Unexpected return type. Expected: "core::option::Option::", found: "core::option::Option::". - --> lib.cairo:13:74 + --> lib.cairo:13:49 fn generic_args(x: Option) -> Option { - ^ + ^**********************^ error: Unexpected return type. Expected: "(core::integer::u32, core::integer::u16)", found: "(core::integer::u16, core::integer::u32)". - --> lib.cairo:16:92 + --> lib.cairo:16:55 fn tuple(x: MyTrait::MyType1, y: MyTrait::MyType2) -> (MyTrait::MyType2, MyTrait::MyType1) { - ^ + ^**********************************^ error: Unexpected return type. Expected: "[core::integer::u32; 3]", found: "[core::integer::u16; 3]". - --> lib.cairo:19:71 + --> lib.cairo:19:49 fn fix_sized_array(x: [MyTrait::MyType1; 3]) -> [MyTrait::MyType2; 3] { - ^ + ^*******************^ error: Unexpected return type. Expected: "@@core::integer::u32", found: "@@core::integer::u16". - --> lib.cairo:22:58 + --> lib.cairo:22:39 fn snapshot(x: @@MyTrait::MyType1) -> @@MyTrait::MyType2 { - ^ + ^****************^ error: Unexpected return type. Expected: "@@core::integer::u32", found: "@@core::integer::u16". - --> lib.cairo:25:58 + --> lib.cairo:25:39 fn snapshot2(x: @MyTrait::MyType1) -> @@MyTrait::MyType2 { - ^ + ^****************^ error: Unexpected return type. Expected: "@(core::integer::u32, core::option::Option::<@core::result::Result::<[core::option::Option::; 3], (@core::integer::u32, core::integer::u32)>>)", found: "@(core::integer::u16, core::option::Option::<@core::result::Result::<[core::option::Option::; 3], (@core::integer::u32, core::integer::u32)>>)". - --> lib.cairo:33:61 + --> lib.cairo:33:6 ) -> @(u32, Option<@Result<[Option; 3], (@u32, u32)>>) { - ^ + ^****************************************************^ error: Unexpected return type. Expected: "@(core::integer::u16, core::option::Option::<@core::result::Result::<[core::option::Option::; 3], (@core::integer::u32, core::integer::u32)>>)", found: "@(core::integer::u32, core::option::Option::<@core::result::Result::<[core::option::Option::; 3], (@core::integer::u32, core::integer::u32)>>)". - --> lib.cairo:41:3 -) { - ^ + --> lib.cairo:38:6 +) -> @( + ^^ //! > ========================================================================== @@ -1799,9 +1799,9 @@ impl MyImpl of MyTrait { //! > expected_diagnostics error: Unexpected return type. Expected: "core::integer::u32", found: "core::integer::u16". - --> lib.cairo:9:37 + --> lib.cairo:9:33 fn bar(x: Self::MyType1) -> u32 { - ^ + ^*^ //! > ========================================================================== @@ -2339,9 +2339,9 @@ trait MyTrait { //! > expected_diagnostics error: Unexpected return type. Expected: "core::felt252", found: "core::integer::u32". - --> lib.cairo:9:15 + --> lib.cairo:9:8 >() -> I2::ty { - ^ + ^****^ //! > ========================================================================== @@ -2690,9 +2690,9 @@ trait MyTrait { //! > expected_diagnostics error: Unexpected return type. Expected: "core::integer::u32", found: "core::integer::u8". - --> lib.cairo:4:77 + --> lib.cairo:4:70 fn bar() -> I2::ty { - ^ + ^****^ //! > ========================================================================== @@ -2831,9 +2831,9 @@ trait MyTrait { //! > expected_diagnostics error: Unexpected return type. Expected: "J1::ty", found: "core::felt252". - --> lib.cairo:8:39 + --> lib.cairo:8:32 fn bar2() -> J1::ty { - ^ + ^****^ //! > ========================================================================== @@ -2890,9 +2890,9 @@ trait MyTrait { //! > expected_diagnostics error: Unexpected return type. Expected: "core::integer::u8", found: "core::integer::u32". - --> lib.cairo:8:47 + --> lib.cairo:8:40 fn bar2() -> I1::ty { - ^ + ^****^ //! > ========================================================================== diff --git a/crates/cairo-lang-starknet/src/plugin/plugin_test_data/contracts/diagnostics b/crates/cairo-lang-starknet/src/plugin/plugin_test_data/contracts/diagnostics index ff40443f3ba..3f7a2a0e68d 100644 --- a/crates/cairo-lang-starknet/src/plugin/plugin_test_data/contracts/diagnostics +++ b/crates/cairo-lang-starknet/src/plugin/plugin_test_data/contracts/diagnostics @@ -1090,9 +1090,9 @@ impl StorageStorageBaseMutCopy of core::traits::Copy::; //! > expected_diagnostics error: Unexpected return type. Expected: "(core::felt252, core::felt252)", found: "()". - --> lib.cairo:6:59 + --> lib.cairo:6:40 fn foo(ref self: ContractState) -> (felt252, felt252) {} - ^^ + ^****************^ //! > ========================================================================== @@ -1530,9 +1530,9 @@ impl StorageStorageBaseMutCopy of core::traits::Copy::; //! > expected_diagnostics error: Unexpected return type. Expected: "(core::felt252, core::felt252)", found: "()". - --> lib.cairo:8:29 + --> lib.cairo:8:10 ) -> (felt252, felt252) {} - ^^ + ^****************^ //! > ==========================================================================