//! > Test boolean if.
//! > test_runner_name
test_function_lowering(expect_diagnostics: false)
//! > function
fn foo(flag: bool) -> Option<A> {
let a = A {};
if flag {
Option::Some(a)
} else {
Option::None
}
}
//! > function_name
foo
//! > module_code
struct A {}
impl ADestruct of Destruct<A> {
#[inline(never)]
#[feature("corelib-internal-use")]
fn destruct(self: A) nopanic {
let A { } = self;
// Use RangeCheck, a previously unused implicit.
match core::integer::u128_overflowing_add(1_u128, 2_u128) {
Result::Ok(v) => v,
Result::Err(v) => v,
};
}
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::bool
blk0 (root):
Statements:
End:
Match(match_enum(v1) {
bool::False(v2) => blk1,
bool::True(v3) => blk2,
})
blk1:
Statements:
(v4: test::A) <- struct_construct()
(v5: core::RangeCheck) <- test::ADestruct::destruct(v0, v4)
(v6: ()) <- struct_construct()
(v7: core::option::Option::<test::A>) <- Option::None(v6)
End:
Return(v5, v7)
blk2:
Statements:
(v8: test::A) <- struct_construct()
(v9: core::option::Option::<test::A>) <- Option::Some(v8)
End:
Return(v0, v9)
//! > ==========================================================================
//! > Test dict destruct through panic.
//! > test_runner_name
test_function_lowering(expect_diagnostics: false)
//! > function
fn foo(d: Felt252Dict<felt252>) {
get_total_signed_weight(d);
}
//! > function_name
foo
//! > module_code
fn get_total_signed_weight(used_keys: Felt252Dict<felt252>) -> u128 {
1_u128
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v0: core::RangeCheck, v1: core::SegmentArena, v2: core::gas::GasBuiltin, v3: core::dict::Felt252Dict::<core::felt252>
blk0 (root):
Statements:
(v4: core::RangeCheck, v5: core::SegmentArena, v6: core::gas::GasBuiltin, v7: core::dict::SquashedFelt252Dict::<core::felt252>) <- core::dict::Felt252DictImpl::<core::felt252, core::Felt252Felt252DictValue>::squash(v0, v1, v2, v3)
End:
Return(v4, v5, v6)
//! > ==========================================================================
//! > Test panic destruct.
//! > test_runner_name
test_function_lowering(expect_diagnostics: false)
//! > function
fn foo(a: A, b: B) {
may_panic();
may_panic();
panic_with_felt252('123');
}
//! > function_name
foo
//! > module_code
#[inline(never)]
fn may_panic() {
panic_with_felt252('123');
}
struct A {}
impl APanicDestruct of PanicDestruct<A> {
#[inline(never)]
fn panic_destruct(self: A, ref panic: Panic) nopanic {
let A { } = self;
}
}
struct B {
a: A,
}
impl BPanicDestruct of PanicDestruct<B> {
#[inline(never)]
fn panic_destruct(self: B, ref panic: Panic) nopanic {
let B { a: _ } = self;
}
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v0: test::A, v1: test::B
blk0 (root):
Statements:
(v2: core::panics::PanicResult::<((),)>) <- test::may_panic()
End:
Match(match_enum(v2) {
PanicResult::Ok(v3) => blk1,
PanicResult::Err(v4) => blk4,
})
blk1:
Statements:
(v5: core::panics::PanicResult::<((),)>) <- test::may_panic()
End:
Match(match_enum(v5) {
PanicResult::Ok(v6) => blk2,
PanicResult::Err(v7) => blk3,
})
blk2:
Statements:
(v8: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v9: core::felt252) <- 3224115
(v10: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v8, v9)
(v11: core::panics::Panic) <- struct_construct()
(v12: core::panics::Panic) <- test::BPanicDestruct::panic_destruct(v1, v11)
(v13: core::panics::Panic) <- test::APanicDestruct::panic_destruct(v0, v12)
(v14: (core::panics::Panic, core::array::Array::<core::felt252>)) <- struct_construct(v13, v10)
(v15: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v14)
End:
Return(v15)
blk3:
Statements:
(v16: core::panics::Panic, v17: core::array::Array::<core::felt252>) <- struct_destructure(v7)
(v18: core::panics::Panic) <- test::BPanicDestruct::panic_destruct(v1, v16)
(v19: core::panics::Panic) <- test::APanicDestruct::panic_destruct(v0, v18)
(v20: (core::panics::Panic, core::array::Array::<core::felt252>)) <- struct_construct(v19, v17)
(v21: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v20)
End:
Return(v21)
blk4:
Statements:
(v22: core::panics::Panic, v23: core::array::Array::<core::felt252>) <- struct_destructure(v4)
(v24: core::panics::Panic) <- test::BPanicDestruct::panic_destruct(v1, v22)
(v25: core::panics::Panic) <- test::APanicDestruct::panic_destruct(v0, v24)
(v26: (core::panics::Panic, core::array::Array::<core::felt252>)) <- struct_construct(v25, v23)
(v27: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v26)
End:
Return(v27)
//! > ==========================================================================
//! > Test panic destruct with merge.
//! > test_runner_name
test_function_lowering(expect_diagnostics: false)
//! > function
fn foo(a: bool, d: A) -> A {
if a {
may_panic();
}
return d;
}
//! > function_name
foo
//! > module_code
#[inline(never)]
fn may_panic() {
panic_with_felt252('123');
}
struct A {}
impl APanicDestruct of PanicDestruct<A> {
#[inline(never)]
fn panic_destruct(self: A, ref panic: Panic) nopanic {
let A { } = self;
}
}
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v0: core::bool, v1: test::A
blk0 (root):
Statements:
End:
Match(match_enum(v0) {
bool::False(v2) => blk1,
bool::True(v3) => blk2,
})
blk1:
Statements:
End:
Goto(blk4, {})
blk2:
Statements:
(v4: core::panics::PanicResult::<((),)>) <- test::may_panic()
End:
Match(match_enum(v4) {
PanicResult::Ok(v5) => blk3,
PanicResult::Err(v6) => blk5,
})
blk3:
Statements:
End:
Goto(blk4, {})
blk4:
Statements:
(v7: (test::A,)) <- struct_construct(v1)
(v8: core::panics::PanicResult::<(test::A,)>) <- PanicResult::Ok(v7)
End:
Return(v8)
blk5:
Statements:
(v9: core::panics::Panic, v10: core::array::Array::<core::felt252>) <- struct_destructure(v6)
(v11: core::panics::Panic) <- test::APanicDestruct::panic_destruct(v1, v9)
(v12: (core::panics::Panic, core::array::Array::<core::felt252>)) <- struct_construct(v11, v10)
(v13: core::panics::PanicResult::<(test::A,)>) <- PanicResult::Err(v12)
End:
Return(v13)
//! > ==========================================================================
//! > Test custom unit destruct (Currently broken).
//! > test_runner_name
test_function_lowering(expect_diagnostics: false)
//! > function
fn foo(a: Option<()>) {
bar::<(), MyDestruct>(a.expect('Should be Some.'))
}
//! > function_name
foo
//! > module_code
#[inline]
fn bar<T, +Destruct<T>>(a: T) {}
pub impl MyDestruct of Destruct<()> {
#[inline(always)]
fn destruct(self: ()) nopanic {
my_fn()
}
}
extern fn my_fn() nopanic;
//! > semantic_diagnostics
//! > lowering_diagnostics
//! > lowering_flat
Parameters: v0: core::option::Option::<()>
blk0 (root):
Statements:
End:
Match(match_enum(v0) {
Option::Some(v1) => blk1,
Option::None(v2) => blk2,
})
blk1:
Statements:
() <- test::my_fn()
(v3: ()) <- struct_construct()
(v4: ((),)) <- struct_construct(v3)
(v5: core::panics::PanicResult::<((),)>) <- PanicResult::Ok(v4)
End:
Return(v5)
blk2:
Statements:
(v6: core::array::Array::<core::felt252>) <- core::array::array_new::<core::felt252>()
(v7: core::felt252) <- 433078840523992521883553638820701486
(v8: core::array::Array::<core::felt252>) <- core::array::array_append::<core::felt252>(v6, v7)
(v9: core::panics::Panic) <- struct_construct()
(v10: (core::panics::Panic, core::array::Array::<core::felt252>)) <- struct_construct(v9, v8)
(v11: core::panics::PanicResult::<((),)>) <- PanicResult::Err(v10)
End:
Return(v11)
//! > ==========================================================================
//! > Test inline of self calling destruct.
//! > test_runner_name
test_function_lowering(expect_diagnostics: true)
//! > function
fn foo() {
let _a = A {};
}
//! > function_name
foo
//! > module_code
struct A {}
impl ADestruct of Destruct<A> {
#[inline(always)]
fn destruct(self: A) nopanic {}
}
//! > semantic_diagnostics
//! > lowering_diagnostics
error: Call cycle of `nopanic` functions is not allowed.
--> lib.cairo:3:5
#[inline(always)]
^***************^
error: Cannot inline a function that might call itself.
--> lib.cairo:3:5
#[inline(always)]
^***************^
//! > lowering_flat
Parameters:
blk0 (root):
Statements:
(v0: test::A) <- struct_construct()
() <- test::ADestruct::destruct(v0)
End:
Return()