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