]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/mir_augmented_assignments.rs
Auto merge of #35856 - phimuemue:master, r=brson
[rust.git] / src / test / run-pass / mir_augmented_assignments.rs
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 use std::mem;
12 use std::ops::{
13     AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, MulAssign, RemAssign,
14     ShlAssign, ShrAssign, SubAssign,
15 };
16
17 #[derive(Debug, PartialEq)]
18 struct Int(i32);
19
20 struct Slice([i32]);
21
22 impl Slice {
23     fn new(slice: &mut [i32]) -> &mut Slice {
24         unsafe {
25             mem::transmute(slice)
26         }
27     }
28 }
29
30 fn main() {
31     main_mir();
32 }
33
34 fn main_mir() {
35     let mut x = Int(1);
36
37     x += Int(2);
38     assert_eq!(x, Int(0b11));
39
40     x &= Int(0b01);
41     assert_eq!(x, Int(0b01));
42
43     x |= Int(0b10);
44     assert_eq!(x, Int(0b11));
45
46     x ^= Int(0b01);
47     assert_eq!(x, Int(0b10));
48
49     x /= Int(2);
50     assert_eq!(x, Int(1));
51
52     x *= Int(3);
53     assert_eq!(x, Int(3));
54
55     x %= Int(2);
56     assert_eq!(x, Int(1));
57
58     // overloaded RHS
59     x <<= 1u8;
60     assert_eq!(x, Int(2));
61
62     x <<= 1u16;
63     assert_eq!(x, Int(4));
64
65     x >>= 1u8;
66     assert_eq!(x, Int(2));
67
68     x >>= 1u16;
69     assert_eq!(x, Int(1));
70
71     x -= Int(1);
72     assert_eq!(x, Int(0));
73
74     // indexed LHS
75     // FIXME(mir-drop): use the vec![..] macro
76     let mut v = Vec::new();
77     v.push(Int(1));
78     v.push(Int(2));
79     v[0] += Int(2);
80     assert_eq!(v[0], Int(3));
81
82     // unsized RHS
83     let mut array = [0, 1, 2];
84     *Slice::new(&mut array) += 1;
85     assert_eq!(array[0], 1);
86     assert_eq!(array[1], 2);
87     assert_eq!(array[2], 3);
88
89 }
90
91 impl AddAssign for Int {
92     fn add_assign(&mut self, rhs: Int) {
93         self.0 += rhs.0;
94     }
95 }
96
97 impl BitAndAssign for Int {
98     fn bitand_assign(&mut self, rhs: Int) {
99         self.0 &= rhs.0;
100     }
101 }
102
103 impl BitOrAssign for Int {
104     fn bitor_assign(&mut self, rhs: Int) {
105         self.0 |= rhs.0;
106     }
107 }
108
109 impl BitXorAssign for Int {
110     fn bitxor_assign(&mut self, rhs: Int) {
111         self.0 ^= rhs.0;
112     }
113 }
114
115 impl DivAssign for Int {
116     fn div_assign(&mut self, rhs: Int) {
117         self.0 /= rhs.0;
118     }
119 }
120
121 impl MulAssign for Int {
122     fn mul_assign(&mut self, rhs: Int) {
123         self.0 *= rhs.0;
124     }
125 }
126
127 impl RemAssign for Int {
128     fn rem_assign(&mut self, rhs: Int) {
129         self.0 %= rhs.0;
130     }
131 }
132
133 impl ShlAssign<u8> for Int {
134     fn shl_assign(&mut self, rhs: u8) {
135         self.0 <<= rhs;
136     }
137 }
138
139 impl ShlAssign<u16> for Int {
140     fn shl_assign(&mut self, rhs: u16) {
141         self.0 <<= rhs;
142     }
143 }
144
145 impl ShrAssign<u8> for Int {
146     fn shr_assign(&mut self, rhs: u8) {
147         self.0 >>= rhs;
148     }
149 }
150
151 impl ShrAssign<u16> for Int {
152     fn shr_assign(&mut self, rhs: u16) {
153         self.0 >>= rhs;
154     }
155 }
156
157 impl SubAssign for Int {
158     fn sub_assign(&mut self, rhs: Int) {
159         self.0 -= rhs.0;
160     }
161 }
162
163 impl AddAssign<i32> for Slice {
164     fn add_assign(&mut self, rhs: i32) {
165         for lhs in &mut self.0 {
166             *lhs += rhs;
167         }
168     }
169 }