From 8497b0998b22af64106a08e9fd5a235dd649a843 Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Sat, 4 Jan 2025 08:32:43 +0200 Subject: [PATCH] Added `unwrap`s as const fns. commit-id:7148ebf6 --- corelib/src/option.cairo | 12 ++--- corelib/src/result.cairo | 10 ++-- .../src/plugins/generate_trait.rs | 1 + .../src/expr/test_data/constant | 47 ++++++++++++++++++- .../src/statement_location_test_data/simple | 32 ++++++------- 5 files changed, 74 insertions(+), 28 deletions(-) diff --git a/corelib/src/option.cairo b/corelib/src/option.cairo index 3e6c450a45b..de3464bce8d 100644 --- a/corelib/src/option.cairo +++ b/corelib/src/option.cairo @@ -240,7 +240,7 @@ pub trait OptionTrait { /// let value = option.expect('no value'); /// assert!(value == 123); /// ``` - fn expect(self: Option, err: felt252) -> T; + const fn expect(self: Option, err: felt252) -> T; /// Returns the contained `Some` value, consuming the `self` value. /// @@ -255,7 +255,7 @@ pub trait OptionTrait { /// let value = option.unwrap(); /// assert!(value == 123); /// ``` - fn unwrap(self: Option) -> T; + const fn unwrap(self: Option) -> T; /// Transforms the `Option` into a `Result`, mapping `Option::Some(v)` to /// `Result::Ok(v)` and `Option::None` to `Result::Err(err)`. @@ -477,7 +477,7 @@ pub trait OptionTrait { /// let option = Option::None; /// assert!(option.unwrap_or(456) == 456); /// ``` - fn unwrap_or<+Destruct>(self: Option, default: T) -> T; + const fn unwrap_or<+Destruct>(self: Option, default: T) -> T; /// Returns the contained `Some` value if `self` is `Option::Some(x)`. Otherwise, returns /// `Default::::default()`. @@ -582,7 +582,7 @@ pub trait OptionTrait { pub impl OptionTraitImpl of OptionTrait { #[inline(always)] - fn expect(self: Option, err: felt252) -> T { + const fn expect(self: Option, err: felt252) -> T { match self { Option::Some(x) => x, Option::None => crate::panic_with_felt252(err), @@ -590,7 +590,7 @@ pub impl OptionTraitImpl of OptionTrait { } #[inline(always)] - fn unwrap(self: Option) -> T { + const fn unwrap(self: Option) -> T { self.expect('Option::unwrap failed.') } @@ -694,7 +694,7 @@ pub impl OptionTraitImpl of OptionTrait { } #[inline] - fn unwrap_or<+Destruct>(self: Option, default: T) -> T { + const fn unwrap_or<+Destruct>(self: Option, default: T) -> T { match self { Option::Some(x) => x, Option::None => default, diff --git a/corelib/src/result.cairo b/corelib/src/result.cairo index 87ef397f303..d68a369c4ed 100644 --- a/corelib/src/result.cairo +++ b/corelib/src/result.cairo @@ -232,7 +232,7 @@ pub impl ResultTraitImpl of ResultTrait { /// let result: Result = Result::Ok(123); /// assert!(result.expect('no value') == 123); /// ``` - fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + const fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { match self { Result::Ok(x) => x, Result::Err(_) => crate::panic_with_felt252(err), @@ -251,7 +251,7 @@ pub impl ResultTraitImpl of ResultTrait { /// let result: Result = Result::Ok(123); /// assert!(result.unwrap() == 123); /// ``` - fn unwrap<+Destruct>(self: Result) -> T { + const fn unwrap<+Destruct>(self: Result) -> T { self.expect('Result::unwrap failed.') } @@ -266,7 +266,7 @@ pub impl ResultTraitImpl of ResultTrait { /// let result: Result = Result::Err('no value'); /// assert!(result.unwrap_or(456) == 456); /// ``` - fn unwrap_or<+Destruct, +Destruct>(self: Result, default: T) -> T { + const fn unwrap_or<+Destruct, +Destruct>(self: Result, default: T) -> T { match self { Result::Ok(x) => x, Result::Err(_) => default, @@ -443,7 +443,7 @@ pub impl ResultTraitImpl of ResultTrait { /// let result: Result = Result::Err('no value'); /// assert!(result.expect_err('result is ok') == 'no value'); /// ``` - fn expect_err<+PanicDestruct>(self: Result, err: felt252) -> E { + const fn expect_err<+PanicDestruct>(self: Result, err: felt252) -> E { match self { Result::Ok(_) => crate::panic_with_felt252(err), Result::Err(x) => x, @@ -462,7 +462,7 @@ pub impl ResultTraitImpl of ResultTrait { /// let result: Result = Result::Err('no value'); /// assert!(result.unwrap_err() == 'no value'); /// ``` - fn unwrap_err<+PanicDestruct>(self: Result) -> E { + const fn unwrap_err<+PanicDestruct>(self: Result) -> E { self.expect_err('Result::unwrap_err failed.') } diff --git a/crates/cairo-lang-plugins/src/plugins/generate_trait.rs b/crates/cairo-lang-plugins/src/plugins/generate_trait.rs index 8dc472cbe4e..c7661704549 100644 --- a/crates/cairo-lang-plugins/src/plugins/generate_trait.rs +++ b/crates/cairo-lang-plugins/src/plugins/generate_trait.rs @@ -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()); diff --git a/crates/cairo-lang-semantic/src/expr/test_data/constant b/crates/cairo-lang-semantic/src/expr/test_data/constant index 4241a63bac1..fc2a1d2fb50 100644 --- a/crates/cairo-lang-semantic/src/expr/test_data/constant +++ b/crates/cairo-lang-semantic/src/expr/test_data/constant @@ -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::::Err(5).unwrap_err(); +const FUNC_CALC_FAILURE_RESULT2: felt252 = Result::Ok(5).unwrap_err(); const fn panic_if_true(cond: bool) { if cond { @@ -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::::expect`: + --> /home/ori/rust/cairo-ws/corelib/src/option.cairo:588:29 + Option::None => crate::panic_with_felt252(err), + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: In `core::option::OptionTraitImpl::::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::::expect::>>`: + --> /home/ori/rust/cairo-ws/corelib/src/result.cairo:207:31 + Result::Err(_) => crate::panic_with_felt252(err), + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: In `core::result::ResultTraitImpl::::unwrap::>`: + --> /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::::expect_err::<+PanicDestruct>`: + --> /home/ori/rust/cairo-ws/corelib/src/result.cairo:417:30 + Result::Ok(_) => crate::panic_with_felt252(err), + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +note: In `core::result::ResultTraitImpl::::unwrap_err::>>`: + --> /home/ori/rust/cairo-ws/corelib/src/result.cairo:435:9 + self.expect_err('Result::unwrap_err failed.') + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + //! > ========================================================================== //! > Const of wrong type. diff --git a/crates/cairo-lang-sierra-generator/src/statement_location_test_data/simple b/crates/cairo-lang-sierra-generator/src/statement_location_test_data/simple index 1f039edb5c1..dbaf79275be 100644 --- a/crates/cairo-lang-sierra-generator/src/statement_location_test_data/simple +++ b/crates/cairo-lang-sierra-generator/src/statement_location_test_data/simple @@ -340,8 +340,8 @@ Inlined at: In function: lib.cairo::foo enum_init, 0>([20]) -> ([21]) Originating location: - fn expect(self: Option, err: felt252) -> T { - ___________________________________________________^ + const fn expect(self: Option, err: felt252) -> T { + _________________________________________________________^ | ... | } |_____^ @@ -356,8 +356,8 @@ Inlined at: In function: lib.cairo::foo store_temp([17]) -> ([17]) Originating location: - fn expect(self: Option, err: felt252) -> T { - ___________________________________________________^ + const fn expect(self: Option, err: felt252) -> T { + _________________________________________________________^ | ... | } |_____^ @@ -372,8 +372,8 @@ Inlined at: In function: lib.cairo::foo store_temp>([21]) -> ([21]) Originating location: - fn expect(self: Option, err: felt252) -> T { - ___________________________________________________^ + const fn expect(self: Option, err: felt252) -> T { + _________________________________________________________^ | ... | } |_____^ @@ -388,8 +388,8 @@ Inlined at: In function: lib.cairo::foo return([17], [21]) Originating location: - fn expect(self: Option, err: felt252) -> T { - ___________________________________________________^ + const fn expect(self: Option, err: felt252) -> T { + _________________________________________________________^ | ... | } |_____^ @@ -684,8 +684,8 @@ Inlined at: In function: lib.cairo::foo enum_init, 0>([32]) -> ([33]) Originating location: - fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { - _________________________________________________________________________^ + const fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + _______________________________________________________________________________^ | ... | } |_____^ @@ -700,8 +700,8 @@ Inlined at: In function: lib.cairo::foo store_temp([28]) -> ([28]) Originating location: - fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { - _________________________________________________________________________^ + const fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + _______________________________________________________________________________^ | ... | } |_____^ @@ -716,8 +716,8 @@ Inlined at: In function: lib.cairo::foo store_temp>([33]) -> ([33]) Originating location: - fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { - _________________________________________________________________________^ + const fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + _______________________________________________________________________________^ | ... | } |_____^ @@ -732,8 +732,8 @@ Inlined at: In function: lib.cairo::foo return([28], [33]) Originating location: - fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { - _________________________________________________________________________^ + const fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + _______________________________________________________________________________^ | ... | } |_____^