//! > Test match optimizer simple scenario.
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo() -> Option<u16> {
let v = get_option();
match v {
Some(_) => Some(1_u16),
None => None,
}
}
//! > function_name
foo
//! > module_code
extern fn get_option() -> Option<u16> nopanic;
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > before
Parameters:
blk0 (root):
Statements:
End:
Match(match test::get_option() {
Option::Some(v0) => blk1,
Option::None => blk2,
})
blk1:
Statements:
(v1: core::option::Option::<core::integer::u16>) <- Option::Some(v0)
End:
Goto(blk3, {v1 -> v2})
blk2:
Statements:
(v3: ()) <- struct_construct()
(v4: core::option::Option::<core::integer::u16>) <- Option::None(v3)
End:
Goto(blk3, {v4 -> v2})
blk3:
Statements:
End:
Match(match_enum(v2) {
Option::Some(v5) => blk4,
Option::None(v6) => blk5,
})
blk4:
Statements:
(v7: core::integer::u16) <- 1
(v8: core::option::Option::<core::integer::u16>) <- Option::Some(v7)
End:
Goto(blk6, {v8 -> v9})
blk5:
Statements:
(v10: ()) <- struct_construct()
(v11: core::option::Option::<core::integer::u16>) <- Option::None(v10)
End:
Goto(blk6, {v11 -> v9})
blk6:
Statements:
End:
Return(v9)
//! > after
Parameters:
blk0 (root):
Statements:
End:
Match(match test::get_option() {
Option::Some(v0) => blk1,
Option::None => blk2,
})
blk1:
Statements:
End:
Goto(blk4, {v0 -> v5})
blk2:
Statements:
(v3: ()) <- struct_construct()
End:
Goto(blk5, {v3 -> v6})
blk3:
Statements:
End:
Match(match_enum(v2) {
Option::Some(v13) => blk8,
Option::None(v12) => blk7,
})
blk4:
Statements:
(v7: core::integer::u16) <- 1
(v8: core::option::Option::<core::integer::u16>) <- Option::Some(v7)
End:
Goto(blk6, {v8 -> v9})
blk5:
Statements:
(v10: ()) <- struct_construct()
(v11: core::option::Option::<core::integer::u16>) <- Option::None(v10)
End:
Goto(blk6, {v11 -> v9})
blk6:
Statements:
End:
Return(v9)
blk7:
Statements:
End:
Goto(blk5, {v12 -> v6})
blk8:
Statements:
End:
Goto(blk4, {v13 -> v5})
//! > ==========================================================================
//! > Test skipping of match optimization.
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo(a: felt252) -> Option<u16> {
let v = get_option();
// The following instruction should block the optimization.
core::internal::revoke_ap_tracking();
let v1 = match v {
Some(_) => Some(1_u16),
None => None,
};
match v1 {
// v1 is used after the match.
Some(_) => v1,
None => Some(2_u16),
}
}
//! > function_name
foo
//! > module_code
extern fn get_option() -> Option<u16> nopanic;
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > before
Parameters: v0: core::felt252
blk0 (root):
Statements:
End:
Match(match test::get_option() {
Option::Some(v1) => blk1,
Option::None => blk2,
})
blk1:
Statements:
(v2: core::option::Option::<core::integer::u16>) <- Option::Some(v1)
End:
Goto(blk3, {v2 -> v3})
blk2:
Statements:
(v4: ()) <- struct_construct()
(v5: core::option::Option::<core::integer::u16>) <- Option::None(v4)
End:
Goto(blk3, {v5 -> v3})
blk3:
Statements:
() <- core::internal::revoke_ap_tracking()
End:
Match(match_enum(v3) {
Option::Some(v6) => blk4,
Option::None(v7) => blk5,
})
blk4:
Statements:
(v8: core::integer::u16) <- 1
(v9: core::option::Option::<core::integer::u16>) <- Option::Some(v8)
End:
Goto(blk6, {v9 -> v10})
blk5:
Statements:
(v11: ()) <- struct_construct()
(v12: core::option::Option::<core::integer::u16>) <- Option::None(v11)
End:
Goto(blk6, {v12 -> v10})
blk6:
Statements:
End:
Match(match_enum(v10) {
Option::Some(v13) => blk7,
Option::None(v14) => blk8,
})
blk7:
Statements:
End:
Goto(blk9, {v10 -> v15})
blk8:
Statements:
(v16: core::integer::u16) <- 2
(v17: core::option::Option::<core::integer::u16>) <- Option::Some(v16)
End:
Goto(blk9, {v17 -> v15})
blk9:
Statements:
End:
Return(v15)
//! > after
Parameters: v0: core::felt252
blk0 (root):
Statements:
End:
Match(match test::get_option() {
Option::Some(v1) => blk1,
Option::None => blk2,
})
blk1:
Statements:
() <- core::internal::revoke_ap_tracking()
End:
Goto(blk4, {v1 -> v6})
blk2:
Statements:
(v4: ()) <- struct_construct()
() <- core::internal::revoke_ap_tracking()
End:
Goto(blk5, {v4 -> v7})
blk3:
Statements:
() <- core::internal::revoke_ap_tracking()
End:
Match(match_enum(v3) {
Option::Some(v19) => blk11,
Option::None(v18) => blk10,
})
blk4:
Statements:
(v8: core::integer::u16) <- 1
(v9: core::option::Option::<core::integer::u16>) <- Option::Some(v8)
End:
Goto(blk6, {v9 -> v10})
blk5:
Statements:
(v11: ()) <- struct_construct()
(v12: core::option::Option::<core::integer::u16>) <- Option::None(v11)
End:
Goto(blk6, {v12 -> v10})
blk6:
Statements:
End:
Match(match_enum(v10) {
Option::Some(v13) => blk7,
Option::None(v14) => blk8,
})
blk7:
Statements:
End:
Goto(blk9, {v10 -> v15})
blk8:
Statements:
(v16: core::integer::u16) <- 2
(v17: core::option::Option::<core::integer::u16>) <- Option::Some(v16)
End:
Goto(blk9, {v17 -> v15})
blk9:
Statements:
End:
Return(v15)
blk10:
Statements:
End:
Goto(blk5, {v18 -> v7})
blk11:
Statements:
End:
Goto(blk4, {v19 -> v6})
//! > ==========================================================================
//! > Test two matches.
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo() -> felt252 {
let opt = get_option();
match opt {
Some(x) => Some(x + 3),
None => None,
}.unwrap()
}
//! > function_name
foo
//! > module_code
extern fn get_option() -> Option<felt252> nopanic;
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > before
Parameters:
blk0 (root):
Statements:
End:
Match(match test::get_option() {
Option::Some(v0) => blk1,
Option::None => blk2,
})
blk1:
Statements:
(v1: core::option::Option::<core::felt252>) <- Option::Some(v0)
End:
Goto(blk3, {v1 -> v2})
blk2:
Statements:
(v3: ()) <- struct_construct()
(v4: core::option::Option::<core::felt252>) <- Option::None(v3)
End:
Goto(blk3, {v4 -> v2})
blk3:
Statements:
End:
Match(match_enum(v2) {
Option::Some(v5) => blk4,
Option::None(v6) => blk5,
})
blk4:
Statements:
(v7: core::felt252) <- 3
(v8: core::felt252) <- core::felt252_add(v5, v7)
(v9: core::option::Option::<core::felt252>) <- Option::Some(v8)
End:
Goto(blk6, {v9 -> v10})
blk5:
Statements:
(v11: ()) <- struct_construct()
(v12: core::option::Option::<core::felt252>) <- Option::None(v11)
End:
Goto(blk6, {v12 -> v10})
blk6:
Statements:
End:
Match(match_enum(v10) {
Option::Some(v13) => blk7,
Option::None(v14) => blk8,
})
blk7:
Statements:
(v15: (core::felt252,)) <- struct_construct(v13)
(v16: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v15)
End:
Goto(blk9, {v16 -> v17})
blk8:
Statements:
(v18: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<29721761890975875353235833581453094220424382983267374>()
(v19: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v18)
End:
Goto(blk9, {v19 -> v17})
blk9:
Statements:
End:
Match(match_enum(v17) {
PanicResult::Ok(v20) => blk10,
PanicResult::Err(v21) => blk11,
})
blk10:
Statements:
(v22: core::felt252) <- struct_destructure(v20)
(v23: (core::felt252,)) <- struct_construct(v22)
(v24: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v23)
End:
Return(v24)
blk11:
Statements:
(v25: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v21)
End:
Return(v25)
//! > after
Parameters:
blk0 (root):
Statements:
End:
Match(match test::get_option() {
Option::Some(v0) => blk1,
Option::None => blk2,
})
blk1:
Statements:
End:
Goto(blk4, {v0 -> v5})
blk2:
Statements:
(v3: ()) <- struct_construct()
End:
Goto(blk5, {v3 -> v6})
blk3:
Statements:
End:
Match(match_enum(v2) {
Option::Some(v31) => blk17,
Option::None(v30) => blk16,
})
blk4:
Statements:
(v7: core::felt252) <- 3
(v8: core::felt252) <- core::felt252_add(v5, v7)
End:
Goto(blk7, {v8 -> v13})
blk5:
Statements:
(v11: ()) <- struct_construct()
End:
Goto(blk8, {v11 -> v14})
blk6:
Statements:
End:
Match(match_enum(v10) {
Option::Some(v29) => blk15,
Option::None(v28) => blk14,
})
blk7:
Statements:
(v15: (core::felt252,)) <- struct_construct(v13)
End:
Goto(blk10, {v15 -> v20})
blk8:
Statements:
(v18: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<29721761890975875353235833581453094220424382983267374>()
End:
Goto(blk11, {v18 -> v21})
blk9:
Statements:
End:
Match(match_enum(v17) {
PanicResult::Ok(v27) => blk13,
PanicResult::Err(v26) => blk12,
})
blk10:
Statements:
(v22: core::felt252) <- struct_destructure(v20)
(v23: (core::felt252,)) <- struct_construct(v22)
(v24: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Ok(v23)
End:
Return(v24)
blk11:
Statements:
(v25: core::panics::PanicResult::<(core::felt252,)>) <- PanicResult::Err(v21)
End:
Return(v25)
blk12:
Statements:
End:
Goto(blk11, {v26 -> v21})
blk13:
Statements:
End:
Goto(blk10, {v27 -> v20})
blk14:
Statements:
End:
Goto(blk8, {v28 -> v14})
blk15:
Statements:
End:
Goto(blk7, {v29 -> v13})
blk16:
Statements:
End:
Goto(blk5, {v30 -> v6})
blk17:
Statements:
End:
Goto(blk4, {v31 -> v5})
//! > ==========================================================================
//! > withdraw_gas
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo() {
gas::withdraw_gas().expect('Out of gas');
}
//! > function_name
foo
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > before
Parameters:
blk0 (root):
Statements:
End:
Match(match core::gas::withdraw_gas() {
Option::Some => blk1,
Option::None => blk2,
})
blk1:
Statements:
(v0: ()) <- struct_construct()
(v1: core::option::Option::<()>) <- Option::Some(v0)
End:
Goto(blk3, {v1 -> v2})
blk2:
Statements:
(v3: ()) <- struct_construct()
(v4: core::option::Option::<()>) <- Option::None(v3)
End:
Goto(blk3, {v4 -> v2})
blk3:
Statements:
End:
Match(match_enum(v2) {
Option::Some(v6) => blk4,
Option::None(v7) => blk5,
})
blk4:
Statements:
(v8: ((),)) <- struct_construct(v6)
(v9: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v8)
End:
Goto(blk6, {v9 -> v10})
blk5:
Statements:
(v11: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
(v12: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v11)
End:
Goto(blk6, {v12 -> v10})
blk6:
Statements:
End:
Match(match_enum(v10) {
PanicResult::Ok(v13) => blk7,
PanicResult::Err(v14) => blk8,
})
blk7:
Statements:
(v16: ()) <- struct_construct()
(v17: ((),)) <- struct_construct(v16)
(v18: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v17)
End:
Return(v18)
blk8:
Statements:
(v19: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v14)
End:
Return(v19)
//! > after
Parameters:
blk0 (root):
Statements:
End:
Match(match core::gas::withdraw_gas() {
Option::Some => blk1,
Option::None => blk2,
})
blk1:
Statements:
(v0: ()) <- struct_construct()
End:
Goto(blk4, {v0 -> v6})
blk2:
Statements:
(v3: ()) <- struct_construct()
End:
Goto(blk5, {v3 -> v7})
blk3:
Statements:
End:
Match(match_enum(v2) {
Option::Some(v23) => blk12,
Option::None(v22) => blk11,
})
blk4:
Statements:
(v8: ((),)) <- struct_construct(v6)
End:
Goto(blk7, {v8 -> v13})
blk5:
Statements:
(v11: (core::panics::Panic, core::array::Array::<core::felt252>)) <- core::panic_with_const_felt252::<375233589013918064796019>()
End:
Goto(blk8, {v11 -> v14})
blk6:
Statements:
End:
Match(match_enum(v10) {
PanicResult::Ok(v21) => blk10,
PanicResult::Err(v20) => blk9,
})
blk7:
Statements:
(v16: ()) <- struct_construct()
(v17: ((),)) <- struct_construct(v16)
(v18: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v17)
End:
Return(v18)
blk8:
Statements:
(v19: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v14)
End:
Return(v19)
blk9:
Statements:
End:
Goto(blk8, {v20 -> v14})
blk10:
Statements:
End:
Goto(blk7, {v21 -> v13})
blk11:
Statements:
End:
Goto(blk5, {v22 -> v7})
blk12:
Statements:
End:
Goto(blk4, {v23 -> v6})
//! > ==========================================================================
//! > Convergence after the optimization is applied to one arm.
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo(a: MyEnum) -> u32 {
let b = match a {
MyEnum::A(_) => MyEnum::A(5),
MyEnum::B(_) => a,
};
match b {
MyEnum::A(x) => x,
MyEnum::B(x) => x,
}
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum MyEnum {
A: u32,
B: u32,
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > before
Parameters: v0: test::MyEnum
blk0 (root):
Statements:
End:
Match(match_enum(v0) {
MyEnum::A(v1) => blk1,
MyEnum::B(v2) => blk2,
})
blk1:
Statements:
(v3: core::integer::u32) <- 5
(v4: test::MyEnum) <- MyEnum::A(v3)
End:
Goto(blk3, {v4 -> v5})
blk2:
Statements:
End:
Goto(blk3, {v0 -> v5})
blk3:
Statements:
End:
Match(match_enum(v5) {
MyEnum::A(v6) => blk4,
MyEnum::B(v7) => blk5,
})
blk4:
Statements:
End:
Goto(blk6, {v6 -> v8})
blk5:
Statements:
End:
Goto(blk6, {v7 -> v8})
blk6:
Statements:
End:
Return(v8)
//! > after
Parameters: v0: test::MyEnum
blk0 (root):
Statements:
End:
Match(match_enum(v0) {
MyEnum::A(v1) => blk1,
MyEnum::B(v2) => blk2,
})
blk1:
Statements:
(v3: core::integer::u32) <- 5
End:
Goto(blk4, {v3 -> v6})
blk2:
Statements:
End:
Goto(blk3, {v0 -> v5})
blk3:
Statements:
End:
Match(match_enum(v5) {
MyEnum::A(v9) => blk7,
MyEnum::B(v7) => blk5,
})
blk4:
Statements:
End:
Goto(blk6, {v6 -> v8})
blk5:
Statements:
End:
Goto(blk6, {v7 -> v8})
blk6:
Statements:
End:
Return(v8)
blk7:
Statements:
End:
Goto(blk4, {v9 -> v6})
//! > ==========================================================================
//! > ref argument remapping with return.
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo(ref a: u32, c: Color) -> Color {
let c = match c {
Color::Red(x) => {
a = 0;
Color::Red(x)
},
Color::Green(x) => Color::Green(x),
// Map `Blue` to `Green` to check an arm that is reached twice.
Color::Blue(x) => Color::Green(x),
Color::White(_) => c,
};
match c {
Color::Red(x) => { return Color::Red(x); },
Color::Green(x) => { return Color::Green(x); },
Color::Blue(x) => { return Color::Blue(x); },
Color::White(x) => { return Color::White(x); },
}
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum Color {
Red: u32,
Green: u32,
Blue: u32,
White: u32,
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > before
Parameters: v0: core::integer::u32, v1: test::Color
blk0 (root):
Statements:
End:
Match(match_enum(v1) {
Color::Red(v2) => blk1,
Color::Green(v3) => blk2,
Color::Blue(v4) => blk3,
Color::White(v5) => blk4,
})
blk1:
Statements:
(v6: core::integer::u32) <- 0
(v7: test::Color) <- Color::Red(v2)
End:
Goto(blk5, {v6 -> v8, v7 -> v9})
blk2:
Statements:
(v10: test::Color) <- Color::Green(v3)
End:
Goto(blk5, {v0 -> v8, v10 -> v9})
blk3:
Statements:
(v11: test::Color) <- Color::Green(v4)
End:
Goto(blk5, {v0 -> v8, v11 -> v9})
blk4:
Statements:
End:
Goto(blk5, {v0 -> v8, v1 -> v9})
blk5:
Statements:
End:
Match(match_enum(v9) {
Color::Red(v12) => blk6,
Color::Green(v13) => blk7,
Color::Blue(v14) => blk8,
Color::White(v15) => blk9,
})
blk6:
Statements:
(v16: test::Color) <- Color::Red(v12)
End:
Return(v8, v16)
blk7:
Statements:
(v17: test::Color) <- Color::Green(v13)
End:
Return(v8, v17)
blk8:
Statements:
(v18: test::Color) <- Color::Blue(v14)
End:
Return(v8, v18)
blk9:
Statements:
(v19: test::Color) <- Color::White(v15)
End:
Return(v8, v19)
//! > after
Parameters: v0: core::integer::u32, v1: test::Color
blk0 (root):
Statements:
End:
Match(match_enum(v1) {
Color::Red(v2) => blk1,
Color::Green(v3) => blk2,
Color::Blue(v4) => blk3,
Color::White(v5) => blk4,
})
blk1:
Statements:
(v6: core::integer::u32) <- 0
End:
Goto(blk6, {v2 -> v12, v6 -> v22})
blk2:
Statements:
End:
Goto(blk7, {v3 -> v13, v0 -> v20})
blk3:
Statements:
End:
Goto(blk7, {v4 -> v13, v0 -> v20})
blk4:
Statements:
End:
Goto(blk5, {v0 -> v8, v1 -> v9})
blk5:
Statements:
End:
Match(match_enum(v9) {
Color::Red(v23) => blk11,
Color::Green(v21) => blk10,
Color::Blue(v14) => blk8,
Color::White(v15) => blk9,
})
blk6:
Statements:
(v16: test::Color) <- Color::Red(v12)
End:
Return(v22, v16)
blk7:
Statements:
(v17: test::Color) <- Color::Green(v13)
End:
Return(v20, v17)
blk8:
Statements:
(v18: test::Color) <- Color::Blue(v14)
End:
Return(v8, v18)
blk9:
Statements:
(v19: test::Color) <- Color::White(v15)
End:
Return(v8, v19)
blk10:
Statements:
End:
Goto(blk7, {v21 -> v13, v8 -> v20})
blk11:
Statements:
End:
Goto(blk6, {v23 -> v12, v8 -> v22})
//! > ==========================================================================
//! > ref argument remapping with merge.
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo(ref a: u32, c: Color) -> Color {
let c = match c {
Color::Red(x) => {
a = 0;
Color::Red(x)
},
Color::Green(x) => Color::Green(x),
// Map `Blue` to `Green` to check an arm that is reached twice.
Color::Blue(x) => Color::Green(x),
Color::White(_) => c,
};
match c {
Color::Red(x) => Color::Red(x),
Color::Green(x) => Color::Green(x),
Color::Blue(x) => Color::Blue(x),
Color::White(x) => Color::White(x),
}
}
//! > function_name
foo
//! > module_code
#[derive(Copy, Drop)]
enum Color {
Red: u32,
Green: u32,
Blue: u32,
White: u32,
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > before
Parameters: v0: core::integer::u32, v1: test::Color
blk0 (root):
Statements:
End:
Match(match_enum(v1) {
Color::Red(v2) => blk1,
Color::Green(v3) => blk2,
Color::Blue(v4) => blk3,
Color::White(v5) => blk4,
})
blk1:
Statements:
(v6: core::integer::u32) <- 0
(v7: test::Color) <- Color::Red(v2)
End:
Goto(blk5, {v6 -> v8, v7 -> v9})
blk2:
Statements:
(v10: test::Color) <- Color::Green(v3)
End:
Goto(blk5, {v0 -> v8, v10 -> v9})
blk3:
Statements:
(v11: test::Color) <- Color::Green(v4)
End:
Goto(blk5, {v0 -> v8, v11 -> v9})
blk4:
Statements:
End:
Goto(blk5, {v0 -> v8, v1 -> v9})
blk5:
Statements:
End:
Match(match_enum(v9) {
Color::Red(v12) => blk6,
Color::Green(v13) => blk7,
Color::Blue(v14) => blk8,
Color::White(v15) => blk9,
})
blk6:
Statements:
(v16: test::Color) <- Color::Red(v12)
End:
Goto(blk10, {v16 -> v17})
blk7:
Statements:
(v18: test::Color) <- Color::Green(v13)
End:
Goto(blk10, {v18 -> v17})
blk8:
Statements:
(v19: test::Color) <- Color::Blue(v14)
End:
Goto(blk10, {v19 -> v17})
blk9:
Statements:
(v20: test::Color) <- Color::White(v15)
End:
Goto(blk10, {v20 -> v17})
blk10:
Statements:
End:
Return(v8, v17)
//! > after
Parameters: v0: core::integer::u32, v1: test::Color
blk0 (root):
Statements:
End:
Match(match_enum(v1) {
Color::Red(v2) => blk1,
Color::Green(v3) => blk2,
Color::Blue(v4) => blk3,
Color::White(v5) => blk4,
})
blk1:
Statements:
(v6: core::integer::u32) <- 0
(v7: test::Color) <- Color::Red(v2)
End:
Goto(blk5, {v6 -> v8, v7 -> v9})
blk2:
Statements:
(v10: test::Color) <- Color::Green(v3)
End:
Goto(blk5, {v0 -> v8, v10 -> v9})
blk3:
Statements:
(v11: test::Color) <- Color::Green(v4)
End:
Goto(blk5, {v0 -> v8, v11 -> v9})
blk4:
Statements:
End:
Goto(blk5, {v0 -> v8, v1 -> v9})
blk5:
Statements:
End:
Match(match_enum(v9) {
Color::Red(v12) => blk6,
Color::Green(v13) => blk7,
Color::Blue(v14) => blk8,
Color::White(v15) => blk9,
})
blk6:
Statements:
(v16: test::Color) <- Color::Red(v12)
End:
Goto(blk10, {v16 -> v17})
blk7:
Statements:
(v18: test::Color) <- Color::Green(v13)
End:
Goto(blk10, {v18 -> v17})
blk8:
Statements:
(v19: test::Color) <- Color::Blue(v14)
End:
Goto(blk10, {v19 -> v17})
blk9:
Statements:
(v20: test::Color) <- Color::White(v15)
End:
Goto(blk10, {v20 -> v17})
blk10:
Statements:
End:
Return(v8, v17)
//! > ==========================================================================
//! > Match optimization with multiple remappings in the way.
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo(ref a: Array<felt252>, ref b: Array<felt252>) -> Option<()> {
let _ = double_merge(ref a, ref b)?;
let _ = double_merge(ref a, ref b)?;
Some(())
}
//! > function_name
foo
//! > module_code
fn double_merge(ref a: Array<felt252>, ref b: Array<felt252>) -> Option<felt252> {
if let Some(q) = a.pop_front() {
if q == 0 {
return Some(q);
}
}
b.pop_front()
}
//! > semantic_diagnostics
//! > before
Parameters: v0: core::array::Array::<core::felt252>, v1: core::array::Array::<core::felt252>
blk0 (root):
Statements:
End:
Match(match core::array::array_pop_front::<core::felt252>(v0) {
Option::Some(v2, v3) => blk1,
Option::None(v4) => blk2,
})
blk1:
Statements:
(v5: core::felt252) <- unbox(v3)
(v6: core::option::Option::<core::felt252>) <- Option::Some(v5)
End:
Goto(blk3, {v2 -> v7, v6 -> v8})
blk2:
Statements:
(v9: ()) <- struct_construct()
(v10: core::option::Option::<core::felt252>) <- Option::None(v9)
End:
Goto(blk3, {v4 -> v7, v10 -> v8})
blk3:
Statements:
End:
Match(match_enum(v8) {
Option::Some(v11) => blk4,
Option::None(v12) => blk7,
})
blk4:
Statements:
End:
Match(match core::felt252_is_zero(v11) {
IsZeroResult::Zero => blk5,
IsZeroResult::NonZero(v13) => blk6,
})
blk5:
Statements:
(v14: core::option::Option::<core::felt252>) <- Option::Some(v11)
End:
Goto(blk11, {v1 -> v15, v14 -> v16})
blk6:
Statements:
End:
Goto(blk8, {})
blk7:
Statements:
End:
Goto(blk8, {})
blk8:
Statements:
End:
Match(match core::array::array_pop_front::<core::felt252>(v1) {
Option::Some(v17, v18) => blk9,
Option::None(v19) => blk10,
})
blk9:
Statements:
(v20: core::felt252) <- unbox(v18)
(v21: core::option::Option::<core::felt252>) <- Option::Some(v20)
End:
Goto(blk11, {v17 -> v15, v21 -> v16})
blk10:
Statements:
(v22: ()) <- struct_construct()
(v23: core::option::Option::<core::felt252>) <- Option::None(v22)
End:
Goto(blk11, {v19 -> v15, v23 -> v16})
blk11:
Statements:
End:
Match(match_enum(v16) {
Option::Some(v24) => blk12,
Option::None(v25) => blk26,
})
blk12:
Statements:
End:
Match(match core::array::array_pop_front::<core::felt252>(v7) {
Option::Some(v26, v27) => blk13,
Option::None(v28) => blk14,
})
blk13:
Statements:
(v29: core::felt252) <- unbox(v27)
(v30: core::option::Option::<core::felt252>) <- Option::Some(v29)
End:
Goto(blk15, {v26 -> v31, v30 -> v32})
blk14:
Statements:
(v33: ()) <- struct_construct()
(v34: core::option::Option::<core::felt252>) <- Option::None(v33)
End:
Goto(blk15, {v28 -> v31, v34 -> v32})
blk15:
Statements:
End:
Match(match_enum(v32) {
Option::Some(v35) => blk16,
Option::None(v36) => blk19,
})
blk16:
Statements:
End:
Match(match core::felt252_is_zero(v35) {
IsZeroResult::Zero => blk17,
IsZeroResult::NonZero(v37) => blk18,
})
blk17:
Statements:
(v38: core::option::Option::<core::felt252>) <- Option::Some(v35)
End:
Goto(blk23, {v15 -> v39, v38 -> v40})
blk18:
Statements:
End:
Goto(blk20, {})
blk19:
Statements:
End:
Goto(blk20, {})
blk20:
Statements:
End:
Match(match core::array::array_pop_front::<core::felt252>(v15) {
Option::Some(v41, v42) => blk21,
Option::None(v43) => blk22,
})
blk21:
Statements:
(v44: core::felt252) <- unbox(v42)
(v45: core::option::Option::<core::felt252>) <- Option::Some(v44)
End:
Goto(blk23, {v41 -> v39, v45 -> v40})
blk22:
Statements:
(v46: ()) <- struct_construct()
(v47: core::option::Option::<core::felt252>) <- Option::None(v46)
End:
Goto(blk23, {v43 -> v39, v47 -> v40})
blk23:
Statements:
End:
Match(match_enum(v40) {
Option::Some(v48) => blk24,
Option::None(v49) => blk25,
})
blk24:
Statements:
(v50: ()) <- struct_construct()
(v51: core::option::Option::<()>) <- Option::Some(v50)
End:
Return(v31, v39, v51)
blk25:
Statements:
(v52: core::option::Option::<()>) <- Option::None(v49)
End:
Return(v31, v39, v52)
blk26:
Statements:
(v53: core::option::Option::<()>) <- Option::None(v25)
End:
Return(v7, v15, v53)
//! > after
Parameters: v0: core::array::Array::<core::felt252>, v1: core::array::Array::<core::felt252>
blk0 (root):
Statements:
End:
Match(match core::array::array_pop_front::<core::felt252>(v0) {
Option::Some(v2, v3) => blk1,
Option::None(v4) => blk2,
})
blk1:
Statements:
(v5: core::felt252) <- unbox(v3)
(v6: core::option::Option::<core::felt252>) <- Option::Some(v5)
End:
Goto(blk3, {v2 -> v7, v6 -> v8})
blk2:
Statements:
(v9: ()) <- struct_construct()
(v10: core::option::Option::<core::felt252>) <- Option::None(v9)
End:
Goto(blk3, {v4 -> v7, v10 -> v8})
blk3:
Statements:
End:
Match(match_enum(v8) {
Option::Some(v11) => blk4,
Option::None(v12) => blk7,
})
blk4:
Statements:
End:
Match(match core::felt252_is_zero(v11) {
IsZeroResult::Zero => blk5,
IsZeroResult::NonZero(v13) => blk6,
})
blk5:
Statements:
End:
Goto(blk12, {v11 -> v24, v1 -> v60})
blk6:
Statements:
End:
Goto(blk8, {})
blk7:
Statements:
End:
Goto(blk8, {})
blk8:
Statements:
End:
Match(match core::array::array_pop_front::<core::felt252>(v1) {
Option::Some(v17, v18) => blk9,
Option::None(v19) => blk10,
})
blk9:
Statements:
(v20: core::felt252) <- unbox(v18)
End:
Goto(blk12, {v20 -> v24, v17 -> v60})
blk10:
Statements:
(v22: ()) <- struct_construct()
End:
Goto(blk26, {v22 -> v25, v19 -> v58})
blk11:
Statements:
End:
Match(match_enum(v16) {
Option::Some(v61) => blk30,
Option::None(v59) => blk29,
})
blk12:
Statements:
End:
Match(match core::array::array_pop_front::<core::felt252>(v7) {
Option::Some(v26, v27) => blk13,
Option::None(v28) => blk14,
})
blk13:
Statements:
(v29: core::felt252) <- unbox(v27)
(v30: core::option::Option::<core::felt252>) <- Option::Some(v29)
End:
Goto(blk15, {v26 -> v31, v30 -> v32})
blk14:
Statements:
(v33: ()) <- struct_construct()
(v34: core::option::Option::<core::felt252>) <- Option::None(v33)
End:
Goto(blk15, {v28 -> v31, v34 -> v32})
blk15:
Statements:
End:
Match(match_enum(v32) {
Option::Some(v35) => blk16,
Option::None(v36) => blk19,
})
blk16:
Statements:
End:
Match(match core::felt252_is_zero(v35) {
IsZeroResult::Zero => blk17,
IsZeroResult::NonZero(v37) => blk18,
})
blk17:
Statements:
End:
Goto(blk24, {v35 -> v48, v60 -> v56})
blk18:
Statements:
End:
Goto(blk20, {})
blk19:
Statements:
End:
Goto(blk20, {})
blk20:
Statements:
End:
Match(match core::array::array_pop_front::<core::felt252>(v60) {
Option::Some(v41, v42) => blk21,
Option::None(v43) => blk22,
})
blk21:
Statements:
(v44: core::felt252) <- unbox(v42)
End:
Goto(blk24, {v44 -> v48, v41 -> v56})
blk22:
Statements:
(v46: ()) <- struct_construct()
End:
Goto(blk25, {v46 -> v49, v43 -> v54})
blk23:
Statements:
End:
Match(match_enum(v40) {
Option::Some(v57) => blk28,
Option::None(v55) => blk27,
})
blk24:
Statements:
(v50: ()) <- struct_construct()
(v51: core::option::Option::<()>) <- Option::Some(v50)
End:
Return(v31, v56, v51)
blk25:
Statements:
(v52: core::option::Option::<()>) <- Option::None(v49)
End:
Return(v31, v54, v52)
blk26:
Statements:
(v53: core::option::Option::<()>) <- Option::None(v25)
End:
Return(v7, v58, v53)
blk27:
Statements:
End:
Goto(blk25, {v55 -> v49, v39 -> v54})
blk28:
Statements:
End:
Goto(blk24, {v57 -> v48, v39 -> v56})
blk29:
Statements:
End:
Goto(blk26, {v59 -> v25, v15 -> v58})
blk30:
Statements:
End:
Goto(blk12, {v61 -> v24, v15 -> v60})
//! > lowering_diagnostics
//! > ==========================================================================
//! > Match optimization with struct construct after merge.
//! > test_runner_name
test_match_optimizer
//! > test_comments
//! > function_code
fn foo(ref input: Span<felt252>) -> Option<@felt252> {
let mut snapshot = input.snapshot;
let item = array_snapshot_pop_front(ref snapshot);
input = Span { snapshot };
if let Some(x) = item {
return Some(x.unbox());
}
None
}
//! > function_name
foo
//! > module_code
use core::array::array_snapshot_pop_front;
//! > semantic_diagnostics
//! > before
Parameters: v0: core::array::Span::<core::felt252>
blk0 (root):
Statements:
(v1: @core::array::Array::<core::felt252>) <- struct_destructure(v0)
End:
Match(match core::array::array_snapshot_pop_front::<core::felt252>(v1) {
Option::Some(v2, v3) => blk1,
Option::None(v4) => blk2,
})
blk1:
Statements:
(v5: core::option::Option::<core::box::Box::<@core::felt252>>) <- Option::Some(v3)
End:
Goto(blk3, {v2 -> v6, v5 -> v7})
blk2:
Statements:
(v8: ()) <- struct_construct()
(v9: core::option::Option::<core::box::Box::<@core::felt252>>) <- Option::None(v8)
End:
Goto(blk3, {v4 -> v6, v9 -> v7})
blk3:
Statements:
(v10: core::array::Span::<core::felt252>) <- struct_construct(v6)
End:
Match(match_enum(v7) {
Option::Some(v11) => blk4,
Option::None(v12) => blk5,
})
blk4:
Statements:
(v13: @core::felt252) <- unbox(v11)
(v14: core::option::Option::<@core::felt252>) <- Option::Some(v13)
End:
Return(v10, v14)
blk5:
Statements:
(v15: ()) <- struct_construct()
(v16: core::option::Option::<@core::felt252>) <- Option::None(v15)
End:
Return(v10, v16)
//! > after
Parameters: v0: core::array::Span::<core::felt252>
blk0 (root):
Statements:
(v1: @core::array::Array::<core::felt252>) <- struct_destructure(v0)
End:
Match(match core::array::array_snapshot_pop_front::<core::felt252>(v1) {
Option::Some(v2, v3) => blk1,
Option::None(v4) => blk2,
})
blk1:
Statements:
(v22: core::array::Span::<core::felt252>) <- struct_construct(v2)
End:
Goto(blk4, {v3 -> v11, v2 -> v21, v22 -> v23})
blk2:
Statements:
(v8: ()) <- struct_construct()
(v18: core::array::Span::<core::felt252>) <- struct_construct(v4)
End:
Goto(blk5, {v8 -> v12, v4 -> v17, v18 -> v19})
blk3:
Statements:
(v10: core::array::Span::<core::felt252>) <- struct_construct(v6)
End:
Match(match_enum(v7) {
Option::Some(v24) => blk7,
Option::None(v20) => blk6,
})
blk4:
Statements:
(v13: @core::felt252) <- unbox(v11)
(v14: core::option::Option::<@core::felt252>) <- Option::Some(v13)
End:
Return(v23, v14)
blk5:
Statements:
(v15: ()) <- struct_construct()
(v16: core::option::Option::<@core::felt252>) <- Option::None(v15)
End:
Return(v19, v16)
blk6:
Statements:
End:
Goto(blk5, {v20 -> v12, v6 -> v17, v10 -> v19})
blk7:
Statements:
End:
Goto(blk4, {v24 -> v11, v6 -> v21, v10 -> v23})
//! > lowering_diagnostics
//! > ==========================================================================
//! > test enum variable is used in a struct.
//! > test_runner_name
test_match_optimizer
//! > function_code
fn foo() -> MyStruct {
let v = Some(1);
let res = MyStruct { v };
if let Some(_) = v {
return res;
}
res
}
//! > function_name
foo
//! > module_code
struct MyStruct {
v: Option<u32>,
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > before
Parameters:
blk0 (root):
Statements:
(v0: core::integer::u32) <- 1
(v1: core::option::Option::<core::integer::u32>) <- Option::Some(v0)
(v2: test::MyStruct) <- struct_construct(v1)
End:
Return(v2)
//! > after
Parameters:
blk0 (root):
Statements:
(v0: core::integer::u32) <- 1
(v1: core::option::Option::<core::integer::u32>) <- Option::Some(v0)
(v2: test::MyStruct) <- struct_construct(v1)
End:
Return(v2)
//! > ==========================================================================
//! > Match optimization with struct construct before merge.
//! > test_runner_name
test_match_optimizer
//! > test_comments
//! > function_code
fn foo(ref input: Span<felt252>) -> felt252 {
let res: Option<@Box<[felt252; 4]>> = input.multi_pop_front();
if let Some(_) = res {
return 1;
}
2
}
//! > function_name
foo
//! > module_code
use core::array::array_snapshot_multi_pop_front;
//! > semantic_diagnostics
//! > before
Parameters: v0: core::array::Span::<core::felt252>
blk0 (root):
Statements:
(v1: @core::array::Array::<core::felt252>) <- struct_destructure(v0)
End:
Match(match core::array::array_snapshot_multi_pop_front::<[core::felt252; 4], core::array::FixedSizedArrayInfoImpl::<core::felt252, 4>>(v1) {
Option::Some(v2, v3) => blk1,
Option::None(v4) => blk2,
})
blk1:
Statements:
(v5: core::array::Span::<core::felt252>) <- struct_construct(v2)
(v6: core::option::Option::<@core::box::Box::<[core::felt252; 4]>>) <- Option::Some(v3)
End:
Goto(blk3, {v5 -> v7, v6 -> v8})
blk2:
Statements:
(v9: ()) <- struct_construct()
(v10: core::option::Option::<@core::box::Box::<[core::felt252; 4]>>) <- Option::None(v9)
(v11: core::array::Span::<core::felt252>) <- struct_construct(v4)
End:
Goto(blk3, {v11 -> v7, v10 -> v8})
blk3:
Statements:
End:
Match(match_enum(v8) {
Option::Some(v12) => blk4,
Option::None(v13) => blk5,
})
blk4:
Statements:
(v14: core::felt252) <- 1
End:
Return(v7, v14)
blk5:
Statements:
(v15: core::felt252) <- 2
End:
Return(v7, v15)
//! > after
Parameters: v0: core::array::Span::<core::felt252>
blk0 (root):
Statements:
(v1: @core::array::Array::<core::felt252>) <- struct_destructure(v0)
End:
Match(match core::array::array_snapshot_multi_pop_front::<[core::felt252; 4], core::array::FixedSizedArrayInfoImpl::<core::felt252, 4>>(v1) {
Option::Some(v2, v3) => blk1,
Option::None(v4) => blk2,
})
blk1:
Statements:
(v5: core::array::Span::<core::felt252>) <- struct_construct(v2)
End:
Goto(blk4, {v3 -> v12, v5 -> v18})
blk2:
Statements:
(v9: ()) <- struct_construct()
(v11: core::array::Span::<core::felt252>) <- struct_construct(v4)
End:
Goto(blk5, {v9 -> v13, v11 -> v16})
blk3:
Statements:
End:
Match(match_enum(v8) {
Option::Some(v19) => blk7,
Option::None(v17) => blk6,
})
blk4:
Statements:
(v14: core::felt252) <- 1
End:
Return(v18, v14)
blk5:
Statements:
(v15: core::felt252) <- 2
End:
Return(v16, v15)
blk6:
Statements:
End:
Goto(blk5, {v17 -> v13, v7 -> v16})
blk7:
Statements:
End:
Goto(blk4, {v19 -> v12, v7 -> v18})
//! > lowering_diagnostics
//! > ==========================================================================
//! > enum construct result is used before the match.
//! > test_runner_name
test_match_optimizer
//! > test_comments
optimization is not applicable.
//! > function_code
fn foo(b: bool) -> felt252 {
let v = if b {
true
} else {
false
};
bar(v)
}
//! > function_name
foo
//! > module_code
extern fn use_var(a: bool) nopanic;
fn bar(b: bool) -> felt252 {
let a = !b;
if b {
use_var(a);
return 1;
}
use_var(a);
return 2;
}
//! > semantic_diagnostics
//! > before
Parameters: v0: core::bool
blk0 (root):
Statements:
End:
Match(match_enum(v0) {
bool::False(v1) => blk1,
bool::True(v2) => blk2,
})
blk1:
Statements:
(v3: ()) <- struct_construct()
(v4: core::bool) <- bool::False(v3)
End:
Goto(blk3, {v4 -> v5})
blk2:
Statements:
(v6: ()) <- struct_construct()
(v7: core::bool) <- bool::True(v6)
End:
Goto(blk3, {v7 -> v5})
blk3:
Statements:
(v8: core::bool) <- core::bool_not_impl(v5)
End:
Match(match_enum(v5) {
bool::False(v9) => blk4,
bool::True(v10) => blk5,
})
blk4:
Statements:
() <- test::use_var(v8)
(v11: core::felt252) <- 2
End:
Goto(blk6, {v11 -> v12})
blk5:
Statements:
() <- test::use_var(v8)
(v13: core::felt252) <- 1
End:
Goto(blk6, {v13 -> v12})
blk6:
Statements:
End:
Return(v12)
//! > after
Parameters: v0: core::bool
blk0 (root):
Statements:
End:
Match(match_enum(v0) {
bool::False(v1) => blk1,
bool::True(v2) => blk2,
})
blk1:
Statements:
(v3: ()) <- struct_construct()
(v4: core::bool) <- bool::False(v3)
End:
Goto(blk3, {v4 -> v5})
blk2:
Statements:
(v6: ()) <- struct_construct()
(v7: core::bool) <- bool::True(v6)
End:
Goto(blk3, {v7 -> v5})
blk3:
Statements:
(v8: core::bool) <- core::bool_not_impl(v5)
End:
Match(match_enum(v5) {
bool::False(v9) => blk4,
bool::True(v10) => blk5,
})
blk4:
Statements:
() <- test::use_var(v8)
(v11: core::felt252) <- 2
End:
Goto(blk6, {v11 -> v12})
blk5:
Statements:
() <- test::use_var(v8)
(v13: core::felt252) <- 1
End:
Goto(blk6, {v13 -> v12})
blk6:
Statements:
End:
Return(v12)
//! > lowering_diagnostics