Skip to content

Commit de447eb

Browse files
Add tests for assoc. const defaults
1 parent 37686ed commit de447eb

File tree

6 files changed

+182
-0
lines changed

6 files changed

+182
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// compile-fail
2+
3+
// Cyclic assoc. const defaults don't error unless *used*
4+
trait Tr {
5+
const A: u8 = Self::B;
6+
//~^ ERROR cycle detected when const-evaluating `Tr::A`
7+
8+
const B: u8 = Self::A;
9+
}
10+
11+
// This impl is *allowed* unless its assoc. consts are used
12+
impl Tr for () {}
13+
14+
fn main() {
15+
// This triggers the cycle error
16+
assert_eq!(<() as Tr>::A, 0);
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0391]: cycle detected when const-evaluating `Tr::A`
2+
--> $DIR/defaults-cyclic-fail.rs:5:19
3+
|
4+
LL | const A: u8 = Self::B;
5+
| ^^^^^^^
6+
|
7+
note: ...which requires const-evaluating `Tr::B`...
8+
--> $DIR/defaults-cyclic-fail.rs:8:19
9+
|
10+
LL | const B: u8 = Self::A;
11+
| ^^^^^^^
12+
= note: ...which again requires const-evaluating `Tr::A`, completing the cycle
13+
note: cycle used when processing `main`
14+
--> $DIR/defaults-cyclic-fail.rs:16:16
15+
|
16+
LL | assert_eq!(<() as Tr>::A, 0);
17+
| ^^^^^^^^^^^^^
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0391`.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// run-pass
2+
3+
// Cyclic assoc. const defaults don't error unless *used*
4+
trait Tr {
5+
const A: u8 = Self::B;
6+
const B: u8 = Self::A;
7+
}
8+
9+
// This impl is *allowed* unless its assoc. consts are used
10+
impl Tr for () {}
11+
12+
// Overriding either constant breaks the cycle
13+
impl Tr for u8 {
14+
const A: u8 = 42;
15+
}
16+
17+
impl Tr for u16 {
18+
const B: u8 = 0;
19+
}
20+
21+
impl Tr for u32 {
22+
const A: u8 = 100;
23+
const B: u8 = 123;
24+
}
25+
26+
fn main() {
27+
assert_eq!(<u8 as Tr>::A, 42);
28+
assert_eq!(<u8 as Tr>::B, 42);
29+
30+
assert_eq!(<u16 as Tr>::A, 0);
31+
assert_eq!(<u16 as Tr>::B, 0);
32+
33+
assert_eq!(<u32 as Tr>::A, 100);
34+
assert_eq!(<u32 as Tr>::B, 123);
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// compile-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+
}
11+
12+
// An impl that doesn't override any constant will NOT cause a const eval error
13+
// just because it's defined, but only if the bad constant is used anywhere.
14+
// This matches the behavior without defaults.
15+
impl Tr for () {}
16+
17+
// An impl that overrides either constant with a suitable value will be fine.
18+
impl Tr for u8 {
19+
const A: u8 = 254;
20+
}
21+
22+
impl Tr for u16 {
23+
const B: u8 = 0;
24+
}
25+
26+
impl Tr for u32 {
27+
const A: u8 = 254;
28+
const B: u8 = 0;
29+
}
30+
31+
fn main() {
32+
assert_eq!(<() as Tr>::A, 255);
33+
assert_eq!(<() as Tr>::B, 0); // causes the error above
34+
//~^ ERROR evaluation of constant expression failed
35+
36+
assert_eq!(<u8 as Tr>::A, 254);
37+
assert_eq!(<u8 as Tr>::B, 255);
38+
39+
assert_eq!(<u16 as Tr>::A, 255);
40+
assert_eq!(<u16 as Tr>::B, 0);
41+
42+
assert_eq!(<u32 as Tr>::A, 254);
43+
assert_eq!(<u32 as Tr>::B, 0);
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
error: any use of this value will cause an error
2+
--> $DIR/defaults-not-assumed-fail.rs:8:19
3+
|
4+
LL | const B: u8 = Self::A + 1;
5+
| --------------^^^^^^^^^^^-
6+
| |
7+
| attempt to add with overflow
8+
|
9+
= note: #[deny(const_err)] on by default
10+
11+
error[E0080]: evaluation of constant expression failed
12+
--> $DIR/defaults-not-assumed-fail.rs:33:5
13+
|
14+
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
15+
| ^^^^^^^^^^^-------------^^^^^
16+
| |
17+
| referenced constant has errors
18+
|
19+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
20+
21+
error: aborting due to 2 previous errors
22+
23+
For more information about this error, try `rustc --explain E0080`.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// run-pass
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+
}
10+
11+
// An impl that doesn't override any constant will NOT cause a const eval error
12+
// just because it's defined, but only if the bad constant is used anywhere.
13+
// This matches the behavior without defaults.
14+
impl Tr for () {}
15+
16+
// An impl that overrides either constant with a suitable value will be fine.
17+
impl Tr for u8 {
18+
const A: u8 = 254;
19+
}
20+
21+
impl Tr for u16 {
22+
const B: u8 = 0;
23+
}
24+
25+
impl Tr for u32 {
26+
const A: u8 = 254;
27+
const B: u8 = 0;
28+
}
29+
30+
fn main() {
31+
assert_eq!(<() as Tr>::A, 255);
32+
//assert_eq!(<() as Tr>::B, 0); // using this is an error
33+
34+
assert_eq!(<u8 as Tr>::A, 254);
35+
assert_eq!(<u8 as Tr>::B, 255);
36+
37+
assert_eq!(<u16 as Tr>::A, 255);
38+
assert_eq!(<u16 as Tr>::B, 0);
39+
40+
assert_eq!(<u32 as Tr>::A, 254);
41+
assert_eq!(<u32 as Tr>::B, 0);
42+
}

0 commit comments

Comments
 (0)