cairo-lang-semantic 2.18.0

Cairo semantic model.
Documentation
//! > Test fixed size array.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() {
    let _x: [u32; 3] = [1; 3];
    let _y: [u32; 3] = [1, 2, 3];
    let _z = [1, 2, 3];
}

//! > function_name
foo

//! > expected_diagnostics

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

//! > Test fixed size array with expr size.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() {
    let _x: [u32; 1 + 2] = [1; 1 + 2];
    let _x: [u32; SIZE] = [1; 1 + 2];
    let _x: [u32; SIZE + 1] = [1; 5 - 1];
}

//! > function_name
foo

//! > module_code
const SIZE: usize = 3;

//! > expected_diagnostics

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

//! > Test fixed size array incompatible size.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let _x: [u32; 2] = [1, 2, 3];
    let _x: [u32; 2] = [1; 3];
}

//! > function_name
foo

//! > expected_diagnostics
error[E2041]: Unexpected argument type. Expected: "[core::integer::u32; 2]", found: "[?1; 3]".
 --> lib.cairo:2:24
    let _x: [u32; 2] = [1, 2, 3];
                       ^^^^^^^^^

error[E2041]: Unexpected argument type. Expected: "[core::integer::u32; 2]", found: "[?6; 3]".
 --> lib.cairo:3:24
    let _x: [u32; 2] = [1; 3];
                       ^^^^^^

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

//! > Test fixed size array illegal number of elements.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let _x: [u32, u32; 2] = [1, 2];
    let _x: [; 2] = [1, 2];
    let _x: [u32; 2] = [1, 2; 2];
    let _x: [u32; 2] = [];
    let _x: [u32; 2] = [; 2];
}

//! > function_name
foo

//! > expected_diagnostics
error[E2170]: Fixed size array type must have exactly one type.
 --> lib.cairo:2:13
    let _x: [u32, u32; 2] = [1, 2];
            ^^^^^^^^^^^^^

error[E2170]: Fixed size array type must have exactly one type.
 --> lib.cairo:3:13
    let _x: [; 2] = [1, 2];
            ^^^^^

error[E2173]: Fixed size array with defined size must have exactly one value.
 --> lib.cairo:4:24
    let _x: [u32; 2] = [1, 2; 2];
                       ^^^^^^^^^

error[E2041]: Unexpected argument type. Expected: "[core::integer::u32; 2]", found: "[?7; 0]".
 --> lib.cairo:5:24
    let _x: [u32; 2] = [];
                       ^^

error[E2173]: Fixed size array with defined size must have exactly one value.
 --> lib.cairo:6:24
    let _x: [u32; 2] = [; 2];
                       ^^^^^

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

//! > Test fixed size array illegal size.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let _x: [u32; -1] = [1; -1];
    let _x: [u32; SIZE] = [1; SIZE];
    let _x: [u32; '1'] = [1; '1'];
    let _x: [u32; 32768] = [1; 32768];
}

//! > function_name
foo

//! > module_code
const SIZE: usize = 2;

//! > expected_diagnostics
error[E2008]: The value does not fit within the range of type core::integer::u32.
 --> lib.cairo:3:19
    let _x: [u32; -1] = [1; -1];
                  ^^

error[E2172]: Fixed size array type must have a positive integer size.
 --> lib.cairo:3:13
    let _x: [u32; -1] = [1; -1];
            ^^^^^^^^^

error[E2008]: The value does not fit within the range of type core::integer::u32.
 --> lib.cairo:3:29
    let _x: [u32; -1] = [1; -1];
                            ^^

error[E2172]: Fixed size array type must have a positive integer size.
 --> lib.cairo:3:25
    let _x: [u32; -1] = [1; -1];
                        ^^^^^^^

error[E2174]: Fixed size array size must be smaller than 2^15.
 --> lib.cairo:6:28
    let _x: [u32; 32768] = [1; 32768];
                           ^^^^^^^^^^

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

//! > Test fixed size array mixed types.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let _x: [u32; 2] = [1, 2_u16];
    let _x = [1_u32, 2_u16];
    let _x: [u32; 2] = [true, false];
}

//! > function_name
foo

//! > module_code
const SIZE: usize = 2;

//! > expected_diagnostics
error[E2041]: Unexpected argument type. Expected: "[core::integer::u32; 2]", found: "[core::integer::u16; 2]".
 --> lib.cairo:3:24
    let _x: [u32; 2] = [1, 2_u16];
                       ^^^^^^^^^^

error[E2041]: Unexpected argument type. Expected: "core::integer::u32", found: "core::integer::u16".
 --> lib.cairo:4:22
    let _x = [1_u32, 2_u16];
                     ^^^^^

error[E2041]: Unexpected argument type. Expected: "[core::integer::u32; 2]", found: "[core::bool; 2]".
 --> lib.cairo:5:24
    let _x: [u32; 2] = [true, false];
                       ^^^^^^^^^^^^^

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

//! > Test fixed size array, generic short notation

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> [felt252; 1] {
    bar()
}

//! > function_name
foo

//! > module_code
fn bar<const N: usize>() -> [felt252; N] {
    [0; N]
}

//! > expected_diagnostics
error[E2172]: Fixed size array type must have a positive integer size.
 --> lib.cairo:2:5
    [0; N]
    ^^^^^^

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

//! > Test infer fixed size array size from pattern.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() {
    let [_a, _b] = bar();
}

//! > function_name
foo

//! > module_code
extern fn bar<const N: usize>() -> [felt252; N] nopanic;

//! > expected_diagnostics

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

//! > Test fixed size array, wrong pattern size.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    let [_a, _b] = bar::<3>();
}

//! > function_name
foo

//! > module_code
extern fn bar<const N: usize>() -> [felt252; N] nopanic;

//! > expected_diagnostics
error[E2108]: Wrong number of fixed size array elements in pattern. Expected: 3. Got: 2.
 --> lib.cairo:3:9
    let [_a, _b] = bar::<3>();
        ^^^^^^^^

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

//! > Test fixed size array, has core traits implementation.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() {}

//! > function_name
foo

//! > module_code
#[derive(Default, Clone, Debug, Drop, PartialEq, Serde)]
pub struct StructWithByteArray {
    pub features: [felt252; 2],
}

//! > expected_diagnostics

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

//! > Test fixed size array, support wildcard.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() {
    let _x: [_; 3] = [1_u8, 2_u8, 3_u8];
}

//! > function_name
foo

//! > expected_diagnostics

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

//! > Loop expression in fixed-size array size position.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {}

//! > function_name
foo

//! > module_code
fn f() -> [felt252; for _ in 0..1_u32 {}] {
    [0]
}

//! > expected_diagnostics
error[E2302]: Type mismatch: `()` and `core::integer::u32`.
 --> lib.cairo:1:21
fn f() -> [felt252; for _ in 0..1_u32 {}] {
                    ^^^^^^^^^^^^^^^^^^^^

error[E2172]: Fixed size array type must have a positive integer size.
 --> lib.cairo:1:11
fn f() -> [felt252; for _ in 0..1_u32 {}] {
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^