]> git.lizzy.rs Git - rust.git/blob - tests/ui/consts/const_let_eq.rs
Rollup merge of #107023 - scottmcm:stop-shouting, r=Nilstrieb
[rust.git] / tests / ui / consts / const_let_eq.rs
1 // run-pass
2
3 struct Foo<T>(T);
4 struct Bar<T> { x: T }
5 struct W(u32);
6 struct A { a: u32 }
7
8 #[allow(redundant_semicolons)]
9 const fn basics((a,): (u32,)) -> u32 {
10     // Deferred assignment:
11     let b: u32;
12     b = a + 1;
13
14     // Immediate assignment:
15     let c: u32 = b + 1;
16
17     // Mutables:
18     let mut d: u32 = c + 1;
19     d = d + 1;
20     // +4 so far.
21
22     // No effect statements work:
23     ; ;
24     1;
25
26     // Array projection
27     let mut arr: [u32; 1] = [0];
28     arr[0] = 1;
29     d = d + arr[0];
30     // +5
31
32     // Field projection:
33     let mut foo: Foo<u32> = Foo(0);
34     let mut bar: Bar<u32> = Bar { x: 0 };
35     foo.0 = 1;
36     bar.x = 1;
37     d = d + foo.0 + bar.x;
38     // +7
39
40     // Array + Field projection:
41     let mut arr: [Foo<u32>; 1] = [Foo(0)];
42     arr[0].0 = 1;
43     d = d + arr[0].0;
44     let mut arr: [Bar<u32>; 1] = [Bar { x: 0 }];
45     arr[0].x = 1;
46     d = d + arr[0].x;
47     // +9
48
49     // Field + Array projection:
50     let mut arr: Foo<[u32; 1]> = Foo([0]);
51     (arr.0)[0] = 1;
52     d = d + (arr.0)[0];
53     let mut arr: Bar<[u32; 1]> = Bar { x: [0] };
54     arr.x[0] = 1;
55     d = d + arr.x[0];
56     // +11
57
58     d
59 }
60
61 const fn add_assign(W(a): W) -> u32 {
62     // Mutables:
63     let mut d: u32 = a + 1;
64     d += 1;
65     // +2 so far.
66
67     // Array projection
68     let mut arr: [u32; 1] = [0];
69     arr[0] += 1;
70     d += arr[0];
71     // +3
72
73     // Field projection:
74     let mut foo: Foo<u32> = Foo(0);
75     let mut bar: Bar<u32> = Bar { x: 0 };
76     foo.0 += 1;
77     bar.x += 1;
78     d += foo.0 + bar.x;
79     // +5
80
81     // Array + Field projection:
82     let mut arr: [Foo<u32>; 1] = [Foo(0)];
83     arr[0].0 += 1;
84     d += arr[0].0;
85     let mut arr: [Bar<u32>; 1] = [Bar { x: 0 }];
86     arr[0].x += 1;
87     d += arr[0].x;
88     // +7
89
90     // Field + Array projection:
91     let mut arr: Foo<[u32; 1]> = Foo([0]);
92     (arr.0)[0] += 1;
93     d += (arr.0)[0];
94     let mut arr: Bar<[u32; 1]> = Bar { x: [0] };
95     arr.x[0] += 1;
96     d += arr.x[0];
97     // +9
98
99     d
100 }
101
102 const fn mul_assign(A { a }: A) -> u32 {
103     // Mutables:
104     let mut d: u32 = a + 1;
105     d *= 2;
106     // 2^1 * (a + 1)
107
108     // Array projection
109     let mut arr: [u32; 1] = [1];
110     arr[0] *= 2;
111     d *= arr[0];
112     // 2^2 * (a + 1)
113
114     // Field projection:
115     let mut foo: Foo<u32> = Foo(1);
116     let mut bar: Bar<u32> = Bar { x: 1 };
117     foo.0 *= 2;
118     bar.x *= 2;
119     d *= foo.0 + bar.x;
120     // 2^4 * (a + 1)
121
122     // Array + Field projection:
123     let mut arr: [Foo<u32>; 1] = [Foo(1)];
124     arr[0].0 *= 2;
125     d *= arr[0].0;
126     let mut arr: [Bar<u32>; 1] = [Bar { x: 1 }];
127     arr[0].x *= 2;
128     d *= arr[0].x;
129     // 2^6 * (a + 1)
130
131     // Field + Array projection:
132     let mut arr: Foo<[u32; 1]> = Foo([1]);
133     (arr.0)[0] *= 2;
134     d *= (arr.0)[0];
135     let mut arr: Bar<[u32; 1]> = Bar { x: [1] };
136     arr.x[0] *= 2;
137     d *= arr.x[0];
138     // 2^8 * (a + 1)
139
140     d
141 }
142
143 const fn div_assign(a: [u32; 1]) -> u32 {
144     let a = a[0];
145     // Mutables:
146     let mut d: u32 = 1024 * a;
147     d /= 2;
148     // 512
149
150     // Array projection
151     let mut arr: [u32; 1] = [4];
152     arr[0] /= 2;
153     d /= arr[0];
154     // 256
155
156     // Field projection:
157     let mut foo: Foo<u32> = Foo(4);
158     let mut bar: Bar<u32> = Bar { x: 4 };
159     foo.0 /= 2;
160     bar.x /= 2;
161     d /= foo.0;
162     d /= bar.x;
163     // 64
164
165     // Array + Field projection:
166     let mut arr: [Foo<u32>; 1] = [Foo(4)];
167     arr[0].0 /= 2;
168     d /= arr[0].0;
169     let mut arr: [Bar<u32>; 1] = [Bar { x: 4 }];
170     arr[0].x /= 2;
171     d /= arr[0].x;
172     // 16
173
174     // Field + Array projection:
175     let mut arr: Foo<[u32; 1]> = Foo([4]);
176     (arr.0)[0] /= 2;
177     d /= (arr.0)[0];
178     let mut arr: Bar<[u32; 1]> = Bar { x: [4] };
179     arr.x[0] /= 2;
180     d /= arr.x[0];
181     // 4
182
183     d
184 }
185
186 const fn rem_assign(W(a): W) -> u32 {
187     // Mutables:
188     let mut d: u32 = a;
189     d %= 10;
190     d += 10;
191
192     // Array projection
193     let mut arr: [u32; 1] = [3];
194     arr[0] %= 2;
195     d %= 9 + arr[0];
196     d += 10;
197
198     // Field projection:
199     let mut foo: Foo<u32> = Foo(5);
200     let mut bar: Bar<u32> = Bar { x: 7 };
201     foo.0 %= 2;
202     bar.x %= 2;
203     d %= 8 + foo.0 + bar.x;
204     d += 10;
205
206     // Array + Field projection:
207     let mut arr: [Foo<u32>; 1] = [Foo(4)];
208     arr[0].0 %= 3;
209     d %= 9 + arr[0].0;
210     d += 10;
211     let mut arr: [Bar<u32>; 1] = [Bar { x: 7 }];
212     arr[0].x %= 3;
213     d %= 9 + arr[0].x;
214     d += 10;
215
216     // Field + Array projection:
217     let mut arr: Foo<[u32; 1]> = Foo([6]);
218     (arr.0)[0] %= 5;
219     d %= 9 + (arr.0)[0];
220     let mut arr: Bar<[u32; 1]> = Bar { x: [11] };
221     arr.x[0] %= 5;
222     d %= 9 + arr.x[0];
223
224     d
225 }
226
227 const fn sub_assign(W(a): W) -> u32 {
228     // Mutables:
229     let mut d: u32 = a;
230     d -= 1;
231
232     // Array projection
233     let mut arr: [u32; 1] = [2];
234     arr[0] -= 1;
235     d -= arr[0];
236
237     // Field projection:
238     let mut foo: Foo<u32> = Foo(2);
239     let mut bar: Bar<u32> = Bar { x: 2 };
240     foo.0 -= 1;
241     bar.x -= 1;
242     d -= foo.0 + bar.x;
243
244     // Array + Field projection:
245     let mut arr: [Foo<u32>; 1] = [Foo(2)];
246     arr[0].0 -= 1;
247     d -= arr[0].0;
248     let mut arr: [Bar<u32>; 1] = [Bar { x: 2 }];
249     arr[0].x -= 1;
250     d -= arr[0].x;
251
252     // Field + Array projection:
253     let mut arr: Foo<[u32; 1]> = Foo([2]);
254     (arr.0)[0] -= 1;
255     d -= (arr.0)[0];
256     let mut arr: Bar<[u32; 1]> = Bar { x: [2] };
257     arr.x[0] -= 1;
258     d -= arr.x[0];
259
260     d
261 }
262
263 const fn shl_assign(W(a): W) -> u32 {
264     // Mutables:
265     let mut d: u32 = a;
266     d <<= 1; // 10
267
268     // Array projection
269     let mut arr: [u32; 1] = [1];
270     arr[0] <<= 1;
271     d <<= arr[0]; // 10 << 2
272
273     // Field projection:
274     let mut foo: Foo<u32> = Foo(1);
275     let mut bar: Bar<u32> = Bar { x: 1 };
276     foo.0 <<= 1;
277     bar.x <<= 1;
278     d <<= foo.0 + bar.x; // 1000 << 4
279
280     // Array + Field projection:
281     let mut arr: [Foo<u32>; 1] = [Foo(1)];
282     arr[0].0 <<= 1;
283     d <<= arr[0].0; // 1000_0000 << 2
284     let mut arr: [Bar<u32>; 1] = [Bar { x: 1 }];
285     arr[0].x <<= 1;
286     d <<= arr[0].x; // 1000_0000_00 << 2
287
288     // Field + Array projection:
289     let mut arr: Foo<[u32; 1]> = Foo([1]);
290     (arr.0)[0] <<= 1;
291     d <<= (arr.0)[0]; // 1000_0000_0000 << 2
292     let mut arr: Bar<[u32; 1]> = Bar { x: [1] };
293     arr.x[0] <<= 1;
294     d <<= arr.x[0]; // 1000_0000_0000_00 << 2
295
296     d
297 }
298
299 const fn shr_assign(W(a): W) -> u32 {
300     // Mutables:
301     let mut d: u32 = a;
302     d >>= 1; // /= 2
303
304     // Array projection
305     let mut arr: [u32; 1] = [2];
306     arr[0] >>= 1;
307     d >>= arr[0]; // /= 4
308
309     // Field projection:
310     let mut foo: Foo<u32> = Foo(2);
311     let mut bar: Bar<u32> = Bar { x: 2 };
312     foo.0 >>= 1;
313     bar.x >>= 1;
314     d >>= foo.0 + bar.x; // /= 16
315
316     // Array + Field projection:
317     let mut arr: [Foo<u32>; 1] = [Foo(2)];
318     arr[0].0 >>= 1;
319     d >>= arr[0].0; // /= 32
320     let mut arr: [Bar<u32>; 1] = [Bar { x: 2 }];
321     arr[0].x >>= 1;
322     d >>= arr[0].x; // /= 64
323
324     // Field + Array projection:
325     let mut arr: Foo<[u32; 1]> = Foo([2]);
326     (arr.0)[0] >>= 1;
327     d >>= (arr.0)[0]; // /= 128
328     let mut arr: Bar<[u32; 1]> = Bar { x: [2] };
329     arr.x[0] >>= 1;
330     d >>= arr.x[0]; // /= 256
331
332     d
333 }
334
335 const fn bit_and_assign(W(a): W) -> u32 {
336     let f = 0b1111_1111_1111_1111;
337
338     // Mutables:
339     let mut d: u32 = a;
340     d &= 0b1111_1111_1111_1110;
341
342     // Array projection
343     let mut arr: [u32; 1] = [f];
344     arr[0] &= 0b1111_1111_1111_1101;
345     d &= arr[0];
346
347     // Field projection:
348     let mut foo: Foo<u32> = Foo(f);
349     let mut bar: Bar<u32> = Bar { x: f };
350     foo.0 &= 0b1111_1111_1111_0111;
351     bar.x &= 0b1111_1111_1101_1111;
352     d &= foo.0 & bar.x;
353
354     // Array + Field projection:
355     let mut arr: [Foo<u32>; 1] = [Foo(f)];
356     arr[0].0 &= 0b1111_1110_1111_1111;
357     d &= arr[0].0;
358     let mut arr: [Bar<u32>; 1] = [Bar { x: f }];
359     arr[0].x &= 0b1111_1101_1111_1111;
360     d &= arr[0].x;
361
362     // Field + Array projection:
363     let mut arr: Foo<[u32; 1]> = Foo([f]);
364     (arr.0)[0] &= 0b1011_1111_1111_1111;
365     d &= (arr.0)[0];
366     let mut arr: Bar<[u32; 1]> = Bar { x: [f] };
367     arr.x[0] &= 0b0111_1111_1111_1111;
368     d &= arr.x[0];
369
370     d
371 }
372
373 const fn bit_or_assign(W(a): W) -> u32 {
374     let f = 0b0000_0000_0000_0000;
375
376     // Mutables:
377     let mut d: u32 = a;
378     d |= 0b0000_0000_0000_0001;
379
380     // Array projection
381     let mut arr: [u32; 1] = [f];
382     arr[0] |= 0b0000_0000_0000_1001;
383     d |= arr[0];
384
385     // Field projection:
386     let mut foo: Foo<u32> = Foo(f);
387     let mut bar: Bar<u32> = Bar { x: f };
388     foo.0 |= 0b0000_0000_0001_0000;
389     bar.x |= 0b0000_0000_0100_0000;
390     d |= foo.0 | bar.x;
391
392     // Array + Field projection:
393     let mut arr: [Foo<u32>; 1] = [Foo(f)];
394     arr[0].0 |= 0b0000_0001_0000_0000;
395     d |= arr[0].0;
396     let mut arr: [Bar<u32>; 1] = [Bar { x: f }];
397     arr[0].x |= 0b0000_0010_0000_0000;
398     d |= arr[0].x;
399
400     // Field + Array projection:
401     let mut arr: Foo<[u32; 1]> = Foo([f]);
402     (arr.0)[0] |= 0b1000_0000_0000_0000;
403     d |= (arr.0)[0]; // /= 128
404     let mut arr: Bar<[u32; 1]> = Bar { x: [f] };
405     arr.x[0] |= 0b1100_0000_0000_0000;
406     d |= arr.x[0]; // /= 256
407
408     d
409 }
410
411 const fn bit_xor_assign(W(a): W) -> u32 {
412     let f = 0b0000_0000_0000_0000;
413
414     // Mutables:
415     let mut d: u32 = a;
416     d ^= 0b0000_0000_0000_0001;
417
418     // Array projection
419     let mut arr: [u32; 1] = [f];
420     arr[0] ^= 0b0000_0000_0000_0010;
421     d ^= arr[0];
422
423     // Field projection:
424     let mut foo: Foo<u32> = Foo(f);
425     let mut bar: Bar<u32> = Bar { x: f };
426     foo.0 ^= 0b0000_0000_0001_0000;
427     bar.x ^= 0b0000_0000_1000_0000;
428     d ^= foo.0 ^ bar.x;
429
430     // Array + Field projection:
431     let mut arr: [Foo<u32>; 1] = [Foo(f)];
432     arr[0].0 ^= 0b0000_0001_0000_0000;
433     d ^= arr[0].0;
434     let mut arr: [Bar<u32>; 1] = [Bar { x: f }];
435     arr[0].x ^= 0b0000_0010_0000_0000;
436     d ^= arr[0].x;
437
438     // Field + Array projection:
439     let mut arr: Foo<[u32; 1]> = Foo([f]);
440     (arr.0)[0] ^= 0b0100_0000_0000_0000;
441     d ^= (arr.0)[0];
442     let mut arr: Bar<[u32; 1]> = Bar { x: [f] };
443     arr.x[0] ^= 0b1000_0000_0000_0000;
444     d ^= arr.x[0];
445
446     d
447 }
448
449 macro_rules! test {
450     ($c:ident, $e:expr, $r:expr) => {
451         const $c: u32 = $e;
452         assert_eq!($c, $r);
453         assert_eq!($e, $r);
454     }
455 }
456
457 fn main() {
458     test!(BASICS, basics((2,)), 13);
459     test!(ADD, add_assign(W(1)), 10);
460     test!(MUL, mul_assign(A { a: 0 }), 256);
461     test!(DIV, div_assign([1]), 4);
462     test!(REM, rem_assign(W(5)), 5);
463     test!(SUB, sub_assign(W(8)), 0);
464     test!(SHL, shl_assign(W(1)), 0b1000_0000_0000_0000);
465     test!(SHR, shr_assign(W(256)), 1);
466     test!(AND, bit_and_assign(W(0b1011_1111_1111_1111_1111)), 0b0011_1100_1101_0100);
467     test!(OR, bit_or_assign(W(0b1011_0000_0000_0000)), 0b1111_0011_0101_1001);
468     test!(XOR, bit_xor_assign(W(0b0000_0000_0000_0000)), 0b1100_0011_1001_0011);
469 }