Skip to content

Commit

Permalink
add SierraAssertError enum (#688)
Browse files Browse the repository at this point in the history
* add SierraAssertError

* Update src/error.rs

Co-authored-by: Mario Rugiero <[email protected]>

* fix comments

---------

Co-authored-by: Mario Rugiero <[email protected]>
  • Loading branch information
tcoratger and Oppen authored Jun 18, 2024
1 parent 50d2c60 commit 392f531
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
13 changes: 11 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Various error types used thorough the crate.
use crate::metadata::gas::GasMetadataError;
use cairo_lang_sierra::extensions::modules::utils::Range;
use cairo_lang_sierra::{
edit_state::EditStateError, ids::ConcreteTypeId, program_registry::ProgramRegistryError,
};
Expand Down Expand Up @@ -43,8 +44,8 @@ pub enum Error {
#[error("missing metadata")]
MissingMetadata,

#[error("a cairo-native sierra related assert failed: {0}")]
SierraAssert(String),
#[error(transparent)]
SierraAssert(SierraAssertError),

#[error("a compiler related error happened: {0}")]
Error(String),
Expand Down Expand Up @@ -73,6 +74,14 @@ impl Error {
}
}

#[derive(Error, Debug)]
pub enum SierraAssertError {
#[error("casts always happen between numerical types")]
Cast,
#[error("range should always intersect, from {:?} to {:?}", ranges.0, ranges.1)]
Range { ranges: Box<(Range, Range)> },
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
62 changes: 31 additions & 31 deletions src/libfuncs/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::ops::Shr;
use super::LibfuncHelper;
use crate::{
block_ext::BlockExt,
error::{Error, Result},
error::{Error, Result, SierraAssertError},
metadata::{prime_modulo::PrimeModuloMeta, MetadataStorage},
types::TypeBuilder,
};
Expand Down Expand Up @@ -65,12 +65,12 @@ pub fn build_downcast<'ctx, 'this>(

let src_type = registry.get_type(&info.from_ty)?;
let dst_type = registry.get_type(&info.to_ty)?;
let src_width = src_type.integer_width().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})?;
let dst_width = dst_type.integer_width().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})?;
let src_width = src_type
.integer_width()
.ok_or_else(|| Error::SierraAssert(SierraAssertError::Cast))?;
let dst_width = dst_type
.integer_width()
.ok_or_else(|| Error::SierraAssert(SierraAssertError::Cast))?;

let src_ty = src_type.build(context, helper, registry, metadata, &info.from_ty)?;
let dst_ty = dst_type.build(context, helper, registry, metadata, &info.to_ty)?;
Expand All @@ -81,12 +81,12 @@ pub fn build_downcast<'ctx, 'this>(
location,
);

let src_is_signed = src_type.is_integer_signed().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})?;
let dst_is_signed = dst_type.is_integer_signed().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})?;
let src_is_signed = src_type
.is_integer_signed()
.ok_or_else(|| Error::SierraAssert(SierraAssertError::Cast))?;
let dst_is_signed = dst_type
.is_integer_signed()
.ok_or_else(|| Error::SierraAssert(SierraAssertError::Cast))?;
let any_is_signed = src_is_signed | dst_is_signed;
let src_is_felt = matches!(
src_type,
Expand Down Expand Up @@ -188,18 +188,18 @@ pub fn build_downcast<'ctx, 'this>(
(result, dst_ty)
};

let mut int_max_value: BigInt = info
let info_range = info
.to_range
.intersection(&info.from_range)
.ok_or_else(|| Error::SierraAssert("range should always interesct".to_string()))?
.upper
- 1;
.ok_or_else(|| {
Error::SierraAssert(SierraAssertError::Range {
ranges: Box::new((info.from_range.clone(), info.to_range.clone())),
})
})?;

let mut int_min_value = info
.to_range
.intersection(&info.from_range)
.ok_or_else(|| Error::SierraAssert("range should always interesct".to_string()))?
.lower;
let mut int_max_value: BigInt = info_range.upper - 1;

let mut int_min_value = info_range.lower;

if dst_is_felt {
let prime = &metadata
Expand Down Expand Up @@ -290,17 +290,17 @@ pub fn build_upcast<'ctx, 'this>(
location,
);

let src_width = src_ty.integer_width().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})?;
let dst_width = dst_ty.integer_width().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})?;
let src_width = src_ty
.integer_width()
.ok_or_else(|| Error::SierraAssert(SierraAssertError::Cast))?;
let dst_width = dst_ty
.integer_width()
.ok_or_else(|| Error::SierraAssert(SierraAssertError::Cast))?;
assert!(src_width <= dst_width);

let is_signed = src_ty.is_integer_signed().ok_or_else(|| {
Error::SierraAssert("casts always happen between numerical types".to_string())
})?;
let is_signed = src_ty
.is_integer_signed()
.ok_or_else(|| Error::SierraAssert(SierraAssertError::Cast))?;

let is_felt = matches!(dst_ty, CoreTypeConcrete::Felt252(_));

Expand Down

0 comments on commit 392f531

Please sign in to comment.