Skip to content

Commit

Permalink
Added unwraps as const fns.
Browse files Browse the repository at this point in the history
commit-id:7148ebf6
  • Loading branch information
orizi committed Jan 5, 2025
1 parent 04b8e01 commit ced3c81
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 28 deletions.
12 changes: 6 additions & 6 deletions corelib/src/option.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ pub trait OptionTrait<T> {
/// let value = option.expect('no value');
/// assert!(value == 123);
/// ```
fn expect(self: Option<T>, err: felt252) -> T;
const fn expect(self: Option<T>, err: felt252) -> T;

/// Returns the contained `Some` value, consuming the `self` value.
///
Expand All @@ -255,7 +255,7 @@ pub trait OptionTrait<T> {
/// let value = option.unwrap();
/// assert!(value == 123);
/// ```
fn unwrap(self: Option<T>) -> T;
const fn unwrap(self: Option<T>) -> T;

/// Transforms the `Option<T>` into a `Result<T, E>`, mapping `Option::Some(v)` to
/// `Result::Ok(v)` and `Option::None` to `Result::Err(err)`.
Expand Down Expand Up @@ -477,7 +477,7 @@ pub trait OptionTrait<T> {
/// let option = Option::None;
/// assert!(option.unwrap_or(456) == 456);
/// ```
fn unwrap_or<+Destruct<T>>(self: Option<T>, default: T) -> T;
const fn unwrap_or<+Destruct<T>>(self: Option<T>, default: T) -> T;

/// Returns the contained `Some` value if `self` is `Option::Some(x)`. Otherwise, returns
/// `Default::<T>::default()`.
Expand Down Expand Up @@ -582,15 +582,15 @@ pub trait OptionTrait<T> {

pub impl OptionTraitImpl<T> of OptionTrait<T> {
#[inline(always)]
fn expect(self: Option<T>, err: felt252) -> T {
const fn expect(self: Option<T>, err: felt252) -> T {
match self {
Option::Some(x) => x,
Option::None => crate::panic_with_felt252(err),
}
}

#[inline(always)]
fn unwrap(self: Option<T>) -> T {
const fn unwrap(self: Option<T>) -> T {
self.expect('Option::unwrap failed.')
}

Expand Down Expand Up @@ -694,7 +694,7 @@ pub impl OptionTraitImpl<T> of OptionTrait<T> {
}

#[inline]
fn unwrap_or<+Destruct<T>>(self: Option<T>, default: T) -> T {
const fn unwrap_or<+Destruct<T>>(self: Option<T>, default: T) -> T {
match self {
Option::Some(x) => x,
Option::None => default,
Expand Down
10 changes: 5 additions & 5 deletions corelib/src/result.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
/// let result: Result<felt252, felt252> = Result::Ok(123);
/// assert!(result.expect('no value') == 123);
/// ```
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
const fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
match self {
Result::Ok(x) => x,
Result::Err(_) => crate::panic_with_felt252(err),
Expand All @@ -251,7 +251,7 @@ pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
/// let result: Result<felt252, felt252> = Result::Ok(123);
/// assert!(result.unwrap() == 123);
/// ```
fn unwrap<+Destruct<E>>(self: Result<T, E>) -> T {
const fn unwrap<+Destruct<E>>(self: Result<T, E>) -> T {
self.expect('Result::unwrap failed.')
}

Expand All @@ -266,7 +266,7 @@ pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
/// let result: Result<felt252, felt252> = Result::Err('no value');
/// assert!(result.unwrap_or(456) == 456);
/// ```
fn unwrap_or<+Destruct<T>, +Destruct<E>>(self: Result<T, E>, default: T) -> T {
const fn unwrap_or<+Destruct<T>, +Destruct<E>>(self: Result<T, E>, default: T) -> T {
match self {
Result::Ok(x) => x,
Result::Err(_) => default,
Expand Down Expand Up @@ -443,7 +443,7 @@ pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
/// let result: Result<felt252, felt252> = Result::Err('no value');
/// assert!(result.expect_err('result is ok') == 'no value');
/// ```
fn expect_err<+PanicDestruct<T>>(self: Result<T, E>, err: felt252) -> E {
const fn expect_err<+PanicDestruct<T>>(self: Result<T, E>, err: felt252) -> E {
match self {
Result::Ok(_) => crate::panic_with_felt252(err),
Result::Err(x) => x,
Expand All @@ -462,7 +462,7 @@ pub impl ResultTraitImpl<T, E> of ResultTrait<T, E> {
/// let result: Result<felt252, felt252> = Result::Err('no value');
/// assert!(result.unwrap_err() == 'no value');
/// ```
fn unwrap_err<+PanicDestruct<T>>(self: Result<T, E>) -> E {
const fn unwrap_err<+PanicDestruct<T>>(self: Result<T, E>) -> E {
self.expect_err('Result::unwrap_err failed.')
}

Expand Down
1 change: 1 addition & 0 deletions crates/cairo-lang-plugins/src/plugins/generate_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ fn generate_trait_for_impl(db: &dyn SyntaxGroup, impl_ast: ast::ItemImpl) -> Plu
let decl = function_item.declaration(db);
let signature = decl.signature(db);
builder.add_node(function_item.attributes(db).as_syntax_node());
builder.add_node(decl.optional_const(db).as_syntax_node());
builder.add_node(decl.function_kw(db).as_syntax_node());
builder.add_node(decl.name(db).as_syntax_node());
builder.add_node(decl.generic_params(db).as_syntax_node());
Expand Down
47 changes: 46 additions & 1 deletion crates/cairo-lang-semantic/src/expr/test_data/constant
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ const FAILING_CALC: felt252 = if true {

const FUNC_CALC_SUCCESS: () = panic_if_true(false);
const FUNC_CALC_FAILURE: () = panic_if_true(true);
const FUNC_CALC_SUCCESS_OPTION: felt252 = Option::Some(5).unwrap();
const FUNC_CALC_FAILURE_OPTION: felt252 = Option::None.unwrap();
const FUNC_CALC_SUCCESS_RESULT1: felt252 = Result::<_, felt252>::Ok(5).unwrap();
const FUNC_CALC_FAILURE_RESULT1: felt252 = Result::Err(5).unwrap();
const FUNC_CALC_SUCCESS_RESULT2: felt252 = Result::<felt252, _>::Err(5).unwrap_err();
const FUNC_CALC_FAILURE_RESULT2: felt252 = Result::Ok(5).unwrap_err();

const fn panic_if_true(cond: bool) {
if cond {
Expand Down Expand Up @@ -107,10 +113,49 @@ error: Failed to calculate constant.
const FUNC_CALC_FAILURE: () = panic_if_true(true);
^^^^^^^^^^^^^^^^^^^
note: In `test::panic_if_true`:
--> lib.cairo:27:9
--> lib.cairo:33:9
core::panic_with_felt252('assertion failed')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: Failed to calculate constant.
--> lib.cairo:25:43
const FUNC_CALC_FAILURE_OPTION: felt252 = Option::None.unwrap();
^^^^^^^^^^^^^^^^^^^^^
note: In `core::option::OptionTraitImpl::<T>::expect`:
--> /home/ori/rust/cairo-ws/corelib/src/option.cairo:588:29
Option::None => crate::panic_with_felt252(err),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: In `core::option::OptionTraitImpl::<core::felt252>::unwrap`:
--> /home/ori/rust/cairo-ws/corelib/src/option.cairo:594:9
self.expect('Option::unwrap failed.')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: Failed to calculate constant.
--> lib.cairo:27:44
const FUNC_CALC_FAILURE_RESULT1: felt252 = Result::Err(5).unwrap();
^^^^^^^^^^^^^^^^^^^^^^^
note: In `core::result::ResultTraitImpl::<T, E>::expect::<core::traits::PanicDestructForDestruct::<E, +Destruct<E>>>`:
--> /home/ori/rust/cairo-ws/corelib/src/result.cairo:207:31
Result::Err(_) => crate::panic_with_felt252(err),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: In `core::result::ResultTraitImpl::<core::felt252, core::felt252>::unwrap::<core::traits::DestructFromDrop::<core::felt252, core::felt252Drop>>`:
--> /home/ori/rust/cairo-ws/corelib/src/result.cairo:224:9
self.expect('Result::unwrap failed.')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: Failed to calculate constant.
--> lib.cairo:29:44
const FUNC_CALC_FAILURE_RESULT2: felt252 = Result::Ok(5).unwrap_err();
^^^^^^^^^^^^^^^^^^^^^^^^^^
note: In `core::result::ResultTraitImpl::<T, E>::expect_err::<+PanicDestruct<T>>`:
--> /home/ori/rust/cairo-ws/corelib/src/result.cairo:417:30
Result::Ok(_) => crate::panic_with_felt252(err),
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: In `core::result::ResultTraitImpl::<core::felt252, core::felt252>::unwrap_err::<core::traits::PanicDestructForDestruct::<core::felt252, core::traits::DestructFromDrop::<core::felt252, core::felt252Drop>>>`:
--> /home/ori/rust/cairo-ws/corelib/src/result.cairo:435:9
self.expect_err('Result::unwrap_err failed.')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

//! > ==========================================================================

//! > Const of wrong type.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ Inlined at:
In function: lib.cairo::foo
enum_init<core::panics::PanicResult::<(core::integer::u8,)>, 0>([20]) -> ([21])
Originating location:
fn expect(self: Option<T>, err: felt252) -> T {
___________________________________________________^
const fn expect(self: Option<T>, err: felt252) -> T {
_________________________________________________________^
| ...
| }
|_____^
Expand All @@ -356,8 +356,8 @@ Inlined at:
In function: lib.cairo::foo
store_temp<RangeCheck>([17]) -> ([17])
Originating location:
fn expect(self: Option<T>, err: felt252) -> T {
___________________________________________________^
const fn expect(self: Option<T>, err: felt252) -> T {
_________________________________________________________^
| ...
| }
|_____^
Expand All @@ -372,8 +372,8 @@ Inlined at:
In function: lib.cairo::foo
store_temp<core::panics::PanicResult::<(core::integer::u8,)>>([21]) -> ([21])
Originating location:
fn expect(self: Option<T>, err: felt252) -> T {
___________________________________________________^
const fn expect(self: Option<T>, err: felt252) -> T {
_________________________________________________________^
| ...
| }
|_____^
Expand All @@ -388,8 +388,8 @@ Inlined at:
In function: lib.cairo::foo
return([17], [21])
Originating location:
fn expect(self: Option<T>, err: felt252) -> T {
___________________________________________________^
const fn expect(self: Option<T>, err: felt252) -> T {
_________________________________________________________^
| ...
| }
|_____^
Expand Down Expand Up @@ -684,8 +684,8 @@ Inlined at:
In function: lib.cairo::foo
enum_init<core::panics::PanicResult::<(core::integer::u8,)>, 0>([32]) -> ([33])
Originating location:
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
_________________________________________________________________________^
const fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
_______________________________________________________________________________^
| ...
| }
|_____^
Expand All @@ -700,8 +700,8 @@ Inlined at:
In function: lib.cairo::foo
store_temp<RangeCheck>([28]) -> ([28])
Originating location:
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
_________________________________________________________________________^
const fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
_______________________________________________________________________________^
| ...
| }
|_____^
Expand All @@ -716,8 +716,8 @@ Inlined at:
In function: lib.cairo::foo
store_temp<core::panics::PanicResult::<(core::integer::u8,)>>([33]) -> ([33])
Originating location:
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
_________________________________________________________________________^
const fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
_______________________________________________________________________________^
| ...
| }
|_____^
Expand All @@ -732,8 +732,8 @@ Inlined at:
In function: lib.cairo::foo
return([28], [33])
Originating location:
fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
_________________________________________________________________________^
const fn expect<+PanicDestruct<E>>(self: Result<T, E>, err: felt252) -> T {
_______________________________________________________________________________^
| ...
| }
|_____^
Expand Down

0 comments on commit ced3c81

Please sign in to comment.