Skip to content

Commit c6d03c5

Browse files
orizidean-starkware
authored andcommitted
Fixed generated functions in trait-fns. (#7081)
feat(corelib): Iterator::enumerate (#7048) fix: Fix handling of --skip-first argument Update release_crates.sh (#7087) chore: orthographic correction in file if_else (#7088) prevents closure parameters from being declared as refrences (#7078) Refactored bounded_int_trim. (#7062) Added const for starknet types. (#6961) feat(corelib): Iterator::fold (#7084) feat(corelib): Iterator::advance_by (#7059) fix(corelib): Add the #[test] annotation to enumerate test (#7098) feat(corelib): storage vectors iterators (#6941) Extract ModuleHelper from const folding. (#7099) Added support for basic `Into`s in consts. (#7100) Removed taking value for `validate_literal`. (#7101) added closure params to semantic defs in lowering (#7085) Added support for `downcast` in constant context. (#7102) fix(corelib): Add the #[test] annotation to enumerate test (#7098)
1 parent 2d0c700 commit c6d03c5

File tree

40 files changed

+1233
-302
lines changed

40 files changed

+1233
-302
lines changed

Cargo.lock

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

corelib/src/bytes_31.cairo

+5-5
Original file line numberDiff line numberDiff line change
@@ -88,31 +88,31 @@ pub(crate) impl Felt252TryIntoBytes31 of TryInto<felt252, bytes31> {
8888
impl Bytes31Serde = crate::serde::into_felt252_based::SerdeImpl<bytes31>;
8989

9090
pub(crate) impl U8IntoBytes31 of Into<u8, bytes31> {
91-
fn into(self: u8) -> bytes31 {
91+
const fn into(self: u8) -> bytes31 {
9292
crate::integer::upcast(self)
9393
}
9494
}
9595

9696
impl U16IntoBytes31 of Into<u16, bytes31> {
97-
fn into(self: u16) -> bytes31 {
97+
const fn into(self: u16) -> bytes31 {
9898
crate::integer::upcast(self)
9999
}
100100
}
101101

102102
impl U32IntoBytes31 of Into<u32, bytes31> {
103-
fn into(self: u32) -> bytes31 {
103+
const fn into(self: u32) -> bytes31 {
104104
crate::integer::upcast(self)
105105
}
106106
}
107107

108108
impl U64IntoBytes31 of Into<u64, bytes31> {
109-
fn into(self: u64) -> bytes31 {
109+
const fn into(self: u64) -> bytes31 {
110110
crate::integer::upcast(self)
111111
}
112112
}
113113

114114
pub(crate) impl U128IntoBytes31 of Into<u128, bytes31> {
115-
fn into(self: u128) -> bytes31 {
115+
const fn into(self: u128) -> bytes31 {
116116
crate::integer::upcast(self)
117117
}
118118
}

corelib/src/integer.cairo

+8-8
Original file line numberDiff line numberDiff line change
@@ -1426,11 +1426,11 @@ pub(crate) impl I128IntoFelt252 of Into<i128, felt252> {
14261426

14271427
// TODO(lior): Restrict the function (using traits) in the high-level compiler so that wrong types
14281428
// will not lead to Sierra errors.
1429-
pub(crate) extern fn upcast<FromType, ToType>(x: FromType) -> ToType nopanic;
1429+
pub(crate) extern const fn upcast<FromType, ToType>(x: FromType) -> ToType nopanic;
14301430

14311431
// TODO(lior): Restrict the function (using traits) in the high-level compiler so that wrong types
14321432
// will not lead to Sierra errors.
1433-
pub(crate) extern fn downcast<FromType, ToType>(
1433+
pub(crate) extern const fn downcast<FromType, ToType>(
14341434
x: FromType,
14351435
) -> Option<ToType> implicits(RangeCheck) nopanic;
14361436

@@ -1599,21 +1599,21 @@ impl U128Felt252DictValue of Felt252DictValue<u128> {
15991599
}
16001600

16011601
impl UpcastableInto<From, To, +Upcastable<From, To>> of Into<From, To> {
1602-
fn into(self: From) -> To {
1602+
const fn into(self: From) -> To {
16031603
upcast(self)
16041604
}
16051605
}
16061606

16071607
impl DowncastableIntTryInto<
16081608
From, To, +DowncastableInt<From>, +DowncastableInt<To>, -Into<From, To>,
16091609
> of TryInto<From, To> {
1610-
fn try_into(self: From) -> Option<To> {
1610+
const fn try_into(self: From) -> Option<To> {
16111611
downcast(self)
16121612
}
16131613
}
16141614

16151615
impl U8IntoU256 of Into<u8, u256> {
1616-
fn into(self: u8) -> u256 {
1616+
const fn into(self: u8) -> u256 {
16171617
u256 { low: upcast(self), high: 0_u128 }
16181618
}
16191619
}
@@ -1631,7 +1631,7 @@ impl U256TryIntoU8 of TryInto<u256, u8> {
16311631
}
16321632

16331633
impl U16IntoU256 of Into<u16, u256> {
1634-
fn into(self: u16) -> u256 {
1634+
const fn into(self: u16) -> u256 {
16351635
u256 { low: upcast(self), high: 0_u128 }
16361636
}
16371637
}
@@ -1649,7 +1649,7 @@ impl U256TryIntoU16 of TryInto<u256, u16> {
16491649
}
16501650

16511651
impl U32IntoU256 of Into<u32, u256> {
1652-
fn into(self: u32) -> u256 {
1652+
const fn into(self: u32) -> u256 {
16531653
u256 { low: upcast(self), high: 0_u128 }
16541654
}
16551655
}
@@ -1667,7 +1667,7 @@ impl U256TryIntoU32 of TryInto<u256, u32> {
16671667
}
16681668

16691669
impl U64IntoU256 of Into<u64, u256> {
1670-
fn into(self: u64) -> u256 {
1670+
const fn into(self: u64) -> u256 {
16711671
u256 { low: upcast(self), high: 0_u128 }
16721672
}
16731673
}

corelib/src/internal/bounded_int.cairo

+37-44
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ impl NumericLiteralBoundedInt<
1111
impl BoundedIntIntoFelt252<
1212
const MIN: felt252, const MAX: felt252,
1313
> of Into<BoundedInt<MIN, MAX>, felt252> {
14-
fn into(self: BoundedInt<MIN, MAX>) -> felt252 {
14+
const fn into(self: BoundedInt<MIN, MAX>) -> felt252 {
1515
upcast(self)
1616
}
1717
}
1818

1919
impl Felt252TryIntoBoundedInt<
2020
const MIN: felt252, const MAX: felt252,
2121
> of TryInto<felt252, BoundedInt<MIN, MAX>> {
22-
fn try_into(self: felt252) -> Option<BoundedInt<MIN, MAX>> {
22+
const fn try_into(self: felt252) -> Option<BoundedInt<MIN, MAX>> {
2323
// Using `downcast` is allowed, since `BoundedInt` itself is not `pub`, and only has a few
2424
// specific `pub` instances, such as `u96`, `ConstZero` and `ConstOne`.
2525
downcast(self)
@@ -113,56 +113,49 @@ extern fn bounded_int_constrain<T, const BOUNDARY: felt252, impl H: ConstrainHel
113113
value: T,
114114
) -> Result<H::LowT, H::HighT> implicits(RangeCheck) nopanic;
115115

116-
/// A helper trait for trimming a `BoundedInt` instance.
117-
pub trait TrimHelper<T, const TRIMMED_VALUE: felt252> {
116+
/// A helper trait for trimming a `BoundedInt` instance min value.
117+
pub trait TrimMinHelper<T> {
118+
type Target;
119+
}
120+
/// A helper trait for trimming a `BoundedInt` instance max value.
121+
pub trait TrimMaxHelper<T> {
118122
type Target;
119123
}
120124
mod trim_impl {
121-
pub impl Impl<
122-
T, const TRIMMED_VALUE: felt252, const MIN: felt252, const MAX: felt252,
123-
> of super::TrimHelper<T, TRIMMED_VALUE> {
125+
pub impl Min<T, const MIN: felt252, const MAX: felt252> of super::TrimMinHelper<T> {
126+
type Target = super::BoundedInt<MIN, MAX>;
127+
}
128+
pub impl Max<T, const MIN: felt252, const MAX: felt252> of super::TrimMaxHelper<T> {
124129
type Target = super::BoundedInt<MIN, MAX>;
125130
}
126131
}
127-
impl U8TrimBelow = trim_impl::Impl<u8, 0, 1, 0xff>;
128-
impl U8TrimAbove = trim_impl::Impl<u8, 0xff, 0, 0xfe>;
129-
impl I8TrimBelow = trim_impl::Impl<i8, -0x80, -0x7f, 0x7f>;
130-
impl I8TrimAbove = trim_impl::Impl<i8, 0x7f, -0x80, 0x7e>;
131-
impl U16TrimBelow = trim_impl::Impl<u16, 0, 1, 0xffff>;
132-
impl U16TrimAbove = trim_impl::Impl<u16, 0xffff, 0, 0xfffe>;
133-
impl I16TrimBelow = trim_impl::Impl<i16, -0x8000, -0x7fff, 0x7fff>;
134-
impl I16TrimAbove = trim_impl::Impl<i16, 0x7fff, -0x8000, 0x7ffe>;
135-
impl U32TrimBelow = trim_impl::Impl<u32, 0, 1, 0xffffffff>;
136-
impl U32TrimAbove = trim_impl::Impl<u32, 0xffffffff, 0, 0xfffffffe>;
137-
impl I32TrimBelow = trim_impl::Impl<i32, -0x80000000, -0x7fffffff, 0x7fffffff>;
138-
impl I32TrimAbove = trim_impl::Impl<i32, 0x7fffffff, -0x80000000, 0x7ffffffe>;
139-
impl U64TrimBelow = trim_impl::Impl<u64, 0, 1, 0xffffffffffffffff>;
140-
impl U64TrimAbove = trim_impl::Impl<u64, 0xffffffffffffffff, 0, 0xfffffffffffffffe>;
141-
impl I64TrimBelow =
142-
trim_impl::Impl<i64, -0x8000000000000000, -0x7fffffffffffffff, 0x7fffffffffffffff>;
143-
impl I64TrimAbove =
144-
trim_impl::Impl<i64, 0x7fffffffffffffff, -0x8000000000000000, 0x7ffffffffffffffe>;
145-
impl U128TrimBelow = trim_impl::Impl<u128, 0, 1, 0xffffffffffffffffffffffffffffffff>;
146-
impl U128TrimAbove =
147-
trim_impl::Impl<
148-
u128, 0xffffffffffffffffffffffffffffffff, 0, 0xfffffffffffffffffffffffffffffffe,
149-
>;
132+
impl U8TrimBelow = trim_impl::Min<u8, 1, 0xff>;
133+
impl U8TrimAbove = trim_impl::Max<u8, 0, 0xfe>;
134+
impl I8TrimBelow = trim_impl::Min<i8, -0x7f, 0x7f>;
135+
impl I8TrimAbove = trim_impl::Max<i8, -0x80, 0x7e>;
136+
impl U16TrimBelow = trim_impl::Min<u16, 1, 0xffff>;
137+
impl U16TrimAbove = trim_impl::Max<u16, 0, 0xfffe>;
138+
impl I16TrimBelow = trim_impl::Min<i16, -0x7fff, 0x7fff>;
139+
impl I16TrimAbove = trim_impl::Max<i16, -0x8000, 0x7ffe>;
140+
impl U32TrimBelow = trim_impl::Min<u32, 1, 0xffffffff>;
141+
impl U32TrimAbove = trim_impl::Max<u32, 0, 0xfffffffe>;
142+
impl I32TrimBelow = trim_impl::Min<i32, -0x7fffffff, 0x7fffffff>;
143+
impl I32TrimAbove = trim_impl::Max<i32, -0x80000000, 0x7ffffffe>;
144+
impl U64TrimBelow = trim_impl::Min<u64, 1, 0xffffffffffffffff>;
145+
impl U64TrimAbove = trim_impl::Max<u64, 0, 0xfffffffffffffffe>;
146+
impl I64TrimBelow = trim_impl::Min<i64, -0x7fffffffffffffff, 0x7fffffffffffffff>;
147+
impl I64TrimAbove = trim_impl::Max<i64, -0x8000000000000000, 0x7ffffffffffffffe>;
148+
impl U128TrimBelow = trim_impl::Min<u128, 1, 0xffffffffffffffffffffffffffffffff>;
149+
impl U128TrimAbove = trim_impl::Max<u128, 0, 0xfffffffffffffffffffffffffffffffe>;
150150
impl I128TrimBelow =
151-
trim_impl::Impl<
152-
i128,
153-
-0x80000000000000000000000000000000,
154-
-0x7fffffffffffffffffffffffffffffff,
155-
0x7fffffffffffffffffffffffffffffff,
156-
>;
151+
trim_impl::Min<i128, -0x7fffffffffffffffffffffffffffffff, 0x7fffffffffffffffffffffffffffffff>;
157152
impl I128TrimAbove =
158-
trim_impl::Impl<
159-
i128,
160-
0x7fffffffffffffffffffffffffffffff,
161-
-0x80000000000000000000000000000000,
162-
0x7ffffffffffffffffffffffffffffffe,
163-
>;
153+
trim_impl::Max<i128, -0x80000000000000000000000000000000, 0x7ffffffffffffffffffffffffffffffe>;
164154

165-
extern fn bounded_int_trim<T, const TRIMMED_VALUE: felt252, impl H: TrimHelper<T, TRIMMED_VALUE>>(
155+
extern fn bounded_int_trim_min<T, impl H: TrimMinHelper<T>>(
156+
value: T,
157+
) -> core::internal::OptionRev<H::Target> nopanic;
158+
extern fn bounded_int_trim_max<T, impl H: TrimMaxHelper<T>>(
166159
value: T,
167160
) -> core::internal::OptionRev<H::Target> nopanic;
168161

@@ -272,5 +265,5 @@ impl MulMinusOneNegateHelper<T, impl H: MulHelper<T, MinusOne>> of NegateHelper<
272265
pub use {
273266
bounded_int_add as add, bounded_int_constrain as constrain, bounded_int_div_rem as div_rem,
274267
bounded_int_is_zero as is_zero, bounded_int_mul as mul, bounded_int_sub as sub,
275-
bounded_int_trim as trim,
268+
bounded_int_trim_max as trim_max, bounded_int_trim_min as trim_min,
276269
};

corelib/src/iter/adapters.cairo

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ mod map;
22
pub use map::Map;
33
#[allow(unused_imports)]
44
pub(crate) use map::mapped_iterator;
5+
6+
mod enumerate;
7+
pub use enumerate::Enumerate;
8+
#[allow(unused_imports)]
9+
pub(crate) use enumerate::enumerated_iterator;
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/// An iterator that yields the current count and the element during iteration.
2+
///
3+
/// This `struct` is created by the [`enumerate`] method on [`Iterator`]. See its
4+
/// documentation for more.
5+
///
6+
/// [`enumerate`]: Iterator::enumerate
7+
/// [`Iterator`]: core::iter::Iterator
8+
#[must_use]
9+
#[derive(Drop, Clone, Debug)]
10+
pub struct Enumerate<I> {
11+
iter: I,
12+
count: usize,
13+
}
14+
15+
pub fn enumerated_iterator<I>(iter: I) -> Enumerate<I> {
16+
Enumerate { iter, count: 0 }
17+
}
18+
19+
impl EnumerateIterator<
20+
I, T, +Iterator<I>[Item: T], +Destruct<I>, +Destruct<T>,
21+
> of Iterator<Enumerate<I>> {
22+
type Item = (usize, T);
23+
24+
/// # Overflow Behavior
25+
///
26+
/// The method does no guarding against overflows, so enumerating more than
27+
/// `Bounded::<usize>::MAX` elements will always panic.
28+
///
29+
/// [`Bounded`]: core::num::traits::Bounded
30+
///
31+
/// # Panics
32+
///
33+
/// Will panic if the index of the element overflows a `usize`.
34+
#[inline]
35+
fn next(ref self: Enumerate<I>) -> Option<Self::Item> {
36+
let a = self.iter.next()?;
37+
let i = self.count;
38+
self.count += 1;
39+
Option::Some((i, a))
40+
}
41+
}

0 commit comments

Comments
 (0)