Skip to content

Commit

Permalink
Modify int_range_try_new to return an empty range on failure.
Browse files Browse the repository at this point in the history
commit-id:312d2d4b
  • Loading branch information
liorgold2 committed Aug 28, 2024
1 parent d28ae43 commit 1f7ec56
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 161 deletions.
6 changes: 3 additions & 3 deletions corelib/src/ops/range.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ mod internal {
pub extern type IntRange<T>;
pub extern fn int_range_try_new<T>(
x: T, y: T
) -> Option<IntRange<T>> implicits(core::RangeCheck) nopanic;
) -> Result<IntRange<T>, IntRange<T>> implicits(core::RangeCheck) nopanic;
pub extern fn int_range_pop_front<T>(range: IntRange<T>) -> OptionRev<(IntRange<T>, T)> nopanic;

impl IntRangeIteratorImpl<T, +Copy<T>, +Drop<T>> of Iterator<IntRange<T>> {
Expand Down Expand Up @@ -107,8 +107,8 @@ impl SierraRangeIntoIterator<

fn into_iter(self: Range<T>) -> Self::IntoIter {
match internal::int_range_try_new(self.start, self.end) {
Option::Some(range) => range,
Option::None => internal::int_range_try_new(self.end, self.end).unwrap(),
Result::Ok(range) => range,
Result::Err(range) => range,
}
}
}
2 changes: 1 addition & 1 deletion crates/cairo-lang-sierra-to-casm/src/invocations/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn build_try_new(
casm_builder,
[
("Fallthrough", &[&[range_check], &[start, end]], None),
("Failure", &[&[range_check]], Some(failure_handle)),
("Failure", &[&[range_check], &[end, end]], Some(failure_handle)),
],
CostValidationInfo {
builtin_infos: vec![BuiltinInfo {
Expand Down
13 changes: 10 additions & 3 deletions crates/cairo-lang-sierra/src/extensions/modules/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ define_libfunc_hierarchy! {
}, IntRangeConcreteLibfunc
}

/// Libfunc that constructs the range `[x, y)` if `x <= y` and fails otherwise.
/// Libfunc that constructs the range `[x, y)` if `x <= y`.
/// Otherwise, returns the empty range `[y, y)`.
#[derive(Default)]
pub struct IntRangeTryNewLibfunc {}
impl SignatureOnlyGenericLibfunc for IntRangeTryNewLibfunc {
Expand Down Expand Up @@ -110,15 +111,21 @@ impl SignatureOnlyGenericLibfunc for IntRangeTryNewLibfunc {
vars: vec![
OutputVarInfo::new_builtin(range_check_type.clone(), 0),
OutputVarInfo {
ty: range_ty,
ty: range_ty.clone(),
ref_info: OutputVarReferenceInfo::SimpleDerefs,
},
],
ap_change: SierraApChange::Known { new_vars_only: false },
},
// Failure.
BranchSignature {
vars: vec![OutputVarInfo::new_builtin(range_check_type, 0)],
vars: vec![
OutputVarInfo::new_builtin(range_check_type, 0),
OutputVarInfo {
ty: range_ty,
ref_info: OutputVarReferenceInfo::SimpleDerefs,
},
],
ap_change: SierraApChange::Known { new_vars_only: false },
},
],
Expand Down
Loading

0 comments on commit 1f7ec56

Please sign in to comment.