From 80fadf8fabc06e457e2d0561233c218bc8529f47 Mon Sep 17 00:00:00 2001 From: ilyalesokhin-starkware Date: Thu, 27 Jun 2024 19:02:49 +0300 Subject: [PATCH] Require destruct instead of drop for unwrapping. (#5902) --- corelib/src/option.cairo | 8 ++++---- corelib/src/result.cairo | 20 +++++++++---------- corelib/src/test/circuit_test.cairo | 17 +++++++--------- .../src/statement_location_test_data/simple | 16 +++++++-------- 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/corelib/src/option.cairo b/corelib/src/option.cairo index 6db7b302bb1..9f2a874b147 100644 --- a/corelib/src/option.cairo +++ b/corelib/src/option.cairo @@ -22,7 +22,7 @@ pub trait OptionTrait { 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)`. - fn ok_or>(self: Option, err: E) -> Result; + fn ok_or>(self: Option, err: E) -> Result; /// Returns `true` if the `Option` is `Option::Some`. #[must_use] fn is_some(self: @Option) -> bool; @@ -30,7 +30,7 @@ pub trait OptionTrait { #[must_use] fn is_none(self: @Option) -> bool; /// If `self` is `Option::Some(x)`, returns `x`. Otherwise, returns the provided default. - fn unwrap_or<+Drop>(self: Option, default: T) -> T; + fn unwrap_or<+Destruct>(self: Option, default: T) -> T; /// If `self` is `Option::Some(x)`, returns `x`. Otherwise, returns `Default::::default()`. fn unwrap_or_default<+Default>(self: Option) -> T; } @@ -50,7 +50,7 @@ pub impl OptionTraitImpl of OptionTrait { } #[inline] - fn ok_or>(self: Option, err: E) -> Result { + fn ok_or>(self: Option, err: E) -> Result { match self { Option::Some(v) => Result::Ok(v), Option::None => Result::Err(err), @@ -74,7 +74,7 @@ pub impl OptionTraitImpl of OptionTrait { } #[inline] - fn unwrap_or<+Drop>(self: Option, default: T) -> T { + 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 5534d7f2533..5c1448adef2 100644 --- a/corelib/src/result.cairo +++ b/corelib/src/result.cairo @@ -12,18 +12,18 @@ pub enum Result { #[generate_trait] pub impl ResultTraitImpl of ResultTrait { /// If `val` is `Result::Ok(x)`, returns `x`. Otherwise, panics with `err`. - fn expect<+Drop>(self: Result, err: felt252) -> T { + fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { match self { Result::Ok(x) => x, Result::Err(_) => core::panic_with_felt252(err), } } /// If `val` is `Result::Ok(x)`, returns `x`. Otherwise, panics. - fn unwrap<+Drop>(self: Result) -> T { + fn unwrap<+Destruct>(self: Result) -> T { self.expect('Result::unwrap failed.') } /// If `val` is `Result::Ok(x)`, returns `x`. Otherwise, returns `default`. - fn unwrap_or<+Drop, +Drop>(self: Result, default: T) -> T { + fn unwrap_or<+Destruct, +Destruct>(self: Result, default: T) -> T { match self { Result::Ok(x) => x, Result::Err(_) => default, @@ -31,7 +31,7 @@ pub impl ResultTraitImpl of ResultTrait { } /// If `val` is `Result::Ok(x)`, returns `x`. /// Otherwise returns `Default::::default()`. - fn unwrap_or_default<+Drop, +Default>(self: Result) -> T { + fn unwrap_or_default<+Destruct, +Default>(self: Result) -> T { match self { Result::Ok(x) => x, Result::Err(_) => Default::default(), @@ -39,14 +39,14 @@ pub impl ResultTraitImpl of ResultTrait { } /// If `val` is `Result::Err(x)`, returns `x`. Otherwise, panics with `err`. - fn expect_err<+Drop>(self: Result, err: felt252) -> E { + fn expect_err<+PanicDestruct>(self: Result, err: felt252) -> E { match self { Result::Ok(_) => core::panic_with_felt252(err), Result::Err(x) => x, } } /// If `val` is `Result::Err(x)`, returns `x`. Otherwise, panics. - fn unwrap_err<+Drop>(self: Result) -> E { + fn unwrap_err<+PanicDestruct>(self: Result) -> E { self.expect_err('Result::unwrap_err failed.') } /// Returns `true` if the `Result` is `Result::Ok`. @@ -67,7 +67,7 @@ pub impl ResultTraitImpl of ResultTrait { } /// Returns `true` if the `Result` is `Result::Ok`, and consumes the value. #[inline] - fn into_is_err<+Drop, +Drop>(self: Result) -> bool { + fn into_is_err<+Destruct, +Destruct>(self: Result) -> bool { match self { Result::Ok(_) => false, Result::Err(_) => true, @@ -75,7 +75,7 @@ pub impl ResultTraitImpl of ResultTrait { } /// Returns `true` if the `Result` is `Result::Err`, and consumes the value. #[inline] - fn into_is_ok<+Drop, +Drop>(self: Result) -> bool { + fn into_is_ok<+Destruct, +Destruct>(self: Result) -> bool { match self { Result::Ok(_) => true, Result::Err(_) => false, @@ -96,7 +96,7 @@ pub impl ResultTraitImpl of ResultTrait { /// let x: Result = Result::Err("Nothing here"); /// assert!(x.ok().is_none()); /// ``` - fn ok<+Drop, +Drop>(self: Result) -> Option { + fn ok<+Destruct, +Destruct>(self: Result) -> Option { match self { Result::Ok(x) => Option::Some(x), Result::Err(_) => Option::None, @@ -117,7 +117,7 @@ pub impl ResultTraitImpl of ResultTrait { /// let x: Result = Result::Ok(2); /// assert!(x.err().is_none()); /// ``` - fn err<+Drop, +Drop>(self: Result) -> Option { + fn err<+Destruct, +Destruct>(self: Result) -> Option { match self { Result::Ok(_) => Option::None, Result::Err(x) => Option::Some(x), diff --git a/corelib/src/test/circuit_test.cairo b/corelib/src/test/circuit_test.cairo index a5e8e837a3b..5f13610a227 100644 --- a/corelib/src/test/circuit_test.cairo +++ b/corelib/src/test/circuit_test.cairo @@ -30,16 +30,13 @@ fn test_circuit_success() { let mul = circuit_mul(inv, sub); let modulus = TryInto::<_, CircuitModulus>::try_into([7, 0, 0, 0]).unwrap(); - let outputs = - match (mul, add, inv) - .new_inputs() - .next([3, 0, 0, 0]) - .next([6, 0, 0, 0]) - .done() - .eval(modulus) { - Result::Ok(outputs) => { outputs }, - Result::Err(_) => { panic!("Expected success") } - }; + let outputs = (mul, add, inv) + .new_inputs() + .next([3, 0, 0, 0]) + .next([6, 0, 0, 0]) + .done() + .eval(modulus) + .unwrap(); assert_eq!(outputs.get_output(add), u384 { limb0: 2, limb1: 0, limb2: 0, limb3: 0 }); assert_eq!(outputs.get_output(inv), u384 { limb0: 4, limb1: 0, limb2: 0, limb3: 0 }); 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 a0ad1d474ad..d4682e97d5a 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 @@ -618,8 +618,8 @@ Inlined at: In function: lib.cairo::foo enum_init, 0>([32]) -> ([33]) Originating location: - fn expect<+Drop>(self: Result, err: felt252) -> T { - ^ + fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + ^ In function: core::result::ResultTraitImpl::expect Inlined at: u8_overflowing_add(lhs, rhs).expect('u8_add Overflow') @@ -631,8 +631,8 @@ Inlined at: In function: lib.cairo::foo store_temp([28]) -> ([28]) Originating location: - fn expect<+Drop>(self: Result, err: felt252) -> T { - ^ + fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + ^ In function: core::result::ResultTraitImpl::expect Inlined at: u8_overflowing_add(lhs, rhs).expect('u8_add Overflow') @@ -644,8 +644,8 @@ Inlined at: In function: lib.cairo::foo store_temp>([33]) -> ([33]) Originating location: - fn expect<+Drop>(self: Result, err: felt252) -> T { - ^ + fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + ^ In function: core::result::ResultTraitImpl::expect Inlined at: u8_overflowing_add(lhs, rhs).expect('u8_add Overflow') @@ -657,8 +657,8 @@ Inlined at: In function: lib.cairo::foo return([28], [33]) Originating location: - fn expect<+Drop>(self: Result, err: felt252) -> T { - ^ + fn expect<+PanicDestruct>(self: Result, err: felt252) -> T { + ^ In function: core::result::ResultTraitImpl::expect Inlined at: u8_overflowing_add(lhs, rhs).expect('u8_add Overflow')