//! > Test expansion of panic_with.
//! > test_runner_name
test_expand_plugin(expect_diagnostics: false)
//! > cairo_code
#[panic_with('1', foo_improved)]
extern fn foo(a: felt252, ref b: other) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;
#[panic_with('2', bar_changed)]
extern fn bar() -> Result<felt252, Err> nopanic;
#[panic_with('3', non_extern_stuff)]
fn non_extern(_a: some_type) -> Option<(felt252, other)> nopanic {
(4, 56)
}
#[panic_with('4', generic_panic)]
extern fn generic<T>(t: T, v: felt252) -> Result<T, Err> nopanic;
#[panic_with('5', wrapped_possibly_panic)]
fn possibly_panic() -> Result<felt252, Err> {
Result::<felt252, Err>::Ok(1)
}
//! > expanded_cairo_code
#[panic_with('1', foo_improved)]
extern fn foo(a: felt252, ref b: other) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;
#[panic_with('2', bar_changed)]
extern fn bar() -> Result<felt252, Err> nopanic;
#[panic_with('3', non_extern_stuff)]
fn non_extern(_a: some_type) -> Option<(felt252, other)> nopanic {
(4, 56)
}
#[panic_with('4', generic_panic)]
extern fn generic<T>(t: T, v: felt252) -> Result<T, Err> nopanic;
#[panic_with('5', wrapped_possibly_panic)]
fn possibly_panic() -> Result<felt252, Err> {
Result::<felt252, Err>::Ok(1)
}
fn foo_improved(a: felt252, ref b: other) -> () {
match foo(a, ref b) {
Option::Some(v) => v,
Option::None(_) => core::panic_with_const_felt252::<'1'>(),
}
}
fn bar_changed() -> felt252 {
match bar() {
Result::Ok(v) => v,
Result::Err(_) => core::panic_with_const_felt252::<'2'>(),
}
}
fn non_extern_stuff(_a: some_type) -> (felt252, other) {
match non_extern(_a) {
Option::Some(v) => v,
Option::None(_) => core::panic_with_const_felt252::<'3'>(),
}
}
fn generic_panic<T>(t: T, v: felt252) -> T {
match generic(t, v) {
Result::Ok(v) => v,
Result::Err(_) => core::panic_with_const_felt252::<'4'>(),
}
}
fn wrapped_possibly_panic() -> felt252 {
match possibly_panic() {
Result::Ok(v) => v,
Result::Err(_) => core::panic_with_const_felt252::<'5'>(),
}
}
//! > expected_diagnostics
//! > ==========================================================================
//! > Test diagnostics of panic with.
//! > test_runner_name
test_expand_plugin(expect_diagnostics: true)
//! > cairo_code
#[panic_with(123, foo_bad_err_code)]
extern fn foo(a: felt252, b: other) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;
#[panic_with(missing_args)]
extern fn non_extern(_a: some_type) -> Option<(felt252, other)> nopanic;
#[panic_with(missing_args)]
extern fn bad_ret_type(_a: some_type) -> felt252 nopanic;
#[panic_with('2', bar_changed)]
#[panic_with('3', bar_changed)]
extern fn bar() -> Result<felt252, Err> nopanic;
//! > expanded_cairo_code
#[panic_with(123, foo_bad_err_code)]
extern fn foo(a: felt252, b: other) -> Option<()> implicits(RangeCheck, GasBuiltin) nopanic;
#[panic_with(missing_args)]
extern fn non_extern(_a: some_type) -> Option<(felt252, other)> nopanic;
#[panic_with(missing_args)]
extern fn bad_ret_type(_a: some_type) -> felt252 nopanic;
#[panic_with('2', bar_changed)]
#[panic_with('3', bar_changed)]
extern fn bar() -> Result<felt252, Err> nopanic;
//! > expected_diagnostics
error: Failed to extract panic data attribute
--> test_src/lib.cairo:1:1
#[panic_with(123, foo_bad_err_code)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: Failed to extract panic data attribute
--> test_src/lib.cairo:4:1
#[panic_with(missing_args)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: Currently only wrapping functions returning an Option<T> or Result<T, E>
--> test_src/lib.cairo:8:39
extern fn bad_ret_type(_a: some_type) -> felt252 nopanic;
^^^^^^^^^^
error: `#[panic_with]` cannot be applied multiple times to the same item.
--> test_src/lib.cairo:11:1
#[panic_with('3', bar_changed)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^