cairo-lang-semantic 2.18.0

Cairo semantic model.
Documentation
//! > Test basic error propagation on Option.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() -> Option<felt252> {
    with_err()?;
    Option::<felt252>::None
}

//! > function_name
foo

//! > module_code
fn with_err() -> Option<felt252> {
    Option::<felt252>::None
}

//! > expected_diagnostics

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

//! > Test bad function for error propagation on Option.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> felt252 {
    with_err()?;
    1
}

//! > function_name
foo

//! > module_code
fn with_err() -> Option<felt252> {
    Option::<felt252>::None
}

//! > expected_diagnostics
error[E2061]: `?` can only be used in a function with `Option` or `Result` return type.
 --> lib.cairo:5:5
    with_err()?;
    ^^^^^^^^^^^

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

//! > Test basic error propagation on Result.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)

//! > function_code
fn foo() -> Result<felt252, u128> {
    with_err()?;
    Result::<felt252, u128>::Ok(with_err2()?)
}

//! > function_name
foo

//! > module_code
fn with_err() -> Result<(), u128> {
    Result::<(), u128>::Ok(())
}

fn with_err2() -> Result<felt252, u128> {
    Result::<felt252, u128>::Ok((0))
}

//! > expected_diagnostics

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

//! > Test bad function for error propagation on Result.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> felt252 {
    with_err()?;
    1
}

//! > function_name
foo

//! > module_code
fn with_err() -> Result<felt252, u128> {
    Result::<felt252, u128>::Ok((0))
}

//! > expected_diagnostics
error[E2061]: `?` can only be used in a function with `Option` or `Result` return type.
 --> lib.cairo:5:5
    with_err()?;
    ^^^^^^^^^^^

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

//! > Test error propagation on mismatching Result types.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> Result<felt252, u128> {
    with_other_err()?;
    Result::<felt252, felt252>::Ok((0))
}

//! > function_name
foo

//! > module_code
fn with_other_err() -> Result<felt252, felt252> {
    Result::<felt252, felt252>::Ok((0))
}

//! > expected_diagnostics
error[E2062]: Return type "core::result::Result::<core::felt252, core::integer::u128>" does not wrap error "core::felt252"
 --> lib.cairo:5:5
    with_other_err()?;
    ^^^^^^^^^^^^^^^^^

error[E2042]: Unexpected return type. Expected: "core::result::Result::<core::felt252, core::integer::u128>", found: "core::result::Result::<core::felt252, core::felt252>".
 --> lib.cairo:4:13
fn foo() -> Result<felt252, u128> {
            ^^^^^^^^^^^^^^^^^^^^^

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

//! > Test bad operand for error propagation.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() -> Option<felt252> {
    6_u32?;
    None
}

//! > function_name
foo

//! > expected_diagnostics
error[E2063]: Type "core::integer::u32" cannot error propagate
 --> lib.cairo:2:5
    6_u32?;
    ^^^^^^