cairo-lang-semantic 2.18.0

Cairo semantic model.
Documentation
//! > Incompatible types in loop breaks

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    loop {
        if true {
            break true;
        } else {
            break 0;
        }
    };
}

//! > function_name
foo

//! > expected_diagnostics
error[E2311]: Mismatched types. The type `core::bool` cannot be created from a numeric literal.
 --> lib.cairo:6:19
            break 0;
                  ^

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

//! > Loop break types incompatible, also with expected type.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(x: Option<()>) {
    loop {
        if true {
            break true;
        } else {
            break 0;
        }
    }
}

//! > function_name
foo

//! > expected_diagnostics
error[E2042]: Unexpected return type. Expected: "()", found: "core::bool".
 --> lib.cairo:1:23-9:1
  fn foo(x: Option<()>) {
 _______________________^
| ...
| }
|_^

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

//! > Continue outside a loop.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    continue;
}

//! > function_name
foo

//! > expected_diagnostics
error[E2145]: `continue` only allowed inside a `loop`.
 --> lib.cairo:2:5
    continue;
    ^^^^^^^^^

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

//! > Break outside a loop.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    break 5;
}

//! > function_name
foo

//! > expected_diagnostics
error[E2146]: `break` only allowed inside a `loop`.
 --> lib.cairo:2:5
    break 5;
    ^^^^^^^^

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

//! > Valid loop

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() -> bool {
    loop {
        break true;
    }
}

//! > function_name
foo

//! > expected_diagnostics

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

//! > Invalid return in regard to the return type

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> bool {
    return;
}

//! > function_name
foo

//! > expected_diagnostics
error[E2042]: Unexpected return type. Expected: "core::bool", found: "()".
 --> lib.cairo:2:11
    return;
          ^

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

//! > Valid empty return

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() {
    return;
}

//! > function_name
foo

//! > expected_diagnostics

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

//! > Empty return inside a loop

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> bool {
    loop {
        return;
    }
}

//! > function_name
foo

//! > expected_diagnostics
error[E2042]: Unexpected return type. Expected: "core::bool", found: "()".
 --> lib.cairo:3:15
        return;
              ^

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

//! > Invalid break in regard to the return type

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> bool {
    loop {
        break;
    }
}

//! > function_name
foo

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

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

//! > Valid empty break

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() {
    loop {
        break;
    }
}

//! > function_name
foo

//! > expected_diagnostics

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

//! > Empty break outside of a loop

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {
    break;
}

//! > function_name
foo

//! > expected_diagnostics
error[E2146]: `break` only allowed inside a `loop`.
 --> lib.cairo:2:5
    break;
    ^^^^^^

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

//! > Error propagation inside a loop.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() -> Option<bool> {
    loop {
        let res = Some(true);
        res?;
        break;
    }
    None
}

//! > function_name
foo

//! > module_code
use option::Option;

//! > expected_diagnostics

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

//! > Loop with a felt252 tail expression.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> Option<bool> {
    loop {
        1
    }
    None
}

//! > function_name
foo

//! > module_code
use option::Option;

//! > expected_diagnostics
error[E2144]: Tail expression not allowed in a `loop` block.
 --> lib.cairo:4:9
        1
        ^

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

//! > Loop with a unit tail expression.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() -> Option<bool> {
    loop {
        ()
    }
    None
}

//! > function_name
foo

//! > module_code
use option::Option;

//! > expected_diagnostics

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

//! > Loop with a never tail expression.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() -> Option<bool> {
    loop {
        panic_with_felt252('no return')
    }
    None
}

//! > function_name
foo

//! > module_code
use option::Option;

//! > expected_diagnostics

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

//! > Loop break with never type.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo(a: u32) -> Option<bool> {
    loop {
        if a == 0 {
            break panic_with_felt252('no return');
        }
        break;
    }
    None
}

//! > function_name
foo

//! > expected_diagnostics
error[E2056]: Loop has incompatible return types: "core::never" and "()"
 --> lib.cairo:6:14
        break;
             ^