cairo-lang-semantic 2.18.0

Cairo semantic model.
Documentation
//! > Test unexpected argument types for operators.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(a: MyType) {
    a + a * a;
}

//! > function_name
foo

//! > module_code
extern type MyType;

//! > expected_diagnostics
error[E2311]: Trait has no implementation in context: core::traits::Add::<test::MyType>.
 --> lib.cairo:3:5
    a + a * a;
    ^^^^^^^^^

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

//! > Test operators.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(a: u128, b: bool) implicits(RangeCheck) {
    // Valid.
    a + a * a - a;
    (b & b) | (b & ((a == a) | (a > a))) & (a < a) & (a <= a) & (a >= a);
    // Errors.
    a > a > a;
    a - b
}

//! > function_name
foo

//! > expected_diagnostics
error[E1028]: Consecutive comparison operators are not allowed: '>' followed by '>'
 --> lib.cairo:6:11
    a > a > a;
          ^

error[E2041]: Unexpected argument type. Expected: "core::bool", found: "core::integer::u128".
 --> lib.cairo:6:13
    a > a > a;
            ^

error[E2041]: Unexpected argument type. Expected: "core::integer::u128", found: "core::bool".
 --> lib.cairo:7:9
    a - b
        ^

error[E2042]: Unexpected return type. Expected: "()", found: "core::integer::u128".
 --> lib.cairo:1:48-8:1
  fn foo(a: u128, b: bool) implicits(RangeCheck) {
 ________________________________________________^
| ...
| }
|_^

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

//! > Test unary operators.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    -(1 == 2);
    -1_u8;
    !17;
}

//! > function_name
foo

//! > expected_diagnostics
error[E2008]: The value does not fit within the range of type core::integer::u8.
 --> lib.cairo:3:5
    -1_u8;
    ^^^^^

error[E2311]: Trait has no implementation in context: core::traits::Neg::<core::bool>.
 --> lib.cairo:2:5
    -(1 == 2);
    ^^^^^^^^^

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

//! > Test index operator

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let x1 = Struct1 { x: 0 };
    let _y1 = x1[0];
    let x2 = Struct2 { x: 0 };
    let _y2 = x2[0];
}

//! > function_name
foo

//! > module_code
struct Struct1 {
    x: felt252,
}
struct Struct2 {
    x: felt252,
}


impl Struct2IndexView of IndexView<Struct2, usize, @felt252> {
    fn index(self: @Struct2, index: usize) -> @felt252 {
        self.x
    }
}
impl Struct2Index of Index<Struct2, usize, felt252> {
    fn index(ref self: Struct2, index: usize) -> felt252 {
        self.x
    }
}

//! > expected_diagnostics
error[E2136]: Type `test::Struct1` could not be indexed.
Candidate `core::ops::index::Index::index` inference failed with: Trait has no implementation in context: core::ops::index::Index::<test::Struct1, ?3>.
Candidate `core::ops::index::IndexView::index` inference failed with: Trait has no implementation in context: core::ops::index::IndexView::<test::Struct1, ?3>.
 --> lib.cairo:21:15
    let _y1 = x1[0];
              ^^^^^

error[E2139]: Type "test::Struct2" implements both the "Index" trait and the "IndexView" trait.
 --> lib.cairo:23:15
    let _y2 = x2[0];
              ^^^^^