//! > Test match
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(b: A) -> felt252 {
match (b, 4) {
(A::a(_x), _) => { 1 },
(A::b(x), 1) => { x },
(7, 1) => { x },
(A::b(x), 1, _) => { x },
}
let x = (5, true);
let (y, _) = x;
y
}
//! > function_name
foo
//! > module_code
enum A {
a: (),
b: felt252,
}
//! > expected_diagnostics
error[E0006]: Identifier not found.
--> lib.cairo:9:21
(7, 1) => { x },
^
error[E2107]: Wrong number of tuple elements in pattern. Expected: 2. Got: 3.
--> lib.cairo:10:9
(A::b(x), 1, _) => { x },
^^^^^^^^^^^^^^^
error[E0006]: Identifier not found.
--> lib.cairo:10:30
(A::b(x), 1, _) => { x },
^
error[E2311]: Mismatched types. The type `test::A` cannot be created from a numeric literal.
--> lib.cairo:9:10
(7, 1) => { x },
^
//! > ==========================================================================
//! > Match with missing type
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(a: bool) -> felt252 {
match a + 1 {
A::a(_) => 0,
A::b(_) => 1,
}
}
//! > function_name
foo
//! > module_code
enum A {
a: (),
b: felt252,
}
//! > expected_diagnostics
error[E2109]: Wrong enum in pattern. Expected: "bool". Got: "A".
--> lib.cairo:7:9
A::a(_) => 0,
^^^^^^^
error[E2109]: Wrong enum in pattern. Expected: "bool". Got: "A".
--> lib.cairo:8:9
A::b(_) => 1,
^^^^^^^
error[E2311]: Trait has no implementation in context: core::traits::Add::<core::bool>.
--> lib.cairo:6:11
match a + 1 {
^^^^^
//! > ==========================================================================
//! > Match incompatible arm types
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(a: felt252, b: bool) -> felt252 {
match a {
0 => 0_felt252,
_ => b,
}
}
//! > function_name
foo
//! > module_code
enum A {
a: (),
b: felt252,
}
//! > expected_diagnostics
error[E2056]: Match arms have incompatible types: "core::felt252" and "core::bool"
--> lib.cairo:8:14
_ => b,
^
//! > ==========================================================================
//! > Match incompatible literal bool patterns
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(a: A) -> felt252 {
match a {
true => 2,
false => 3,
}
}
//! > function_name
foo
//! > module_code
enum A {
a: (),
b: felt252,
}
//! > expected_diagnostics
error[E2109]: Wrong enum in pattern. Expected: "A". Got: "bool".
--> lib.cairo:7:9
true => 2,
^^^^
error[E2109]: Wrong enum in pattern. Expected: "A". Got: "bool".
--> lib.cairo:8:9
false => 3,
^^^^^
//! > ==========================================================================
//! > Match arm types incompatible, also with expected type.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(x: Option<()>) {
match x {
Some(_) => true,
None => 0,
}
}
//! > function_name
foo
//! > expected_diagnostics
error[E2042]: Unexpected return type. Expected: "()", found: "core::bool".
--> lib.cairo:1:23-6:1
fn foo(x: Option<()>) {
_______________________^
| ...
| }
|_^
//! > ==========================================================================
//! > Test match with or pattern inconsistent mutability
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(a: MyEnum) -> felt252 {
match a {
MyEnum::A(mut x) | MyEnum::B((x, _)) => x,
MyEnum::C((x, _, t)) | MyEnum::D(P { x, z: mut t, .. }) => x + t,
}
}
//! > function_name
foo
//! > module_code
struct P {
x: felt252,
y: felt252,
z: felt252,
w: felt252,
}
enum MyEnum {
A: felt252,
B: (felt252, felt252),
C: (felt252, felt252, felt252),
D: P,
}
//! > expected_diagnostics
error[E2040]: variable is bound inconsistently across alternatives separated by `|` bound in different ways
--> lib.cairo:15:39
MyEnum::A(mut x) | MyEnum::B((x, _)) => x,
^
error[E2040]: variable is bound inconsistently across alternatives separated by `|` bound in different ways
--> lib.cairo:16:52
MyEnum::C((x, _, t)) | MyEnum::D(P { x, z: mut t, .. }) => x + t,
^^^^^
//! > ==========================================================================
//! > Test conform in or pattern
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo() {
let a = MyEnum::A(5);
match a {
MyEnum::A(x) | MyEnum::B((x, _)) => x,
}
bar(a);
}
//! > function_name
foo
//! > module_code
enum MyEnum<T> {
A: T,
B: (felt252, felt252),
}
fn bar(a: MyEnum<u32>) {}
//! > expected_diagnostics
error[E2041]: Unexpected argument type. Expected: "test::MyEnum::<core::integer::u32>", found: "test::MyEnum::<core::felt252>".
--> lib.cairo:11:9
bar(a);
^
//! > ==========================================================================
//! > Test match on variant without args.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(a: A, b: A) -> felt252 {
match (a, b) {
(A::One, _) => 2,
(_, _) => 6,
}
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum A {
One: felt252,
}
//! > expected_diagnostics
warning[E2192]: Pattern missing subpattern for the payload of variant. Consider using `A::One(_)`
--> lib.cairo:7:10
(A::One, _) => 2,
^^^^^^
//! > ==========================================================================
//! > Test match on unit variant without args.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)
//! > function_code
fn foo(a: A, b: A) -> felt252 {
match (a, b) {
(A::One(_), _) => 2,
(A::Two, _) => 6,
}
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum A {
One: felt252,
Two: (),
}
//! > expected_diagnostics
//! > ==========================================================================
//! > Test match on unit variant with args.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)
//! > function_code
fn foo(a: A, b: A) -> felt252 {
match (a, b) {
(A::One(_), _) => 2,
(A::Two(_), _) => 6,
}
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum A {
One: felt252,
Two: (),
}
//! > expected_diagnostics
//! > ==========================================================================
//! > Test match on Some without args.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo<T>(a: Option<T>) -> felt252 {
match a {
Some => 2,
None => 6,
}
}
fn bar() -> felt252 {
let a = Some(());
foo(a)
}
//! > function_name
bar
//! > expected_diagnostics
warning[E2192]: Pattern missing subpattern for the payload of variant. Consider using `Some(_)`
--> lib.cairo:3:9
Some => 2,
^^^^
//! > ==========================================================================
//! > Test match on Path no args.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: false)
//! > function_code
fn foo(a: A, b: A) -> felt252 {
match (a, b) {
(A::One(_), _) => 2,
(Two, _) => 6,
}
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum A {
One: felt252,
Two: (),
}
use A::Two;
//! > expected_diagnostics
//! > ==========================================================================
//! > Test match on inferred unit Some without args.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn fb() -> Option<()> {
let x = None;
match x {
Some => 2,
None => 6,
}
return x;
}
//! > function_name
fb
//! > expected_diagnostics
warning[E2192]: Pattern missing subpattern for the payload of variant. Consider using `Some(_)`
--> lib.cairo:4:9
Some => 2,
^^^^
//! > ==========================================================================
//! > Test match on inferred unit Option::Some without args.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn fb() -> Option<()> {
let x = None;
match x {
Option::Some => 2,
Option::None => 6,
}
return x;
}
//! > function_name
fb
//! > expected_diagnostics
warning[E2192]: Pattern missing subpattern for the payload of variant. Consider using `Option::Some(_)`
--> lib.cairo:4:9
Option::Some => 2,
^^^^^^^^^^^^
//! > ==========================================================================
//! > Conform enum generic args in match patterns.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo() -> u32 {
let x = Some(3);
match x {
Some::<u32>(y) => y,
None::<u8> => 6,
}
}
//! > function_name
foo
//! > expected_diagnostics
error[E2302]: Type mismatch: `core::option::Option::<core::integer::u32>` and `core::option::Option::<core::integer::u8>`.
--> lib.cairo:5:9
None::<u8> => 6,
^^^^^^^^^^
//! > ==========================================================================
//! > Test bad type in multi level enum pattern variable binding.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(x: Result<Option<u8>, felt252>) {
let _y = match x {
Ok(Some(x)) | Err(x) => x,
_ => 3,
};
}
//! > function_name
foo
//! > expected_diagnostics
error[E2039]: Expected type "core::integer::u8", found: "core::felt252".
--> lib.cairo:3:27
Ok(Some(x)) | Err(x) => x,
^
//! > ==========================================================================
//! > Test bad inner pattern type in multi level enum pattern.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn foo(x: Result<Option<u8>, felt252>) {
let _y = match x {
Ok(Ok(x)) | Err(x) => x,
_ => 3,
};
}
//! > function_name
foo
//! > expected_diagnostics
error[E2109]: Wrong enum in pattern. Expected: "Option". Got: "Result".
--> lib.cairo:3:12
Ok(Ok(x)) | Err(x) => x,
^^^^^
error[E2048]: Missing variable in pattern.
--> lib.cairo:3:9
Ok(Ok(x)) | Err(x) => x,
^^^^^^^^^
//! > ==========================================================================
//! > Test value match after enum match.
//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)
//! > function_code
fn bar(x: felt252) {
foo(Ok(Some(x)));
}
//! > function_name
bar
//! > module_code
fn foo<T>(x: Result<Option<T>, felt252>) {
let _y = match x {
Ok(Some(x)) | Err(x) => x,
_ => 3,
};
}
//! > expected_diagnostics
error[E2039]: Expected type "T", found: "core::felt252".
--> lib.cairo:3:27
Ok(Some(x)) | Err(x) => x,
^