]>
git.proxmox.com Git - rustc.git/blob - tests/ui/consts/const_let_eq.rs
8 #[allow(redundant_semicolons)]
9 const fn basics((a
,): (u32,)) -> u32 {
10 // Deferred assignment:
14 // Immediate assignment:
18 let mut d
: u32 = c
+ 1;
22 // No effect statements work:
27 let mut arr
: [u32; 1] = [0];
33 let mut foo
: Foo
<u32> = Foo(0);
34 let mut bar
: Bar
<u32> = Bar { x: 0 }
;
37 d
= d
+ foo
.0 + bar
.x
;
40 // Array + Field projection:
41 let mut arr
: [Foo
<u32>; 1] = [Foo(0)];
44 let mut arr
: [Bar
<u32>; 1] = [Bar { x: 0 }
];
49 // Field + Array projection:
50 let mut arr
: Foo
<[u32; 1]> = Foo([0]);
53 let mut arr
: Bar
<[u32; 1]> = Bar { x: [0] }
;
61 const fn add_assign(W(a
): W
) -> u32 {
63 let mut d
: u32 = a
+ 1;
68 let mut arr
: [u32; 1] = [0];
74 let mut foo
: Foo
<u32> = Foo(0);
75 let mut bar
: Bar
<u32> = Bar { x: 0 }
;
81 // Array + Field projection:
82 let mut arr
: [Foo
<u32>; 1] = [Foo(0)];
85 let mut arr
: [Bar
<u32>; 1] = [Bar { x: 0 }
];
90 // Field + Array projection:
91 let mut arr
: Foo
<[u32; 1]> = Foo([0]);
94 let mut arr
: Bar
<[u32; 1]> = Bar { x: [0] }
;
102 const fn mul_assign(A { a }
: A
) -> u32 {
104 let mut d
: u32 = a
+ 1;
109 let mut arr
: [u32; 1] = [1];
115 let mut foo
: Foo
<u32> = Foo(1);
116 let mut bar
: Bar
<u32> = Bar { x: 1 }
;
122 // Array + Field projection:
123 let mut arr
: [Foo
<u32>; 1] = [Foo(1)];
126 let mut arr
: [Bar
<u32>; 1] = [Bar { x: 1 }
];
131 // Field + Array projection:
132 let mut arr
: Foo
<[u32; 1]> = Foo([1]);
135 let mut arr
: Bar
<[u32; 1]> = Bar { x: [1] }
;
143 const fn div_assign(a
: [u32; 1]) -> u32 {
146 let mut d
: u32 = 1024 * a
;
151 let mut arr
: [u32; 1] = [4];
157 let mut foo
: Foo
<u32> = Foo(4);
158 let mut bar
: Bar
<u32> = Bar { x: 4 }
;
165 // Array + Field projection:
166 let mut arr
: [Foo
<u32>; 1] = [Foo(4)];
169 let mut arr
: [Bar
<u32>; 1] = [Bar { x: 4 }
];
174 // Field + Array projection:
175 let mut arr
: Foo
<[u32; 1]> = Foo([4]);
178 let mut arr
: Bar
<[u32; 1]> = Bar { x: [4] }
;
186 const fn rem_assign(W(a
): W
) -> u32 {
193 let mut arr
: [u32; 1] = [3];
199 let mut foo
: Foo
<u32> = Foo(5);
200 let mut bar
: Bar
<u32> = Bar { x: 7 }
;
203 d
%= 8 + foo
.0 + bar
.x
;
206 // Array + Field projection:
207 let mut arr
: [Foo
<u32>; 1] = [Foo(4)];
211 let mut arr
: [Bar
<u32>; 1] = [Bar { x: 7 }
];
216 // Field + Array projection:
217 let mut arr
: Foo
<[u32; 1]> = Foo([6]);
220 let mut arr
: Bar
<[u32; 1]> = Bar { x: [11] }
;
227 const fn sub_assign(W(a
): W
) -> u32 {
233 let mut arr
: [u32; 1] = [2];
238 let mut foo
: Foo
<u32> = Foo(2);
239 let mut bar
: Bar
<u32> = Bar { x: 2 }
;
244 // Array + Field projection:
245 let mut arr
: [Foo
<u32>; 1] = [Foo(2)];
248 let mut arr
: [Bar
<u32>; 1] = [Bar { x: 2 }
];
252 // Field + Array projection:
253 let mut arr
: Foo
<[u32; 1]> = Foo([2]);
256 let mut arr
: Bar
<[u32; 1]> = Bar { x: [2] }
;
263 const fn shl_assign(W(a
): W
) -> u32 {
269 let mut arr
: [u32; 1] = [1];
271 d
<<= arr
[0]; // 10 << 2
274 let mut foo
: Foo
<u32> = Foo(1);
275 let mut bar
: Bar
<u32> = Bar { x: 1 }
;
278 d
<<= foo
.0 + bar
.x
; // 1000 << 4
280 // Array + Field projection:
281 let mut arr
: [Foo
<u32>; 1] = [Foo(1)];
283 d
<<= arr
[0].0; // 1000_0000 << 2
284 let mut arr
: [Bar
<u32>; 1] = [Bar { x: 1 }
];
286 d
<<= arr
[0].x
; // 1000_0000_00 << 2
288 // Field + Array projection:
289 let mut arr
: Foo
<[u32; 1]> = Foo([1]);
291 d
<<= (arr
.0)[0]; // 1000_0000_0000 << 2
292 let mut arr
: Bar
<[u32; 1]> = Bar { x: [1] }
;
294 d
<<= arr
.x
[0]; // 1000_0000_0000_00 << 2
299 const fn shr_assign(W(a
): W
) -> u32 {
305 let mut arr
: [u32; 1] = [2];
307 d
>>= arr
[0]; // /= 4
310 let mut foo
: Foo
<u32> = Foo(2);
311 let mut bar
: Bar
<u32> = Bar { x: 2 }
;
314 d
>>= foo
.0 + bar
.x
; // /= 16
316 // Array + Field projection:
317 let mut arr
: [Foo
<u32>; 1] = [Foo(2)];
319 d
>>= arr
[0].0; // /= 32
320 let mut arr
: [Bar
<u32>; 1] = [Bar { x: 2 }
];
322 d
>>= arr
[0].x
; // /= 64
324 // Field + Array projection:
325 let mut arr
: Foo
<[u32; 1]> = Foo([2]);
327 d
>>= (arr
.0)[0]; // /= 128
328 let mut arr
: Bar
<[u32; 1]> = Bar { x: [2] }
;
330 d
>>= arr
.x
[0]; // /= 256
335 const fn bit_and_assign(W(a
): W
) -> u32 {
336 let f
= 0b1111_1111_1111_1111;
340 d
&= 0b1111_1111_1111_1110;
343 let mut arr
: [u32; 1] = [f
];
344 arr
[0] &= 0b1111_1111_1111_1101;
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;
354 // Array + Field projection:
355 let mut arr
: [Foo
<u32>; 1] = [Foo(f
)];
356 arr
[0].0 &= 0b1111_1110_1111_1111;
358 let mut arr
: [Bar
<u32>; 1] = [Bar { x: f }
];
359 arr
[0].x
&= 0b1111_1101_1111_1111;
362 // Field + Array projection:
363 let mut arr
: Foo
<[u32; 1]> = Foo([f
]);
364 (arr
.0)[0] &= 0b1011_1111_1111_1111;
366 let mut arr
: Bar
<[u32; 1]> = Bar { x: [f] }
;
367 arr
.x
[0] &= 0b0111_1111_1111_1111;
373 const fn bit_or_assign(W(a
): W
) -> u32 {
374 let f
= 0b0000_0000_0000_0000;
378 d
|= 0b0000_0000_0000_0001;
381 let mut arr
: [u32; 1] = [f
];
382 arr
[0] |= 0b0000_0000_0000_1001;
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;
392 // Array + Field projection:
393 let mut arr
: [Foo
<u32>; 1] = [Foo(f
)];
394 arr
[0].0 |= 0b0000_0001_0000_0000;
396 let mut arr
: [Bar
<u32>; 1] = [Bar { x: f }
];
397 arr
[0].x
|= 0b0000_0010_0000_0000;
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
411 const fn bit_xor_assign(W(a
): W
) -> u32 {
412 let f
= 0b0000_0000_0000_0000;
416 d ^
= 0b0000_0000_0000_0001;
419 let mut arr
: [u32; 1] = [f
];
420 arr
[0] ^
= 0b0000_0000_0000_0010;
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;
430 // Array + Field projection:
431 let mut arr
: [Foo
<u32>; 1] = [Foo(f
)];
432 arr
[0].0 ^
= 0b0000_0001_0000_0000;
434 let mut arr
: [Bar
<u32>; 1] = [Bar { x: f }
];
435 arr
[0].x ^
= 0b0000_0010_0000_0000;
438 // Field + Array projection:
439 let mut arr
: Foo
<[u32; 1]> = Foo([f
]);
440 (arr
.0)[0] ^
= 0b0100_0000_0000_0000;
442 let mut arr
: Bar
<[u32; 1]> = Bar { x: [f] }
;
443 arr
.x
[0] ^
= 0b1000_0000_0000_0000;
450 ($c
:ident
, $e
:expr
, $r
:expr
) => {
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);