Skip to content

Commit

Permalink
Added *Assign ops and Depracated *Eq ops.
Browse files Browse the repository at this point in the history
commit-id:2f5d0193
  • Loading branch information
orizi committed May 29, 2024
1 parent 66ea8fd commit 97e4ee1
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 35 deletions.
3 changes: 2 additions & 1 deletion corelib/src/byte_array.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,8 @@ impl ByteArrayAdd of Add<ByteArray> {
ByteArrayTrait::concat(@lhs, @rhs)
}
}
impl ByteArrayAddEq of AddEq<ByteArray> {
#[feature("deprecated-op-assign-traits")]
impl ByteArrayAddEq of core::traits::AddEq<ByteArray> {
#[inline]
fn add_eq(ref self: ByteArray, other: ByteArray) {
self.append(@other);
Expand Down
2 changes: 2 additions & 0 deletions corelib/src/ec.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ impl EcPointAdd of Add<EcPoint> {
}
}

#[feature("deprecated-op-assign-traits")]
impl EcPointAddEq of AddEq<EcPoint> {
#[inline(always)]
fn add_eq(ref self: EcPoint, other: EcPoint) {
Expand All @@ -213,6 +214,7 @@ impl EcPointSub of Sub<EcPoint> {
}
}

#[feature("deprecated-op-assign-traits")]
impl EcPointSubEq of SubEq<EcPoint> {
#[inline(always)]
fn sub_eq(ref self: EcPoint, other: EcPoint) {
Expand Down
1 change: 1 addition & 0 deletions corelib/src/integer.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,7 @@ impl U256Div = by_div_rem::DivImpl<u256>;
impl U256Rem = by_div_rem::RemImpl<u256>;

// Implementations for `*Eq` operations.
#[feature("deprecated-op-assign-traits")]
mod op_eq_by_op {
pub impl AddEqImpl<T, +Add<T>> of AddEq<T> {
fn add_eq(ref self: T, other: T) {
Expand Down
1 change: 1 addition & 0 deletions corelib/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 3 additions & 0 deletions corelib/src/ops.cairo
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod index;
pub use index::{Index, IndexView};

mod arith;
pub use arith::{AddAssign, SubAssign, MulAssign, DivAssign, RemAssign};
62 changes: 62 additions & 0 deletions corelib/src/ops/arith.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/// The addition assignment operator `+=`.
pub trait AddAssign<Lhs, Rhs> {
/// Performs the `+=` operation.
fn add_assign(ref self: Lhs, rhs: Rhs);
}

/// The subtraction assignment operator `-=`.
pub trait SubAssign<Lhs, Rhs> {
/// Performs the `-=` operation.
fn sub_assign(ref self: Lhs, rhs: Rhs);
}

/// The multiplication assignment operator `*=`.
pub trait MulAssign<Lhs, Rhs> {
/// Performs the `*=` operation.
fn mul_assign(ref self: Lhs, rhs: Rhs);
}

/// The division assignment operator `/=`.
pub trait DivAssign<Lhs, Rhs> {
/// Performs the `/=` operation.
fn div_assign(ref self: Lhs, rhs: Rhs);
}

/// The remainder assignment operator `%=`.
pub trait RemAssign<Lhs, Rhs> {
/// 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<T, impl Deprecated: AddEq<T>> of AddAssign<T, T> {
fn add_assign(ref self: T, rhs: T) {
Deprecated::add_eq(ref self, rhs)
}
}

impl DeprecatedSubAssign<T, impl Deprecated: SubEq<T>> of SubAssign<T, T> {
fn sub_assign(ref self: T, rhs: T) {
Deprecated::sub_eq(ref self, rhs)
}
}

impl DeprecatedMulAssign<T, impl Deprecated: MulEq<T>> of MulAssign<T, T> {
fn mul_assign(ref self: T, rhs: T) {
Deprecated::mul_eq(ref self, rhs)
}
}

impl DeprecatedDivAssign<T, impl Deprecated: DivEq<T>> of DivAssign<T, T> {
fn div_assign(ref self: T, rhs: T) {
Deprecated::div_eq(ref self, rhs)
}
}

impl DeprecatedRemAssign<T, impl Deprecated: RemEq<T>> of RemAssign<T, T> {
fn rem_assign(ref self: T, rhs: T) {
Deprecated::rem_eq(ref self, rhs)
}
}
33 changes: 30 additions & 3 deletions corelib/src/prelude/v2023_01.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
32 changes: 29 additions & 3 deletions corelib/src/prelude/v2023_10.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)]
Expand Down
15 changes: 15 additions & 0 deletions corelib/src/traits.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ impl SnapshotDrop<T> of Drop<@T>;
pub trait Add<T> {
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<T> {
fn add_eq(ref self: T, other: T);
}
Expand All @@ -18,6 +21,9 @@ pub trait AddEq<T> {
pub trait Sub<T> {
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<T> {
fn sub_eq(ref self: T, other: T);
}
Expand All @@ -26,6 +32,9 @@ pub trait SubEq<T> {
pub trait Mul<T> {
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<T> {
fn mul_eq(ref self: T, other: T);
}
Expand All @@ -34,6 +43,9 @@ pub trait MulEq<T> {
pub trait Div<T> {
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<T> {
fn div_eq(ref self: T, other: T);
}
Expand All @@ -42,6 +54,9 @@ pub trait DivEq<T> {
pub trait Rem<T> {
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<T> {
fn rem_eq(ref self: T, other: T);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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::<core::felt252, core::Felt252AddEq>::add_assign(v1{`x.a`}, v4{`1`})
(v7: test::MyStruct) <- struct_construct(v6{`x`}, v3{`x`})
End:
Return(v7)
Expand Down Expand Up @@ -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::<core::felt252, core::Felt252AddEq>::add_assign(v2{`x.a`}, v5{`1`})
(v8: core::felt252) <- 5
(v10: core::array::Array::<core::felt252>, v9: ()) <- core::array::ArrayImpl::<core::felt252>::append(v1{`__array_builder_macro_result__`}, v8{`5`})
(v11: test::MyStruct) <- struct_construct(v7{`x`}, v4{`x`})
Expand Down
42 changes: 21 additions & 21 deletions crates/cairo-lang-semantic/src/corelib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,33 +450,33 @@ pub fn core_binary_operator(
binary_op: &BinaryOperator,
stable_ptr: SyntaxStablePtrId,
) -> Maybe<Result<(ConcreteTraitGenericFunctionId, bool), SemanticDiagnosticKind>> {
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,
Expand Down
10 changes: 5 additions & 5 deletions crates/cairo-lang-semantic/src/diagnostic_test_data/plus_eq
Original file line number Diff line number Diff line change
Expand Up @@ -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::<core::felt252, core::bool>.
--> lib.cairo:3:5
x += true;
^*******^

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

Expand All @@ -106,7 +106,7 @@ fn foo() {
//! > function_body

//! > expected_diagnostics
error: Trait has no implementation in context: core::traits::DivEq::<core::bool>.
error: Trait has no implementation in context: core::ops::arith::DivAssign::<core::bool, core::bool>.
--> lib.cairo:3:5
x /= false;
^********^

0 comments on commit 97e4ee1

Please sign in to comment.