cairo-lang-semantic 2.18.0

Cairo semantic model.
Documentation
//! > Named arguments

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(d: bool) {
    // Valid names (one wrong type).
    bar(0, 1, 2, :d, e: 0);
    // Invalid name.
    // Note that a diagnostic is not reported for the wrong type when the name is wrong.
    bar(x: 0, 1, y: 2, 3, false);
    MyEnum::A(x: 0);
    // Wrong number of params.
    bar(0, 1, 2, 3);
    // Wrong number of params.
    bar(0, 1, 2, 3, 4, 5);
}

//! > function_name
foo

//! > module_code
fn bar(a: felt252, b: felt252, c: felt252, d: felt252, e: felt252) {}

enum MyEnum {
    A: felt252,
}

//! > expected_diagnostics
error[E2041]: Unexpected argument type. Expected: "core::felt252", found: "core::bool".
 --> lib.cairo:8:19
    bar(0, 1, 2, :d, e: 0);
                  ^

error[E2124]: Unexpected argument name. Expected: 'a', found 'x'.
 --> lib.cairo:11:9
    bar(x: 0, 1, y: 2, 3, false);
        ^

error[E2123]: Unnamed arguments cannot follow named arguments.
 --> lib.cairo:11:15
    bar(x: 0, 1, y: 2, 3, false);
              ^

error[E2124]: Unexpected argument name. Expected: 'c', found 'y'.
 --> lib.cairo:11:18
    bar(x: 0, 1, y: 2, 3, false);
                 ^

error[E2121]: Named arguments are not supported in this context.
 --> lib.cairo:12:15
    MyEnum::A(x: 0);
              ^

error[E2030]: Wrong number of arguments. Expected 5, found: 4
 --> lib.cairo:14:5
    bar(0, 1, 2, 3);
    ^^^^^^^^^^^^^^^

error[E2030]: Wrong number of arguments. Expected 5, found: 6
 --> lib.cairo:16:5
    bar(0, 1, 2, 3, 4, 5);
    ^^^^^^^^^^^^^^^^^^^^^

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

//! > Ref arguments

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(ref a: felt252) {
    let b = 1;
    let mut c = 2;
    bar(a, a);
    bar(b, b);
    bar(c, c);
    bar(ref a, ref a);
    bar(ref b, ref b);
    bar(ref c, ref c);
}

//! > function_name
foo

//! > module_code
fn bar(a: felt252, ref b: felt252) {}

//! > expected_diagnostics
error[E2081]: ref argument must be passed with a preceding 'ref'.
 --> lib.cairo:5:12
    bar(a, a);
           ^

error[E2080]: ref argument must be a mutable variable.
 --> lib.cairo:6:12
    bar(b, b);
           ^

error[E2081]: ref argument must be passed with a preceding 'ref'.
 --> lib.cairo:6:12
    bar(b, b);
           ^

error[E2081]: ref argument must be passed with a preceding 'ref'.
 --> lib.cairo:7:12
    bar(c, c);
           ^

error[E2082]: Argument to immutable parameter cannot have modifiers.
 --> lib.cairo:8:13
    bar(ref a, ref a);
            ^

error[E2082]: Argument to immutable parameter cannot have modifiers.
 --> lib.cairo:9:13
    bar(ref b, ref b);
            ^

error[E2080]: ref argument must be a mutable variable.
 --> lib.cairo:9:20
    bar(ref b, ref b);
                   ^

error[E2082]: Argument to immutable parameter cannot have modifiers.
 --> lib.cairo:10:13
    bar(ref c, ref c);
            ^

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

//! > Unknown function

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(d: MyStruct) {
    bar(0);
    d + 0;
    -d;
    d.bar();
    d.baz();
}

//! > function_name
foo

//! > module_code
struct MyStruct {}

struct OtherStruct {}

trait MyTrait<T> {
    fn baz(self: T);
}
impl OtherStructImpl of MyTrait<OtherStruct> {
    fn baz(self: OtherStruct) {}
}

//! > expected_diagnostics
error[E0006]: Function not found.
 --> lib.cairo:12:5
    bar(0);
    ^^^

error[E0002]: Method `bar` not found on type `test::MyStruct`. Did you import the correct trait and impl?
 --> lib.cairo:15:7
    d.bar();
      ^^^

error[E0002]: Method `baz` could not be called on type `test::MyStruct`.
Candidate `test::MyTrait::baz` inference failed with: Trait has no implementation in context: test::MyTrait::<test::MyStruct>.
 --> lib.cairo:16:7
    d.baz();
      ^^^

error[E2311]: Trait has no implementation in context: core::traits::Add::<test::MyStruct>.
 --> lib.cairo:13:5
    d + 0;
    ^^^^^

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

//! > Return type should be empty

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

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

//! > function_name
foo

//! > expected_diagnostics
error[E2042]: 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_code
fn foo() -> u8 {}

//! > function_name
foo

//! > expected_diagnostics
error[E2042]: 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_code
fn foo() -> () {}

//! > function_name
foo

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

//! > expected_diagnostics
error[E2042]: 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_code
fn foo() -> () {}

//! > function_name
foo

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

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

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

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

//! > Closure wrong return type

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

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

//! > function_name
foo

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

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

//! > Statement-level const used in named argument shorthand.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() -> felt252 {
    const x: felt252 = 42;
    takes_val(:x)
}

//! > function_name
foo

//! > module_code
fn takes_val(x: felt252) -> felt252 {
    x
}

//! > expected_diagnostics