From 49a7db0db5c542f749677cdf685e819fa65db8c4 Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Wed, 29 May 2024 16:57:51 +0300 Subject: [PATCH] Added `*Assign` ops and Depracated `*Eq` ops. commit-id:2f5d0193 --- corelib/src/byte_array.cairo | 3 +- corelib/src/ec.cairo | 2 + corelib/src/integer.cairo | 1 + corelib/src/lib.cairo | 1 + corelib/src/ops.cairo | 3 + corelib/src/ops/arith.cairo | 62 +++++++++++++++++++ corelib/src/prelude/v2023_01.cairo | 33 +++++++++- corelib/src/prelude/v2023_10.cairo | 32 +++++++++- corelib/src/traits.cairo | 15 +++++ .../src/borrow_check/test_data/borrow_check | 4 +- crates/cairo-lang-semantic/src/corelib.rs | 42 ++++++------- .../src/diagnostic_test_data/plus_eq | 10 +-- 12 files changed, 173 insertions(+), 35 deletions(-) create mode 100644 corelib/src/ops/arith.cairo diff --git a/corelib/src/byte_array.cairo b/corelib/src/byte_array.cairo index fa3fbc5edda..81ce8cc80a4 100644 --- a/corelib/src/byte_array.cairo +++ b/corelib/src/byte_array.cairo @@ -348,7 +348,8 @@ impl ByteArrayAdd of Add { ByteArrayTrait::concat(@lhs, @rhs) } } -impl ByteArrayAddEq of AddEq { +#[feature("deprecated-op-assign-traits")] +impl ByteArrayAddEq of core::traits::AddEq { #[inline] fn add_eq(ref self: ByteArray, other: ByteArray) { self.append(@other); diff --git a/corelib/src/ec.cairo b/corelib/src/ec.cairo index ce90af1ad16..df10af1d770 100644 --- a/corelib/src/ec.cairo +++ b/corelib/src/ec.cairo @@ -193,6 +193,7 @@ impl EcPointAdd of Add { } } +#[feature("deprecated-op-assign-traits")] impl EcPointAddEq of AddEq { #[inline(always)] fn add_eq(ref self: EcPoint, other: EcPoint) { @@ -213,6 +214,7 @@ impl EcPointSub of Sub { } } +#[feature("deprecated-op-assign-traits")] impl EcPointSubEq of SubEq { #[inline(always)] fn sub_eq(ref self: EcPoint, other: EcPoint) { diff --git a/corelib/src/integer.cairo b/corelib/src/integer.cairo index e18f7ab0103..4427e6cb9df 100644 --- a/corelib/src/integer.cairo +++ b/corelib/src/integer.cairo @@ -2094,6 +2094,7 @@ impl U256Div = by_div_rem::DivImpl; impl U256Rem = by_div_rem::RemImpl; // Implementations for `*Eq` operations. +#[feature("deprecated-op-assign-traits")] mod op_eq_by_op { pub impl AddEqImpl> of AddEq { fn add_eq(ref self: T, other: T) { diff --git a/corelib/src/lib.cairo b/corelib/src/lib.cairo index 08830f97fdf..a5d8d599602 100644 --- a/corelib/src/lib.cairo +++ b/corelib/src/lib.cairo @@ -1,5 +1,6 @@ pub mod traits; #[feature("deprecated-index-traits")] +#[feature("deprecated-op-assign-traits")] use traits::{ Add, AddEq, BitAnd, BitNot, BitOr, BitXor, Copy, Div, DivEq, DivRem, Drop, Mul, MulEq, PartialEq, PartialOrd, Rem, RemEq, Sub, SubEq, TupleSize0Copy, TupleSize0Drop, Not, Neg, Into, diff --git a/corelib/src/ops.cairo b/corelib/src/ops.cairo index fde655c542d..9d8fcf54f49 100644 --- a/corelib/src/ops.cairo +++ b/corelib/src/ops.cairo @@ -1,2 +1,5 @@ pub mod index; pub use index::{Index, IndexView}; + +mod arith; +pub use arith::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign}; diff --git a/corelib/src/ops/arith.cairo b/corelib/src/ops/arith.cairo new file mode 100644 index 00000000000..2ad4bd4f36f --- /dev/null +++ b/corelib/src/ops/arith.cairo @@ -0,0 +1,62 @@ +/// The addition assignment operator `+=`. +pub trait AddAssign { + /// Performs the `+=` operation. + fn add_assign(ref self: Lhs, rhs: Rhs); +} + +/// The subtraction assignment operator `-=`. +pub trait SubAssign { + /// Performs the `-=` operation. + fn sub_assign(ref self: Lhs, rhs: Rhs); +} + +/// The multiplication assignment operator `*=`. +pub trait MulAssign { + /// Performs the `*=` operation. + fn mul_assign(ref self: Lhs, rhs: Rhs); +} + +/// The division assignment operator `/=`. +pub trait DivAssign { + /// Performs the `/=` operation. + fn div_assign(ref self: Lhs, rhs: Rhs); +} + +/// The remainder assignment operator `%=`. +pub trait RemAssign { + /// Performs the `%=` operation. + fn rem_assign(ref self: Lhs, rhs: Rhs); +} + +#[feature("deprecated-op-assign-traits")] +use core::traits::{AddEq, SubEq, MulEq, DivEq, RemEq}; + +impl DeprecatedAddAssign> of AddAssign { + fn add_assign(ref self: T, rhs: T) { + Deprecated::add_eq(ref self, rhs) + } +} + +impl DeprecatedSubAssign> of SubAssign { + fn sub_assign(ref self: T, rhs: T) { + Deprecated::sub_eq(ref self, rhs) + } +} + +impl DeprecatedMulAssign> of MulAssign { + fn mul_assign(ref self: T, rhs: T) { + Deprecated::mul_eq(ref self, rhs) + } +} + +impl DeprecatedDivAssign> of DivAssign { + fn div_assign(ref self: T, rhs: T) { + Deprecated::div_eq(ref self, rhs) + } +} + +impl DeprecatedRemAssign> of RemAssign { + fn rem_assign(ref self: T, rhs: T) { + Deprecated::rem_eq(ref self, rhs) + } +} diff --git a/corelib/src/prelude/v2023_01.cairo b/corelib/src/prelude/v2023_01.cairo index 8789bc63e86..b3886471dd5 100644 --- a/corelib/src/prelude/v2023_01.cairo +++ b/corelib/src/prelude/v2023_01.cairo @@ -59,11 +59,38 @@ use core::to_byte_array; use core::{ traits, traits::{ - Add, AddEq, BitAnd, BitNot, BitOr, BitXor, Copy, Default, Destruct, Div, DivEq, DivRem, - Drop, Felt252DictValue, Into, Mul, MulEq, Neg, Not, PanicDestruct, PartialEq, PartialOrd, - Rem, RemEq, Sub, SubEq, TryInto, TupleSize0Copy, TupleSize0Drop + Add, BitAnd, BitNot, BitOr, BitXor, Copy, Default, Destruct, Div, DivRem, Drop, + Felt252DictValue, Into, Mul, Neg, Not, PanicDestruct, PartialEq, PartialOrd, Rem, Sub, + TryInto, TupleSize0Copy, TupleSize0Drop } }; + +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::AddAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::AddEq; +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::SubAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::SubEq; +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::MulAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::MulEq; +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::DivAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::DivEq; +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::RemAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::RemEq; + #[feature("deprecated-index-traits")] use core::traits::Index; #[feature("deprecated-index-traits")] diff --git a/corelib/src/prelude/v2023_10.cairo b/corelib/src/prelude/v2023_10.cairo index 6fce484f89a..a6f4e601a62 100644 --- a/corelib/src/prelude/v2023_10.cairo +++ b/corelib/src/prelude/v2023_10.cairo @@ -20,11 +20,37 @@ pub use core::serde::Serde; pub use core::{starknet, starknet::System}; pub use core::to_byte_array; pub use core::traits::{ - Add, AddEq, BitAnd, BitNot, BitOr, BitXor, Copy, Default, Destruct, Div, DivEq, DivRem, Drop, - Felt252DictValue, Into, Mul, MulEq, Neg, Not, PanicDestruct, PartialEq, PartialOrd, Rem, RemEq, - Sub, SubEq, TryInto + Add, BitAnd, BitNot, BitOr, BitXor, Copy, Default, Destruct, Div, DivRem, Drop, + Felt252DictValue, Into, Mul, Neg, Not, PanicDestruct, PartialEq, PartialOrd, Rem, Sub, TryInto }; + +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::AddAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::AddEq; +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::SubAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::SubEq; +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::MulAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::MulEq; +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::DivAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::DivEq; +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::RemAssign`.", since: "2.7.0" +)] +#[feature("deprecated-op-assign-traits")] +pub use core::traits::RemEq; + #[deprecated( feature: "deprecated-index-traits", note: "Use `core::ops::index::Index`.", since: "2.7.0" )] diff --git a/corelib/src/traits.cairo b/corelib/src/traits.cairo index be6fbde9c96..191e9d428b9 100644 --- a/corelib/src/traits.cairo +++ b/corelib/src/traits.cairo @@ -10,6 +10,9 @@ impl SnapshotDrop of Drop<@T>; pub trait Add { fn add(lhs: T, rhs: T) -> T; } +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::AddAssign`.", since: "2.7.0" +)] pub trait AddEq { fn add_eq(ref self: T, other: T); } @@ -18,6 +21,9 @@ pub trait AddEq { pub trait Sub { fn sub(lhs: T, rhs: T) -> T; } +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::SubAssign`.", since: "2.7.0" +)] pub trait SubEq { fn sub_eq(ref self: T, other: T); } @@ -26,6 +32,9 @@ pub trait SubEq { pub trait Mul { fn mul(lhs: T, rhs: T) -> T; } +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::MulAssign`.", since: "2.7.0" +)] pub trait MulEq { fn mul_eq(ref self: T, other: T); } @@ -34,6 +43,9 @@ pub trait MulEq { pub trait Div { fn div(lhs: T, rhs: T) -> T; } +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::DivAssign`.", since: "2.7.0" +)] pub trait DivEq { fn div_eq(ref self: T, other: T); } @@ -42,6 +54,9 @@ pub trait DivEq { pub trait Rem { fn rem(lhs: T, rhs: T) -> T; } +#[deprecated( + feature: "deprecated-op-assign-traits", note: "Use `core::ops::RemAssign`.", since: "2.7.0" +)] pub trait RemEq { fn rem_eq(ref self: T, other: T); } diff --git a/crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check b/crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check index f02ffd98040..bb0e332567f 100644 --- a/crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check +++ b/crates/cairo-lang-lowering/src/borrow_check/test_data/borrow_check @@ -782,7 +782,7 @@ Statements: (v1: core::felt252) <- 17 (v2: core::felt252, v3: test::NonCopy) <- struct_destructure(v0{`x`}) (v4: core::felt252) <- 1 - (v6: core::felt252, v5: ()) <- core::Felt252AddEq::add_eq(v1{`x.a`}, v4{`1`}) + (v6: core::felt252, v5: ()) <- core::ops::arith::DeprecatedAddAssign::::add_assign(v1{`x.a`}, v4{`1`}) (v7: test::MyStruct) <- struct_construct(v6{`x`}, v3{`x`}) End: Return(v7) @@ -899,7 +899,7 @@ Statements: (v2: core::felt252) <- 17 (v3: core::felt252, v4: test::NonCopy) <- struct_destructure(v0{`x`}) (v5: core::felt252) <- 1 - (v7: core::felt252, v6: ()) <- core::Felt252AddEq::add_eq(v2{`x.a`}, v5{`1`}) + (v7: core::felt252, v6: ()) <- core::ops::arith::DeprecatedAddAssign::::add_assign(v2{`x.a`}, v5{`1`}) (v8: core::felt252) <- 5 (v10: core::array::Array::, v9: ()) <- core::array::ArrayImpl::::append(v1{`__array_builder_macro_result__`}, v8{`5`}) (v11: test::MyStruct) <- struct_construct(v7{`x`}, v4{`x`}) diff --git a/crates/cairo-lang-semantic/src/corelib.rs b/crates/cairo-lang-semantic/src/corelib.rs index ed77c7cc6fd..987f2f96d16 100644 --- a/crates/cairo-lang-semantic/src/corelib.rs +++ b/crates/cairo-lang-semantic/src/corelib.rs @@ -450,33 +450,33 @@ pub fn core_binary_operator( binary_op: &BinaryOperator, stable_ptr: SyntaxStablePtrId, ) -> Maybe> { - let (trait_name, function_name, snapshot) = match binary_op { - BinaryOperator::Plus(_) => ("Add", "add", false), - BinaryOperator::PlusEq(_) => ("AddEq", "add_eq", false), - BinaryOperator::Minus(_) => ("Sub", "sub", false), - BinaryOperator::MinusEq(_) => ("SubEq", "sub_eq", false), - BinaryOperator::Mul(_) => ("Mul", "mul", false), - BinaryOperator::MulEq(_) => ("MulEq", "mul_eq", false), - BinaryOperator::Div(_) => ("Div", "div", false), - BinaryOperator::DivEq(_) => ("DivEq", "div_eq", false), - BinaryOperator::Mod(_) => ("Rem", "rem", false), - BinaryOperator::ModEq(_) => ("RemEq", "rem_eq", false), - BinaryOperator::EqEq(_) => ("PartialEq", "eq", true), - BinaryOperator::Neq(_) => ("PartialEq", "ne", true), - BinaryOperator::LE(_) => ("PartialOrd", "le", false), - BinaryOperator::GE(_) => ("PartialOrd", "ge", false), - BinaryOperator::LT(_) => ("PartialOrd", "lt", false), - BinaryOperator::GT(_) => ("PartialOrd", "gt", false), - BinaryOperator::And(_) => ("BitAnd", "bitand", false), - BinaryOperator::Or(_) => ("BitOr", "bitor", false), - BinaryOperator::Xor(_) => ("BitXor", "bitxor", false), + let (trait_name, function_name, snapshot, context) = match binary_op { + BinaryOperator::Plus(_) => ("Add", "add", false, CoreTraitContext::TopLevel), + BinaryOperator::PlusEq(_) => ("AddAssign", "add_assign", false, CoreTraitContext::Ops), + BinaryOperator::Minus(_) => ("Sub", "sub", false, CoreTraitContext::TopLevel), + BinaryOperator::MinusEq(_) => ("SubAssign", "sub_assign", false, CoreTraitContext::Ops), + BinaryOperator::Mul(_) => ("Mul", "mul", false, CoreTraitContext::TopLevel), + BinaryOperator::MulEq(_) => ("MulAssign", "mul_assign", false, CoreTraitContext::Ops), + BinaryOperator::Div(_) => ("Div", "div", false, CoreTraitContext::TopLevel), + BinaryOperator::DivEq(_) => ("DivAssign", "div_assign", false, CoreTraitContext::Ops), + BinaryOperator::Mod(_) => ("Rem", "rem", false, CoreTraitContext::TopLevel), + BinaryOperator::ModEq(_) => ("RemAssign", "rem_assign", false, CoreTraitContext::Ops), + BinaryOperator::EqEq(_) => ("PartialEq", "eq", true, CoreTraitContext::TopLevel), + BinaryOperator::Neq(_) => ("PartialEq", "ne", true, CoreTraitContext::TopLevel), + BinaryOperator::LE(_) => ("PartialOrd", "le", false, CoreTraitContext::TopLevel), + BinaryOperator::GE(_) => ("PartialOrd", "ge", false, CoreTraitContext::TopLevel), + BinaryOperator::LT(_) => ("PartialOrd", "lt", false, CoreTraitContext::TopLevel), + BinaryOperator::GT(_) => ("PartialOrd", "gt", false, CoreTraitContext::TopLevel), + BinaryOperator::And(_) => ("BitAnd", "bitand", false, CoreTraitContext::TopLevel), + BinaryOperator::Or(_) => ("BitOr", "bitor", false, CoreTraitContext::TopLevel), + BinaryOperator::Xor(_) => ("BitXor", "bitxor", false, CoreTraitContext::TopLevel), _ => return Ok(Err(SemanticDiagnosticKind::UnknownBinaryOperator)), }; Ok(Ok(( get_core_trait_function_infer( db, inference, - CoreTraitContext::TopLevel, + context, trait_name.into(), function_name.into(), stable_ptr, diff --git a/crates/cairo-lang-semantic/src/diagnostic_test_data/plus_eq b/crates/cairo-lang-semantic/src/diagnostic_test_data/plus_eq index 3c7addffc17..01eb9696605 100644 --- a/crates/cairo-lang-semantic/src/diagnostic_test_data/plus_eq +++ b/crates/cairo-lang-semantic/src/diagnostic_test_data/plus_eq @@ -82,10 +82,10 @@ fn foo() { //! > function_body //! > expected_diagnostics -error: Mismatched types. The type `core::bool` cannot be created from a numeric literal. - --> lib.cairo:2:17 - let mut x = 3; - ^ +error: Trait has no implementation in context: core::ops::arith::AddAssign::. + --> lib.cairo:3:5 + x += true; + ^*******^ //! > ========================================================================== @@ -106,7 +106,7 @@ fn foo() { //! > function_body //! > expected_diagnostics -error: Trait has no implementation in context: core::traits::DivEq::. +error: Trait has no implementation in context: core::ops::arith::DivAssign::. --> lib.cairo:3:5 x /= false; ^********^