Skip to content

Make most std::ops traits const on numeric types #89876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions library/core/src/internal_macros.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,23 @@ macro_rules! forward_ref_unop {
forward_ref_unop!(impl $imp, $method for $t,
#[stable(feature = "rust1", since = "1.0.0")]);
};
(impl const $imp:ident, $method:ident for $t:ty) => {
forward_ref_unop!(impl const $imp, $method for $t,
#[stable(feature = "rust1", since = "1.0.0")]);
};
// Equivalent to the non-const version, with the addition of `rustc_const_unstable`
(impl const $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
#[$attr]
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const $imp for &$t {
type Output = <$t as $imp>::Output;

#[inline]
fn $method(self) -> <$t as $imp>::Output {
$imp::$method(*self)
}
}
};
(impl $imp:ident, $method:ident for $t:ty, #[$attr:meta]) => {
#[$attr]
impl $imp for &$t {
@@ -25,6 +42,45 @@ macro_rules! forward_ref_binop {
forward_ref_binop!(impl $imp, $method for $t, $u,
#[stable(feature = "rust1", since = "1.0.0")]);
};
(impl const $imp:ident, $method:ident for $t:ty, $u:ty) => {
forward_ref_binop!(impl const $imp, $method for $t, $u,
#[stable(feature = "rust1", since = "1.0.0")]);
};
// Equivalent to the non-const version, with the addition of `rustc_const_unstable`
(impl const $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
#[$attr]
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl<'a> const $imp<$u> for &'a $t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
$imp::$method(*self, other)
}
}

#[$attr]
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const $imp<&$u> for $t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
$imp::$method(self, *other)
}
}

#[$attr]
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const $imp<&$u> for &$t {
type Output = <$t as $imp<$u>>::Output;

#[inline]
fn $method(self, other: &$u) -> <$t as $imp<$u>>::Output {
$imp::$method(*self, *other)
}
}
};
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
#[$attr]
impl<'a> $imp<$u> for &'a $t {
@@ -65,6 +121,21 @@ macro_rules! forward_ref_op_assign {
forward_ref_op_assign!(impl $imp, $method for $t, $u,
#[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
};
(impl const $imp:ident, $method:ident for $t:ty, $u:ty) => {
forward_ref_op_assign!(impl const $imp, $method for $t, $u,
#[stable(feature = "op_assign_builtins_by_ref", since = "1.22.0")]);
};
// Equivalent to the non-const version, with the addition of `rustc_const_unstable`
(impl const $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
#[$attr]
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const $imp<&$u> for $t {
#[inline]
fn $method(&mut self, other: &$u) {
$imp::$method(self, *other);
}
}
};
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
#[$attr]
impl $imp<&$u> for $t {
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -116,6 +116,7 @@
#![feature(const_maybe_uninit_as_ptr)]
#![feature(const_maybe_uninit_assume_init)]
#![feature(const_num_from_num)]
#![feature(const_ops)]
#![feature(const_option)]
#![feature(const_pin)]
#![feature(const_replace)]
21 changes: 14 additions & 7 deletions library/core/src/num/nonzero.rs
Original file line number Diff line number Diff line change
@@ -92,7 +92,8 @@ macro_rules! nonzero_integers {
}

#[stable(feature = "nonzero_bitor", since = "1.45.0")]
impl BitOr for $Ty {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOr for $Ty {
type Output = Self;
#[inline]
fn bitor(self, rhs: Self) -> Self::Output {
@@ -103,7 +104,8 @@ macro_rules! nonzero_integers {
}

#[stable(feature = "nonzero_bitor", since = "1.45.0")]
impl BitOr<$Int> for $Ty {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOr<$Int> for $Ty {
type Output = Self;
#[inline]
fn bitor(self, rhs: $Int) -> Self::Output {
@@ -115,7 +117,8 @@ macro_rules! nonzero_integers {
}

#[stable(feature = "nonzero_bitor", since = "1.45.0")]
impl BitOr<$Ty> for $Int {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOr<$Ty> for $Int {
type Output = $Ty;
#[inline]
fn bitor(self, rhs: $Ty) -> Self::Output {
@@ -127,15 +130,17 @@ macro_rules! nonzero_integers {
}

#[stable(feature = "nonzero_bitor", since = "1.45.0")]
impl BitOrAssign for $Ty {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOrAssign for $Ty {
#[inline]
fn bitor_assign(&mut self, rhs: Self) {
*self = *self | rhs;
}
}

#[stable(feature = "nonzero_bitor", since = "1.45.0")]
impl BitOrAssign<$Int> for $Ty {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOrAssign<$Int> for $Ty {
#[inline]
fn bitor_assign(&mut self, rhs: $Int) {
*self = *self | rhs;
@@ -257,7 +262,8 @@ macro_rules! nonzero_integers_div {
( $( $Ty: ident($Int: ty); )+ ) => {
$(
#[stable(feature = "nonzero_div", since = "1.51.0")]
impl Div<$Ty> for $Int {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Div<$Ty> for $Int {
type Output = $Int;
/// This operation rounds towards zero,
/// truncating any fractional part of the exact result, and cannot panic.
@@ -270,7 +276,8 @@ macro_rules! nonzero_integers_div {
}

#[stable(feature = "nonzero_div", since = "1.51.0")]
impl Rem<$Ty> for $Int {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Rem<$Ty> for $Int {
type Output = $Int;
/// This operation satisfies `n % d == n - (n / d) * d`, and cannot panic.
#[inline]
128 changes: 77 additions & 51 deletions library/core/src/num/wrapping.rs
Original file line number Diff line number Diff line change
@@ -87,7 +87,8 @@ impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> {
macro_rules! sh_impl_signed {
($t:ident, $f:ident) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl Shl<$f> for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Shl<$f> for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
@@ -99,20 +100,22 @@ macro_rules! sh_impl_signed {
}
}
}
forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
forward_ref_binop! { impl const Shl, shl for Wrapping<$t>, $f,
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl ShlAssign<$f> for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const ShlAssign<$f> for Wrapping<$t> {
#[inline]
fn shl_assign(&mut self, other: $f) {
*self = *self << other;
}
}
forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }
forward_ref_op_assign! { impl const ShlAssign, shl_assign for Wrapping<$t>, $f }

#[stable(feature = "rust1", since = "1.0.0")]
impl Shr<$f> for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Shr<$f> for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
@@ -124,63 +127,68 @@ macro_rules! sh_impl_signed {
}
}
}
forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
forward_ref_binop! { impl const Shr, shr for Wrapping<$t>, $f,
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl ShrAssign<$f> for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const ShrAssign<$f> for Wrapping<$t> {
#[inline]
fn shr_assign(&mut self, other: $f) {
*self = *self >> other;
}
}
forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
forward_ref_op_assign! { impl const ShrAssign, shr_assign for Wrapping<$t>, $f }
};
}

macro_rules! sh_impl_unsigned {
($t:ident, $f:ident) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl Shl<$f> for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Shl<$f> for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn shl(self, other: $f) -> Wrapping<$t> {
Wrapping(self.0.wrapping_shl((other & self::shift_max::$t as $f) as u32))
}
}
forward_ref_binop! { impl Shl, shl for Wrapping<$t>, $f,
forward_ref_binop! { impl const Shl, shl for Wrapping<$t>, $f,
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl ShlAssign<$f> for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const ShlAssign<$f> for Wrapping<$t> {
#[inline]
fn shl_assign(&mut self, other: $f) {
*self = *self << other;
}
}
forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }
forward_ref_op_assign! { impl const ShlAssign, shl_assign for Wrapping<$t>, $f }

#[stable(feature = "rust1", since = "1.0.0")]
impl Shr<$f> for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Shr<$f> for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn shr(self, other: $f) -> Wrapping<$t> {
Wrapping(self.0.wrapping_shr((other & self::shift_max::$t as $f) as u32))
}
}
forward_ref_binop! { impl Shr, shr for Wrapping<$t>, $f,
forward_ref_binop! { impl const Shr, shr for Wrapping<$t>, $f,
#[stable(feature = "wrapping_ref_ops", since = "1.39.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl ShrAssign<$f> for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const ShrAssign<$f> for Wrapping<$t> {
#[inline]
fn shr_assign(&mut self, other: $f) {
*self = *self >> other;
}
}
forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
forward_ref_op_assign! { impl const ShrAssign, shr_assign for Wrapping<$t>, $f }
};
}

@@ -209,49 +217,54 @@ sh_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
macro_rules! wrapping_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Add for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Add for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn add(self, other: Wrapping<$t>) -> Wrapping<$t> {
Wrapping(self.0.wrapping_add(other.0))
}
}
forward_ref_binop! { impl Add, add for Wrapping<$t>, Wrapping<$t>,
forward_ref_binop! { impl const Add, add for Wrapping<$t>, Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl AddAssign for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const AddAssign for Wrapping<$t> {
#[inline]
fn add_assign(&mut self, other: Wrapping<$t>) {
*self = *self + other;
}
}
forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t> }
forward_ref_op_assign! { impl const AddAssign, add_assign for Wrapping<$t>, Wrapping<$t> }

#[stable(feature = "rust1", since = "1.0.0")]
impl Sub for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Sub for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn sub(self, other: Wrapping<$t>) -> Wrapping<$t> {
Wrapping(self.0.wrapping_sub(other.0))
}
}
forward_ref_binop! { impl Sub, sub for Wrapping<$t>, Wrapping<$t>,
forward_ref_binop! { impl const Sub, sub for Wrapping<$t>, Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl SubAssign for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const SubAssign for Wrapping<$t> {
#[inline]
fn sub_assign(&mut self, other: Wrapping<$t>) {
*self = *self - other;
}
}
forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t> }
forward_ref_op_assign! { impl const SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t> }

#[stable(feature = "rust1", since = "1.0.0")]
impl Mul for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Mul for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
@@ -263,140 +276,153 @@ macro_rules! wrapping_impl {
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl MulAssign for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const MulAssign for Wrapping<$t> {
#[inline]
fn mul_assign(&mut self, other: Wrapping<$t>) {
*self = *self * other;
}
}
forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t> }
forward_ref_op_assign! { impl const MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t> }

#[stable(feature = "wrapping_div", since = "1.3.0")]
impl Div for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Div for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn div(self, other: Wrapping<$t>) -> Wrapping<$t> {
Wrapping(self.0.wrapping_div(other.0))
}
}
forward_ref_binop! { impl Div, div for Wrapping<$t>, Wrapping<$t>,
forward_ref_binop! { impl const Div, div for Wrapping<$t>, Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl DivAssign for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const DivAssign for Wrapping<$t> {
#[inline]
fn div_assign(&mut self, other: Wrapping<$t>) {
*self = *self / other;
}
}
forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t> }
forward_ref_op_assign! { impl const DivAssign, div_assign for Wrapping<$t>, Wrapping<$t> }

#[stable(feature = "wrapping_impls", since = "1.7.0")]
impl Rem for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Rem for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn rem(self, other: Wrapping<$t>) -> Wrapping<$t> {
Wrapping(self.0.wrapping_rem(other.0))
}
}
forward_ref_binop! { impl Rem, rem for Wrapping<$t>, Wrapping<$t>,
forward_ref_binop! { impl const Rem, rem for Wrapping<$t>, Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl RemAssign for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const RemAssign for Wrapping<$t> {
#[inline]
fn rem_assign(&mut self, other: Wrapping<$t>) {
*self = *self % other;
}
}
forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t> }
forward_ref_op_assign! { impl const RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t> }

#[stable(feature = "rust1", since = "1.0.0")]
impl Not for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Not for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn not(self) -> Wrapping<$t> {
Wrapping(!self.0)
}
}
forward_ref_unop! { impl Not, not for Wrapping<$t>,
forward_ref_unop! { impl const Not, not for Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "rust1", since = "1.0.0")]
impl BitXor for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitXor for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn bitxor(self, other: Wrapping<$t>) -> Wrapping<$t> {
Wrapping(self.0 ^ other.0)
}
}
forward_ref_binop! { impl BitXor, bitxor for Wrapping<$t>, Wrapping<$t>,
forward_ref_binop! { impl const BitXor, bitxor for Wrapping<$t>, Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl BitXorAssign for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitXorAssign for Wrapping<$t> {
#[inline]
fn bitxor_assign(&mut self, other: Wrapping<$t>) {
*self = *self ^ other;
}
}
forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t> }
forward_ref_op_assign! { impl const BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t> }

#[stable(feature = "rust1", since = "1.0.0")]
impl BitOr for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOr for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn bitor(self, other: Wrapping<$t>) -> Wrapping<$t> {
Wrapping(self.0 | other.0)
}
}
forward_ref_binop! { impl BitOr, bitor for Wrapping<$t>, Wrapping<$t>,
forward_ref_binop! { impl const BitOr, bitor for Wrapping<$t>, Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl BitOrAssign for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOrAssign for Wrapping<$t> {
#[inline]
fn bitor_assign(&mut self, other: Wrapping<$t>) {
*self = *self | other;
}
}
forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t> }
forward_ref_op_assign! { impl const BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t> }

#[stable(feature = "rust1", since = "1.0.0")]
impl BitAnd for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitAnd for Wrapping<$t> {
type Output = Wrapping<$t>;

#[inline]
fn bitand(self, other: Wrapping<$t>) -> Wrapping<$t> {
Wrapping(self.0 & other.0)
}
}
forward_ref_binop! { impl BitAnd, bitand for Wrapping<$t>, Wrapping<$t>,
forward_ref_binop! { impl const BitAnd, bitand for Wrapping<$t>, Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl BitAndAssign for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitAndAssign for Wrapping<$t> {
#[inline]
fn bitand_assign(&mut self, other: Wrapping<$t>) {
*self = *self & other;
}
}
forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t> }
forward_ref_op_assign! { impl const BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t> }

#[stable(feature = "wrapping_neg", since = "1.10.0")]
impl Neg for Wrapping<$t> {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Neg for Wrapping<$t> {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Wrapping(0) - self
}
}
forward_ref_unop! { impl Neg, neg for Wrapping<$t>,
forward_ref_unop! { impl const Neg, neg for Wrapping<$t>,
#[stable(feature = "wrapping_ref", since = "1.14.0")] }

)*)
65 changes: 39 additions & 26 deletions library/core/src/ops/arith.rs
Original file line number Diff line number Diff line change
@@ -92,15 +92,16 @@ pub trait Add<Rhs = Self> {
macro_rules! add_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Add for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Add for $t {
type Output = $t;

#[inline]
#[rustc_inherit_overflow_checks]
fn add(self, other: $t) -> $t { self + other }
}

forward_ref_binop! { impl Add, add for $t, $t }
forward_ref_binop! { impl const Add, add for $t, $t }
)*)
}

@@ -198,15 +199,16 @@ pub trait Sub<Rhs = Self> {
macro_rules! sub_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Sub for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Sub for $t {
type Output = $t;

#[inline]
#[rustc_inherit_overflow_checks]
fn sub(self, other: $t) -> $t { self - other }
}

forward_ref_binop! { impl Sub, sub for $t, $t }
forward_ref_binop! { impl const Sub, sub for $t, $t }
)*)
}

@@ -326,15 +328,16 @@ pub trait Mul<Rhs = Self> {
macro_rules! mul_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Mul for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Mul for $t {
type Output = $t;

#[inline]
#[rustc_inherit_overflow_checks]
fn mul(self, other: $t) -> $t { self * other }
}

forward_ref_binop! { impl Mul, mul for $t, $t }
forward_ref_binop! { impl const Mul, mul for $t, $t }
)*)
}

@@ -464,14 +467,15 @@ macro_rules! div_impl_integer {
///
#[doc = $panic]
#[stable(feature = "rust1", since = "1.0.0")]
impl Div for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Div for $t {
type Output = $t;

#[inline]
fn div(self, other: $t) -> $t { self / other }
}

forward_ref_binop! { impl Div, div for $t, $t }
forward_ref_binop! { impl const Div, div for $t, $t }
)*)*)
}

@@ -483,14 +487,15 @@ div_impl_integer! {
macro_rules! div_impl_float {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Div for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Div for $t {
type Output = $t;

#[inline]
fn div(self, other: $t) -> $t { self / other }
}

forward_ref_binop! { impl Div, div for $t, $t }
forward_ref_binop! { impl const Div, div for $t, $t }
)*)
}

@@ -564,14 +569,15 @@ macro_rules! rem_impl_integer {
///
#[doc = $panic]
#[stable(feature = "rust1", since = "1.0.0")]
impl Rem for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Rem for $t {
type Output = $t;

#[inline]
fn rem(self, other: $t) -> $t { self % other }
}

forward_ref_binop! { impl Rem, rem for $t, $t }
forward_ref_binop! { impl const Rem, rem for $t, $t }
)*)*)
}

@@ -598,14 +604,15 @@ macro_rules! rem_impl_float {
/// assert_eq!(x % y, remainder);
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
impl Rem for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Rem for $t {
type Output = $t;

#[inline]
fn rem(self, other: $t) -> $t { self % other }
}

forward_ref_binop! { impl Rem, rem for $t, $t }
forward_ref_binop! { impl const Rem, rem for $t, $t }
)*)
}

@@ -671,15 +678,16 @@ pub trait Neg {
macro_rules! neg_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Neg for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Neg for $t {
type Output = $t;

#[inline]
#[rustc_inherit_overflow_checks]
fn neg(self) -> $t { -self }
}

forward_ref_unop! { impl Neg, neg for $t }
forward_ref_unop! { impl const Neg, neg for $t }
)*)
}

@@ -739,13 +747,14 @@ pub trait AddAssign<Rhs = Self> {
macro_rules! add_assign_impl {
($($t:ty)+) => ($(
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl AddAssign for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const AddAssign for $t {
#[inline]
#[rustc_inherit_overflow_checks]
fn add_assign(&mut self, other: $t) { *self += other }
}

forward_ref_op_assign! { impl AddAssign, add_assign for $t, $t }
forward_ref_op_assign! { impl const AddAssign, add_assign for $t, $t }
)+)
}

@@ -805,13 +814,14 @@ pub trait SubAssign<Rhs = Self> {
macro_rules! sub_assign_impl {
($($t:ty)+) => ($(
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl SubAssign for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const SubAssign for $t {
#[inline]
#[rustc_inherit_overflow_checks]
fn sub_assign(&mut self, other: $t) { *self -= other }
}

forward_ref_op_assign! { impl SubAssign, sub_assign for $t, $t }
forward_ref_op_assign! { impl const SubAssign, sub_assign for $t, $t }
)+)
}

@@ -862,13 +872,14 @@ pub trait MulAssign<Rhs = Self> {
macro_rules! mul_assign_impl {
($($t:ty)+) => ($(
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl MulAssign for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const MulAssign for $t {
#[inline]
#[rustc_inherit_overflow_checks]
fn mul_assign(&mut self, other: $t) { *self *= other }
}

forward_ref_op_assign! { impl MulAssign, mul_assign for $t, $t }
forward_ref_op_assign! { impl const MulAssign, mul_assign for $t, $t }
)+)
}

@@ -919,12 +930,13 @@ pub trait DivAssign<Rhs = Self> {
macro_rules! div_assign_impl {
($($t:ty)+) => ($(
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl DivAssign for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const DivAssign for $t {
#[inline]
fn div_assign(&mut self, other: $t) { *self /= other }
}

forward_ref_op_assign! { impl DivAssign, div_assign for $t, $t }
forward_ref_op_assign! { impl const DivAssign, div_assign for $t, $t }
)+)
}

@@ -979,12 +991,13 @@ pub trait RemAssign<Rhs = Self> {
macro_rules! rem_assign_impl {
($($t:ty)+) => ($(
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl RemAssign for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const RemAssign for $t {
#[inline]
fn rem_assign(&mut self, other: $t) { *self %= other }
}

forward_ref_op_assign! { impl RemAssign, rem_assign for $t, $t }
forward_ref_op_assign! { impl const RemAssign, rem_assign for $t, $t }
)+)
}

55 changes: 33 additions & 22 deletions library/core/src/ops/bit.rs
Original file line number Diff line number Diff line change
@@ -54,14 +54,15 @@ pub trait Not {
macro_rules! not_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl Not for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Not for $t {
type Output = $t;

#[inline]
fn not(self) -> $t { !self }
}

forward_ref_unop! { impl Not, not for $t }
forward_ref_unop! { impl const Not, not for $t }
)*)
}

@@ -154,14 +155,15 @@ pub trait BitAnd<Rhs = Self> {
macro_rules! bitand_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl BitAnd for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitAnd for $t {
type Output = $t;

#[inline]
fn bitand(self, rhs: $t) -> $t { self & rhs }
}

forward_ref_binop! { impl BitAnd, bitand for $t, $t }
forward_ref_binop! { impl const BitAnd, bitand for $t, $t }
)*)
}

@@ -254,14 +256,15 @@ pub trait BitOr<Rhs = Self> {
macro_rules! bitor_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl BitOr for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOr for $t {
type Output = $t;

#[inline]
fn bitor(self, rhs: $t) -> $t { self | rhs }
}

forward_ref_binop! { impl BitOr, bitor for $t, $t }
forward_ref_binop! { impl const BitOr, bitor for $t, $t }
)*)
}

@@ -354,14 +357,15 @@ pub trait BitXor<Rhs = Self> {
macro_rules! bitxor_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
impl BitXor for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitXor for $t {
type Output = $t;

#[inline]
fn bitxor(self, other: $t) -> $t { self ^ other }
}

forward_ref_binop! { impl BitXor, bitxor for $t, $t }
forward_ref_binop! { impl const BitXor, bitxor for $t, $t }
)*)
}

@@ -451,7 +455,8 @@ pub trait Shl<Rhs = Self> {
macro_rules! shl_impl {
($t:ty, $f:ty) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl Shl<$f> for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Shl<$f> for $t {
type Output = $t;

#[inline]
@@ -461,7 +466,7 @@ macro_rules! shl_impl {
}
}

forward_ref_binop! { impl Shl, shl for $t, $f }
forward_ref_binop! { impl const Shl, shl for $t, $f }
};
}

@@ -569,7 +574,8 @@ pub trait Shr<Rhs = Self> {
macro_rules! shr_impl {
($t:ty, $f:ty) => {
#[stable(feature = "rust1", since = "1.0.0")]
impl Shr<$f> for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const Shr<$f> for $t {
type Output = $t;

#[inline]
@@ -579,7 +585,7 @@ macro_rules! shr_impl {
}
}

forward_ref_binop! { impl Shr, shr for $t, $f }
forward_ref_binop! { impl const Shr, shr for $t, $f }
};
}

@@ -704,12 +710,13 @@ pub trait BitAndAssign<Rhs = Self> {
macro_rules! bitand_assign_impl {
($($t:ty)+) => ($(
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl BitAndAssign for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitAndAssign for $t {
#[inline]
fn bitand_assign(&mut self, other: $t) { *self &= other }
}

forward_ref_op_assign! { impl BitAndAssign, bitand_assign for $t, $t }
forward_ref_op_assign! { impl const BitAndAssign, bitand_assign for $t, $t }
)+)
}

@@ -775,12 +782,13 @@ pub trait BitOrAssign<Rhs = Self> {
macro_rules! bitor_assign_impl {
($($t:ty)+) => ($(
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl BitOrAssign for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitOrAssign for $t {
#[inline]
fn bitor_assign(&mut self, other: $t) { *self |= other }
}

forward_ref_op_assign! { impl BitOrAssign, bitor_assign for $t, $t }
forward_ref_op_assign! { impl const BitOrAssign, bitor_assign for $t, $t }
)+)
}

@@ -846,12 +854,13 @@ pub trait BitXorAssign<Rhs = Self> {
macro_rules! bitxor_assign_impl {
($($t:ty)+) => ($(
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl BitXorAssign for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const BitXorAssign for $t {
#[inline]
fn bitxor_assign(&mut self, other: $t) { *self ^= other }
}

forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for $t, $t }
forward_ref_op_assign! { impl const BitXorAssign, bitxor_assign for $t, $t }
)+)
}

@@ -907,15 +916,16 @@ pub trait ShlAssign<Rhs = Self> {
macro_rules! shl_assign_impl {
($t:ty, $f:ty) => {
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl ShlAssign<$f> for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const ShlAssign<$f> for $t {
#[inline]
#[rustc_inherit_overflow_checks]
fn shl_assign(&mut self, other: $f) {
*self <<= other
}
}

forward_ref_op_assign! { impl ShlAssign, shl_assign for $t, $f }
forward_ref_op_assign! { impl const ShlAssign, shl_assign for $t, $f }
};
}

@@ -989,15 +999,16 @@ pub trait ShrAssign<Rhs = Self> {
macro_rules! shr_assign_impl {
($t:ty, $f:ty) => {
#[stable(feature = "op_assign_traits", since = "1.8.0")]
impl ShrAssign<$f> for $t {
#[rustc_const_unstable(feature = "const_ops", issue = "90080")]
impl const ShrAssign<$f> for $t {
#[inline]
#[rustc_inherit_overflow_checks]
fn shr_assign(&mut self, other: $f) {
*self >>= other
}
}

forward_ref_op_assign! { impl ShrAssign, shr_assign for $t, $f }
forward_ref_op_assign! { impl const ShrAssign, shr_assign for $t, $f }
};
}