Skip to content

Commit

Permalink
Remove missing panics (#942)
Browse files Browse the repository at this point in the history
* remove missing panics

* reviews
  • Loading branch information
FrancoGiachetta authored Nov 27, 2024
1 parent 76e8396 commit 5918794
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 26 deletions.
19 changes: 14 additions & 5 deletions src/libfuncs/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::LibfuncHelper;
use crate::{
error::{panic::ToNativeAssertError, Error, Result},
metadata::{enum_snapshot_variants::EnumSnapshotVariantsMeta, MetadataStorage},
native_panic,
native_assert, native_panic,
types::TypeBuilder,
utils::BlockExt,
};
Expand Down Expand Up @@ -210,8 +210,11 @@ pub fn build_from_bounded_int<'ctx, 'this>(
)?
.try_into()?;
let enum_type = registry.get_type(&info.branch_signatures()[0].vars[0].ty)?;
// we assume its never memory allocated since its always a enum with only a tag
assert!(!enum_type.is_memory_allocated(registry)?);
// we assume its never memory allocated since its always an enum with only a tag
native_assert!(
!enum_type.is_memory_allocated(registry)?,
"an enum with only a tag should not be allocated in memory"
);

let enum_ty = enum_type.build(
context,
Expand Down Expand Up @@ -385,7 +388,10 @@ pub fn build_match<'ctx, 'this>(
if variant_ids.len() == 1 {
entry.arg(0)?
} else {
assert!(registry.get_type(&variant_ids[i])?.is_zst(registry)?);
native_assert!(
registry.get_type(&variant_ids[i])?.is_zst(registry)?,
"should be zero sized"
);
block
.append_operation(llvm::undef(payload_ty, location))
.result(0)?
Expand Down Expand Up @@ -527,7 +533,10 @@ pub fn build_snapshot_match<'ctx, 'this>(
if variant_ids.len() == 1 {
entry.arg(0)?
} else {
assert!(registry.get_type(&variant_ids[i])?.is_zst(registry)?);
native_assert!(
registry.get_type(&variant_ids[i])?.is_zst(registry)?,
"should be zero sized"
);
block.append_op_result(llvm::undef(payload_ty, location))?
}
}
Expand Down
16 changes: 8 additions & 8 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl TypeBuilder for CoreTypeConcrete {
metadata,
WithSelf::new(self_ty, info),
),
Self::Const(_) => todo!(),
Self::Const(_) => native_panic!("todo: Const type to MLIR type"),
Self::EcOp(info) => self::ec_op::build(
context,
module,
Expand Down Expand Up @@ -346,7 +346,7 @@ impl TypeBuilder for CoreTypeConcrete {
metadata,
WithSelf::new(self_ty, info),
),
Self::Span(_) => todo!("implement span type"),
Self::Span(_) => native_panic!("todo: Span type to MLIR type"),
Self::SquashedFelt252Dict(info) => self::squashed_felt252_dict::build(
context,
module,
Expand Down Expand Up @@ -535,10 +535,10 @@ impl TypeBuilder for CoreTypeConcrete {

value
},
CoreTypeConcrete::Const(_) => todo!(),
CoreTypeConcrete::Span(_) => todo!(),
CoreTypeConcrete::Const(_) => native_panic!("todo: check Const is complex"),
CoreTypeConcrete::Span(_) => native_panic!("todo: check Span is complex"),
CoreTypeConcrete::StarkNet(StarkNetTypeConcrete::Secp256Point(_))
| CoreTypeConcrete::StarkNet(StarkNetTypeConcrete::Sha256StateHandle(_)) => todo!(),
| CoreTypeConcrete::StarkNet(StarkNetTypeConcrete::Sha256StateHandle(_)) => native_panic!("todo: check Sha256StateHandle is complex"),
CoreTypeConcrete::Coupon(_) => false,

CoreTypeConcrete::Circuit(info) => circuit::is_complex(info),
Expand Down Expand Up @@ -621,7 +621,7 @@ impl TypeBuilder for CoreTypeConcrete {
let type_info = registry.get_type(&info.inner_ty)?;
type_info.is_zst(registry)?
}
CoreTypeConcrete::Span(_) => todo!(),
CoreTypeConcrete::Span(_) => native_panic!("todo: check Span is zero sized"),
CoreTypeConcrete::Circuit(info) => circuit::is_zst(info),

CoreTypeConcrete::IntRange(info) => {
Expand Down Expand Up @@ -701,7 +701,7 @@ impl TypeBuilder for CoreTypeConcrete {
CoreTypeConcrete::SquashedFelt252Dict(_) => Layout::new::<*mut std::ffi::c_void>(), // ptr
CoreTypeConcrete::Pedersen(_) => Layout::new::<u64>(),
CoreTypeConcrete::Poseidon(_) => Layout::new::<u64>(),
CoreTypeConcrete::Span(_) => todo!(),
CoreTypeConcrete::Span(_) => native_panic!("todo: create layout for Span"),
CoreTypeConcrete::StarkNet(info) => match info {
StarkNetTypeConcrete::ClassHash(_) => get_integer_layout(252),
StarkNetTypeConcrete::ContractAddress(_) => get_integer_layout(252),
Expand Down Expand Up @@ -736,7 +736,7 @@ impl TypeBuilder for CoreTypeConcrete {

CoreTypeConcrete::IntRange(info) => {
let inner = registry.get_type(&info.ty)?.layout(registry)?;
inner.extend(inner).unwrap().0
inner.extend(inner)?.0
}
}
.pad_to_align())
Expand Down
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ pub fn felt252_short_str(value: &str) -> Felt {
.filter_map(|c| c.is_ascii().then_some(c as u8))
.collect();

assert!(values.len() < 32);
assert!(values.len() < 32, "A felt can't longer than 32 bytes");
Felt::from_bytes_be_slice(&values)
}

Expand Down
33 changes: 21 additions & 12 deletions src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl Value {
ptr
}

Self::Bytes31(_) => todo!(),
Self::Bytes31(_) => native_panic!("todo: allocate type Bytes31"),
Self::Array(data) => {
if let CoreTypeConcrete::Array(info) = Self::resolve_type(ty, registry)? {
let elem_ty = registry.get_type(&info.ty)?;
Expand Down Expand Up @@ -513,8 +513,8 @@ impl Value {

ptr
}
Self::Secp256K1Point { .. } => todo!(),
Self::Secp256R1Point { .. } => todo!(),
Self::Secp256K1Point { .. } => native_panic!("todo: allocate type Secp256K1Point"),
Self::Secp256R1Point { .. } => native_panic!("todo: allocate type Secp256R1Point"),
Self::Null => {
native_panic!(
"unimplemented: null is meant as return value for nullable for now"
Expand Down Expand Up @@ -654,7 +654,9 @@ impl Value {
CoreTypeConcrete::Uint32(_) => Self::Uint32(*ptr.cast::<u32>().as_ref()),
CoreTypeConcrete::Uint64(_) => Self::Uint64(*ptr.cast::<u64>().as_ref()),
CoreTypeConcrete::Uint128(_) => Self::Uint128(*ptr.cast::<u128>().as_ref()),
CoreTypeConcrete::Uint128MulGuarantee(_) => todo!(),
CoreTypeConcrete::Uint128MulGuarantee(_) => {
native_panic!("todo: implement uint128mulguarantee from_ptr")
}
CoreTypeConcrete::Sint8(_) => Self::Sint8(*ptr.cast::<i8>().as_ref()),
CoreTypeConcrete::Sint16(_) => Self::Sint16(*ptr.cast::<i16>().as_ref()),
CoreTypeConcrete::Sint32(_) => Self::Sint32(*ptr.cast::<i32>().as_ref()),
Expand All @@ -676,7 +678,7 @@ impl Value {
}
}
CoreTypeConcrete::Uninitialized(_) => {
todo!("implement uninit from_ptr or ignore the return value")
native_panic!("todo: implement uninit from_ptr or ignore the return value")
}
CoreTypeConcrete::Enum(info) => {
let tag_layout = crate::utils::get_integer_layout(match info.variants.len() {
Expand Down Expand Up @@ -825,16 +827,18 @@ impl Value {
Self::Secp256R1Point(*data)
}
},
StarkNetTypeConcrete::Sha256StateHandle(_) => todo!(),
StarkNetTypeConcrete::Sha256StateHandle(_) => {
native_panic!("todo: implement Sha256StateHandle from_ptr")
}
},
CoreTypeConcrete::Span(_) => todo!("implement span from_ptr"),
CoreTypeConcrete::Span(_) => native_panic!("todo: implement span from_ptr"),
CoreTypeConcrete::Snapshot(info) => Self::from_ptr(ptr, &info.ty, registry)?,
CoreTypeConcrete::Bytes31(_) => {
let data = *ptr.cast::<[u8; 31]>().as_ref();
Self::Bytes31(data)
}

CoreTypeConcrete::Const(_) => todo!(),
CoreTypeConcrete::Const(_) => native_panic!("implement const from_ptr"),
CoreTypeConcrete::BoundedInt(info) => {
let mut data = BigInt::from_biguint(
Sign::Plus,
Expand All @@ -854,20 +858,25 @@ impl Value {
}
CoreTypeConcrete::Coupon(_)
| CoreTypeConcrete::Circuit(_)
| CoreTypeConcrete::RangeCheck96(_) => todo!(),
| CoreTypeConcrete::RangeCheck96(_) => native_panic!("implement from_ptr"),
CoreTypeConcrete::IntRange(info) => {
let member = registry.get_type(&info.ty)?;
let member_layout = member.layout(registry)?;

let x =
Self::from_ptr(NonNull::new(ptr.as_ptr()).unwrap(), &info.ty, registry)?;
let x = Self::from_ptr(
NonNull::new(ptr.as_ptr()).to_native_assert_error(
"tried to make a non-null ptr out of a null one",
)?,
&info.ty,
registry,
)?;

let y = Self::from_ptr(
NonNull::new(
ptr.as_ptr()
.byte_add(member_layout.extend(member_layout)?.1),
)
.unwrap(),
.to_native_assert_error("tried to make a non-null ptr out of a null one")?,
&info.ty,
registry,
)?;
Expand Down

0 comments on commit 5918794

Please sign in to comment.