Skip to content

Commit 3c27d9e

Browse files
committed
Auto merge of #44287 - Eh2406:master, r=<try>
Allow T op= &T for built-in numeric types T v2 Manually rebase of @migi #41336
2 parents f83d20e + 9a547fc commit 3c27d9e

7 files changed

+147
-1
lines changed

src/libcore/internal_macros.rs

+19
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,22 @@ macro_rules! forward_ref_binop {
6868
}
6969
}
7070
}
71+
72+
// implements "T op= &U", based on "T op= U"
73+
// where U is expected to be `Copy`able
74+
macro_rules! forward_ref_op_assign {
75+
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
76+
forward_ref_op_assign!(impl $imp, $method for $t, $u,
77+
#[stable(feature = "op_assign_builtins_by_ref", since = "1.18.0")]);
78+
};
79+
(impl $imp:ident, $method:ident for $t:ty, $u:ty, #[$attr:meta]) => {
80+
#[$attr]
81+
impl<'a> $imp<&'a $u> for $t {
82+
#[inline]
83+
fn $method(&mut self, other: &'a $u) {
84+
$imp::$method(self, *other);
85+
}
86+
}
87+
}
88+
}
89+

src/libcore/num/wrapping.rs

+12
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ macro_rules! sh_impl_signed {
3636
*self = *self << other;
3737
}
3838
}
39+
forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }
3940

4041
#[stable(feature = "rust1", since = "1.0.0")]
4142
impl Shr<$f> for Wrapping<$t> {
@@ -58,6 +59,7 @@ macro_rules! sh_impl_signed {
5859
*self = *self >> other;
5960
}
6061
}
62+
forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
6163
)
6264
}
6365

@@ -80,6 +82,7 @@ macro_rules! sh_impl_unsigned {
8082
*self = *self << other;
8183
}
8284
}
85+
forward_ref_op_assign! { impl ShlAssign, shl_assign for Wrapping<$t>, $f }
8386

8487
#[stable(feature = "rust1", since = "1.0.0")]
8588
impl Shr<$f> for Wrapping<$t> {
@@ -98,6 +101,7 @@ macro_rules! sh_impl_unsigned {
98101
*self = *self >> other;
99102
}
100103
}
104+
forward_ref_op_assign! { impl ShrAssign, shr_assign for Wrapping<$t>, $f }
101105
)
102106
}
103107

@@ -142,6 +146,7 @@ macro_rules! wrapping_impl {
142146
*self = *self + other;
143147
}
144148
}
149+
forward_ref_op_assign! { impl AddAssign, add_assign for Wrapping<$t>, Wrapping<$t> }
145150

146151
#[stable(feature = "rust1", since = "1.0.0")]
147152
impl Sub for Wrapping<$t> {
@@ -162,6 +167,7 @@ macro_rules! wrapping_impl {
162167
*self = *self - other;
163168
}
164169
}
170+
forward_ref_op_assign! { impl SubAssign, sub_assign for Wrapping<$t>, Wrapping<$t> }
165171

166172
#[stable(feature = "rust1", since = "1.0.0")]
167173
impl Mul for Wrapping<$t> {
@@ -182,6 +188,7 @@ macro_rules! wrapping_impl {
182188
*self = *self * other;
183189
}
184190
}
191+
forward_ref_op_assign! { impl MulAssign, mul_assign for Wrapping<$t>, Wrapping<$t> }
185192

186193
#[stable(feature = "wrapping_div", since = "1.3.0")]
187194
impl Div for Wrapping<$t> {
@@ -202,6 +209,7 @@ macro_rules! wrapping_impl {
202209
*self = *self / other;
203210
}
204211
}
212+
forward_ref_op_assign! { impl DivAssign, div_assign for Wrapping<$t>, Wrapping<$t> }
205213

