Skip to content

Commit ad1a0c4

Browse files
JulianGCalderongabrielbosio
authored andcommitted
Remove panics from libfuncs directory (#1116)
* Remove panics in starknet libfuncs * Remove panics in poseidon libfuncs * Remove panics in pedersen libfuncs * Remove panic in int libfuncs * Remove gas libfuncs panics * Remove felt252_dict panics * Remove panic un enum libfuncs * Remove panic in debug libfuncs * Remove panics in bool libfuncs * Remove panics in array libfuncs
1 parent 4ba03af commit ad1a0c4

File tree

10 files changed

+26
-31
lines changed

10 files changed

+26
-31
lines changed

src/libfuncs/array.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ pub fn build_get<'ctx, 'this>(
10611061
// Drop the input array.
10621062
metadata
10631063
.get::<DropOverridesMeta>()
1064-
.unwrap()
1064+
.to_native_assert_error("drop overrides metadata should be available")?
10651065
.invoke_override(
10661066
context,
10671067
valid_block,

src/libfuncs/bool.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::LibfuncHelper;
44
use crate::{
5-
error::Result,
5+
error::{panic::ToNativeAssertError, Result},
66
metadata::MetadataStorage,
77
types::TypeBuilder,
88
utils::{BlockExt, ProgramRegistryExt},
@@ -94,7 +94,7 @@ fn build_bool_binary<'ctx, 'this>(
9494
let enum_ty = registry.get_type(&info.param_signatures()[0].ty)?;
9595
let tag_bits = enum_ty
9696
.variants()
97-
.expect("bool is a enum and has variants")
97+
.to_native_assert_error("bool is a enum and has variants")?
9898
.len()
9999
.next_power_of_two()
100100
.trailing_zeros();
@@ -143,7 +143,7 @@ pub fn build_bool_not<'ctx, 'this>(
143143
let enum_ty = registry.get_type(&info.param_signatures()[0].ty)?;
144144
let tag_bits = enum_ty
145145
.variants()
146-
.expect("bool is a enum and has variants")
146+
.to_native_assert_error("bool is a enum and has variants")?
147147
.len()
148148
.next_power_of_two()
149149
.trailing_zeros();
@@ -192,7 +192,7 @@ pub fn build_bool_to_felt252<'ctx, 'this>(
192192

193193
let tag_bits = enum_ty
194194
.variants()
195-
.expect("bool is a enum and has variants")
195+
.to_native_assert_error("bool is a enum and has variants")?
196196
.len()
197197
.next_power_of_two()
198198
.trailing_zeros();

src/libfuncs/debug.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
use super::LibfuncHelper;
1313
use crate::{
14-
error::{Error, Result},
14+
error::{panic::ToNativeAssertError, Error, Result},
1515
metadata::{
1616
drop_overrides::DropOverridesMeta, runtime_bindings::RuntimeBindingsMeta, MetadataStorage,
1717
},
@@ -82,7 +82,7 @@ pub fn build_print<'ctx>(
8282

8383
let runtime_bindings = metadata
8484
.get_mut::<RuntimeBindingsMeta>()
85-
.expect("Runtime library not available.");
85+
.to_native_assert_error("runtime library should be available")?;
8686

8787
let values_len = entry.append_op_result(arith::subi(values_end, values_start, location))?;
8888

src/libfuncs/enum.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,7 @@ pub fn build_enum_value<'ctx, 'this>(
145145
let tag_val = entry
146146
.append_operation(arith::constant(
147147
context,
148-
IntegerAttribute::new(
149-
tag_ty,
150-
variant_index
151-
.try_into()
152-
.expect("couldnt convert index to i64"),
153-
)
154-
.into(),
148+
IntegerAttribute::new(tag_ty, variant_index.try_into()?).into(),
155149
location,
156150
))
157151
.result(0)?
@@ -425,7 +419,7 @@ pub fn build_snapshot_match<'ctx, 'this>(
425419
.get::<EnumSnapshotVariantsMeta>()
426420
.ok_or(Error::MissingMetadata)?
427421
.get_variants(&info.param_signatures()[0].ty)
428-
.expect("enum should always have variants")
422+
.to_native_assert_error("enum should always have variants")?
429423
.clone();
430424
match variant_ids.len() {
431425
0 => {

src/libfuncs/felt252_dict.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::LibfuncHelper;
44
use crate::{
5-
error::Result,
5+
error::{panic::ToNativeAssertError, Result},
66
metadata::{
77
felt252_dict::Felt252DictOverrides, runtime_bindings::RuntimeBindingsMeta, MetadataStorage,
88
},
@@ -112,7 +112,7 @@ pub fn build_new<'ctx, 'this>(
112112

113113
let runtime_bindings = metadata
114114
.get_mut::<RuntimeBindingsMeta>()
115-
.expect("Runtime library not available.");
115+
.to_native_assert_error("runtime library should be available")?;
116116
let dict_ptr = runtime_bindings.dict_new(
117117
context,
118118
helper,
@@ -143,7 +143,7 @@ pub fn build_squash<'ctx, 'this>(
143143

144144
let runtime_bindings = metadata
145145
.get_mut::<RuntimeBindingsMeta>()
146-
.expect("Runtime library not available.");
146+
.to_native_assert_error("runtime library should be available")?;
147147

148148
let gas_refund = runtime_bindings
149149
.dict_gas_refund(context, helper, entry, dict_ptr, location)?

src/libfuncs/gas.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use super::LibfuncHelper;
44
use crate::{
5-
error::{Error, Result},
5+
error::{panic::ToNativeAssertError, Error, Result},
66
metadata::{gas::GasCost, runtime_bindings::RuntimeBindingsMeta, MetadataStorage},
77
native_panic,
88
utils::{BlockExt, GepIndex},
@@ -91,7 +91,7 @@ pub fn build_withdraw_gas<'ctx, 'this>(
9191

9292
let gas_cost = metadata
9393
.get::<GasCost>()
94-
.expect("withdraw_gas should always have a gas cost")
94+
.to_native_assert_error("withdraw_gas should always have a gas cost")?
9595
.clone();
9696

9797
let u64_type: melior::ir::Type = IntegerType::new(context, 64).into();
@@ -172,7 +172,7 @@ pub fn build_redeposit_gas<'ctx, 'this>(
172172

173173
let gas_cost = metadata
174174
.get::<GasCost>()
175-
.expect("redeposit_gas should always have a gas cost")
175+
.to_native_assert_error("redeposit_gas should always have a gas cost")?
176176
.clone();
177177

178178
let u64_type: melior::ir::Type = IntegerType::new(context, 64).into();
@@ -247,7 +247,7 @@ pub fn build_builtin_withdraw_gas<'ctx, 'this>(
247247

248248
let gas_cost = metadata
249249
.get::<GasCost>()
250-
.expect("builtin_withdraw_gas should always have a gas cost")
250+
.to_native_assert_error("builtin_withdraw_gas should always have a gas cost")?
251251
.clone();
252252

253253
let u64_type: melior::ir::Type = IntegerType::new(context, 64).into();

src/libfuncs/int.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,7 @@ fn build_wide_mul<'ctx, 'this>(
880880

881881
let ext_fn = if registry
882882
.get_type(&info.signature.param_signatures[0].ty)?
883-
.integer_range(registry)
884-
.unwrap()
883+
.integer_range(registry)?
885884
.lower
886885
.is_zero()
887886
{

src/libfuncs/pedersen.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use super::LibfuncHelper;
55
use crate::{
6-
error::Result,
6+
error::{panic::ToNativeAssertError, Result},
77
metadata::{runtime_bindings::RuntimeBindingsMeta, MetadataStorage},
88
utils::{get_integer_layout, BlockExt, ProgramRegistryExt},
99
};
@@ -49,7 +49,7 @@ pub fn build_pedersen<'ctx>(
4949
) -> Result<()> {
5050
metadata
5151
.get_mut::<RuntimeBindingsMeta>()
52-
.expect("Runtime library not available.");
52+
.to_native_assert_error("runtime library should be available")?;
5353

5454
let pedersen_builtin =
5555
super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?;
@@ -83,7 +83,7 @@ pub fn build_pedersen<'ctx>(
8383

8484
let runtime_bindings = metadata
8585
.get_mut::<RuntimeBindingsMeta>()
86-
.expect("Runtime library not available.");
86+
.to_native_assert_error("runtime library should be available")?;
8787

8888
runtime_bindings
8989
.libfunc_pedersen(context, helper, entry, dst_ptr, lhs_ptr, rhs_ptr, location)?;

src/libfuncs/poseidon.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use super::LibfuncHelper;
55
use crate::{
6-
error::Result,
6+
error::{panic::ToNativeAssertError, Result},
77
metadata::{runtime_bindings::RuntimeBindingsMeta, MetadataStorage},
88
utils::{get_integer_layout, BlockExt, ProgramRegistryExt},
99
};
@@ -50,7 +50,7 @@ pub fn build_hades_permutation<'ctx>(
5050
) -> Result<()> {
5151
metadata
5252
.get_mut::<RuntimeBindingsMeta>()
53-
.expect("Runtime library not available.");
53+
.to_native_assert_error("runtime library should be available")?;
5454

5555
let poseidon_builtin =
5656
super::increment_builtin_counter(context, entry, location, entry.arg(0)?)?;
@@ -91,7 +91,7 @@ pub fn build_hades_permutation<'ctx>(
9191

9292
let runtime_bindings = metadata
9393
.get_mut::<RuntimeBindingsMeta>()
94-
.expect("Runtime library not available.");
94+
.to_native_assert_error("runtime library should be available")?;
9595

9696
runtime_bindings
9797
.libfunc_hades_permutation(context, helper, entry, op0_ptr, op1_ptr, op2_ptr, location)?;

src/libfuncs/starknet.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,9 @@ pub fn build<'ctx, 'this>(
145145
}
146146
#[cfg(not(feature = "with-cheatcode"))]
147147
StarkNetConcreteLibfunc::Testing(TestingConcreteLibfunc::Cheatcode(_)) => {
148-
unimplemented!("feature 'with-cheatcode' is required to compile with cheatcode syscall")
148+
crate::native_panic!(
149+
"feature 'with-cheatcode' is required to compile with cheatcode syscall"
150+
)
149151
}
150152
}
151153
}

0 commit comments

Comments
 (0)