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