]> git.lizzy.rs Git - rust.git/blob - src/test/ui/associated-consts/defaults-not-assumed-fail.rs
Auto merge of #99612 - yanchen4791:issue-95079-fix, r=compiler-errors
[rust.git] / src / test / ui / associated-consts / defaults-not-assumed-fail.rs
1 // build-fail
2
3 trait Tr {
4     const A: u8 = 255;
5
6     // This should not be a constant evaluation error (overflow). The value of
7     // `Self::A` must not be assumed to hold inside the trait.
8     const B: u8 = Self::A + 1;
9     //~^ ERROR any use of this value will cause an error
10     //~| WARN this was previously accepted by the compiler but is being phased out
11 }
12
13 // An impl that doesn't override any constant will NOT cause a const eval error
14 // just because it's defined, but only if the bad constant is used anywhere.
15 // This matches the behavior without defaults.
16 impl Tr for () {}
17
18 // An impl that overrides either constant with a suitable value will be fine.
19 impl Tr for u8 {
20     const A: u8 = 254;
21 }
22
23 impl Tr for u16 {
24     const B: u8 = 0;
25 }
26
27 impl Tr for u32 {
28     const A: u8 = 254;
29     const B: u8 = 0;
30 }
31
32 fn main() {
33     assert_eq!(<() as Tr>::A, 255);
34     assert_eq!(<() as Tr>::B, 0);    // causes the error above
35     //~^ ERROR evaluation of constant value failed
36     //~| ERROR erroneous constant used
37     //~| WARN this was previously accepted by the compiler but is being phased out
38
39     assert_eq!(<u8 as Tr>::A, 254);
40     assert_eq!(<u8 as Tr>::B, 255);
41
42     assert_eq!(<u16 as Tr>::A, 255);
43     assert_eq!(<u16 as Tr>::B, 0);
44
45     assert_eq!(<u32 as Tr>::A, 254);
46     assert_eq!(<u32 as Tr>::B, 0);
47 }