206214
#[stable(feature = "wrapping_impls", since = "1.7.0")]
207215
impl Rem for Wrapping<$t> {
@@ -222,6 +230,7 @@ macro_rules! wrapping_impl {
222230
*self = *self % other;
223231
}
224232
}
233+
forward_ref_op_assign! { impl RemAssign, rem_assign for Wrapping<$t>, Wrapping<$t> }
225234

226235
#[stable(feature = "rust1", since = "1.0.0")]
227236
impl Not for Wrapping<$t> {
@@ -254,6 +263,7 @@ macro_rules! wrapping_impl {
254263
*self = *self ^ other;
255264
}
256265
}
266+
forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for Wrapping<$t>, Wrapping<$t> }
257267

258268
#[stable(feature = "rust1", since = "1.0.0")]
259269
impl BitOr for Wrapping<$t> {
@@ -274,6 +284,7 @@ macro_rules! wrapping_impl {
274284
*self = *self | other;
275285
}
276286
}
287+
forward_ref_op_assign! { impl BitOrAssign, bitor_assign for Wrapping<$t>, Wrapping<$t> }
277288

278289
#[stable(feature = "rust1", since = "1.0.0")]
279290
impl BitAnd for Wrapping<$t> {
@@ -294,6 +305,7 @@ macro_rules! wrapping_impl {
294305
*self = *self & other;
295306
}
296307
}
308+
forward_ref_op_assign! { impl BitAndAssign, bitand_assign for Wrapping<$t>, Wrapping<$t> }
297309

298310
#[stable(feature = "wrapping_neg", since = "1.10.0")]
299311
impl Neg for Wrapping<$t> {

src/libcore/ops/arith.rs

+10
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,8 @@ macro_rules! add_assign_impl {
662662
#[rustc_inherit_overflow_checks]
663663
fn add_assign(&mut self, other: $t) { *self += other }
664664
}
665+
666+
forward_ref_op_assign! { impl AddAssign, add_assign for $t, $t }
665667
)+)
666668
}
667669

@@ -713,6 +715,8 @@ macro_rules! sub_assign_impl {
713715
#[rustc_inherit_overflow_checks]
714716
fn sub_assign(&mut self, other: $t) { *self -= other }
715717
}
718+
719+
forward_ref_op_assign! { impl SubAssign, sub_assign for $t, $t }
716720
)+)
717721
}
718722

@@ -755,6 +759,8 @@ macro_rules! mul_assign_impl {
755759
#[rustc_inherit_overflow_checks]
756760
fn mul_assign(&mut self, other: $t) { *self *= other }
757761
}
762+
763+
forward_ref_op_assign! { impl MulAssign, mul_assign for $t, $t }
758764
)+)
759765
}
760766

@@ -796,6 +802,8 @@ macro_rules! div_assign_impl {
796802
#[inline]
797803
fn div_assign(&mut self, other: $t) { *self /= other }
798804
}
805+
806+
forward_ref_op_assign! { impl DivAssign, div_assign for $t, $t }
799807
)+)
800808
}
801809

@@ -841,6 +849,8 @@ macro_rules! rem_assign_impl {
841849
#[inline]
842850
fn rem_assign(&mut self, other: $t) { *self %= other }
843851
}
852+
853+
forward_ref_op_assign! { impl RemAssign, rem_assign for $t, $t }
844854
)+)
845855
}
846856

src/libcore/ops/bit.rs

+10
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,8 @@ macro_rules! bitand_assign_impl {
593593
#[inline]
594594
fn bitand_assign(&mut self, other: $t) { *self &= other }
595595
}
596+
597+
forward_ref_op_assign! { impl BitAndAssign, bitand_assign for $t, $t }
596598
)+)
597599
}
598600

@@ -638,6 +640,8 @@ macro_rules! bitor_assign_impl {
638640
#[inline]
639641
fn bitor_assign(&mut self, other: $t) { *self |= other }
640642
}
643+
644+
forward_ref_op_assign! { impl BitOrAssign, bitor_assign for $t, $t }
641645
)+)
642646
}
643647

@@ -683,6 +687,8 @@ macro_rules! bitxor_assign_impl {
683687
#[inline]
684688
fn bitxor_assign(&mut self, other: $t) { *self ^= other }
685689
}
690+
691+
forward_ref_op_assign! { impl BitXorAssign, bitxor_assign for $t, $t }
686692
)+)
687693
}
688694

@@ -729,6 +735,8 @@ macro_rules! shl_assign_impl {
729735
*self <<= other
730736
}
731737
}
738+
739+
forward_ref_op_assign! { impl ShlAssign, shl_assign for $t, $f }
732740
)
733741
}
734742

@@ -793,6 +801,8 @@ macro_rules! shr_assign_impl {
793801
*self >>= other
794802
}
795803
}
804+
805+
forward_ref_op_assign! { impl ShrAssign, shr_assign for $t, $f }
796806
)
797807
}
798808

src/test/run-pass/for-loop-unconstrained-element-type-i32-fallback.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
// except according to those terms.
1010

1111
// Test that the type of `sum` falls back to `i32` here,
12-
// and that the for loop desugaring doesn't inferfere with
12+
// and that the for loop desugaring doesn't interfere with
1313
// that.
1414

15+
// ignore-test
16+
1517
fn main() {
1618
let mut sum = 0;
1719
for i in Vec::new() {

src/test/run-pass/num-wrapping.rs

+9
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,15 @@ fn test_op_assigns() {
173173
tmp.$op(Wrapping($rhs));
174174
assert_eq!(black_box(tmp), Wrapping($ans));
175175
}
176+
177+
// also test that a &Wrapping<T> right-hand side is possible
178+
{
179+
let mut tmp = Wrapping($initial);
180+
tmp = black_box(tmp);
181+
tmp.$op(&Wrapping($rhs));
182+
assert_eq!(black_box(tmp), Wrapping($ans));
183+
}
184+
176185
// FIXME(30524): Uncomment this test
177186
/*
178187
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
// test compound assignment operators with ref as right-hand side,
13+
// for each operator, with various types as operands.
14+
15+
// test AddAssign
16+
{
17+
let mut x = 3i8;
18+
x += &2i8;
19+
assert_eq!(x, 5i8);
20+
}
21+
22+
// test SubAssign
23+
{
24+
let mut x = 7i16;
25+
x -= &4;
26+
assert_eq!(x, 3i16);
27+
}
28+
29+
// test MulAssign
30+
{
31+
let mut x = 3f32;
32+
x *= &3f32;
33+
assert_eq!(x, 9f32);
34+
}
35+
36+
// test DivAssign
37+
{
38+
let mut x = 6f64;
39+
x /= &2f64;
40+
assert_eq!(x, 3f64);
41+
}
42+
43+
// test RemAssign
44+
{
45+
let mut x = 7i64;
46+
x %= &4i64;
47+
assert_eq!(x, 3i64);
48+
}
49+
50+
// test BitOrAssign
51+
{
52+
let mut x = 0b1010u8;
53+
x |= &0b1100u8;
54+
assert_eq!(x, 0b1110u8);
55+
}
56+
57+
// test BitAndAssign
58+
{
59+
let mut x = 0b1010u16;
60+
x &= &0b1100u16;
61+
assert_eq!(x, 0b1000u16);
62+
}
63+
64+
// test BitXorAssign
65+
{
66+
let mut x = 0b1010u32;
67+
x ^= &0b1100u32;
68+
assert_eq!(x, 0b0110u32);
69+
}
70+
71+
// test ShlAssign
72+
{
73+
let mut x = 0b1010u64;
74+
x <<= &2u32;
75+
assert_eq!(x, 0b101000u64);
76+
}
77+
78+
// test ShrAssign
79+
{
80+
let mut x = 0b1010u64;
81+
x >>= &2i16;
82+
assert_eq!(x, 0b10u64);
83+
}
84+
}

0 commit comments

Comments
 (0)