-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #61812 - jonas-schievink:assoc-ty-defaults, r=nikomatsakis
Implement RFC 2532 – Associated Type Defaults This is a partial implementation that is still missing the changes to object types, since I ran into some trouble while implementing that. I'm opening this part already to get feedback on the implementation and the unexpected test fallout (see my comments below). The remaining changes can be done in a later PR. Blockers before this can land: * [x] Resolve unsoundness around interaction with specialization (#61812 (comment)) - #64564 cc #29661 Fixes #53907 Fixes #54182 Fixes #62211 Fixes #41868 Fixes #63593 Fixes #47385 Fixes #43924 Fixes #32350 Fixes #26681 Fixes #67187
- Loading branch information
Showing
53 changed files
with
2,048 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// build-fail | ||
|
||
// Cyclic assoc. const defaults don't error unless *used* | ||
trait Tr { | ||
const A: u8 = Self::B; | ||
//~^ ERROR cycle detected when const-evaluating + checking `Tr::A` | ||
|
||
const B: u8 = Self::A; | ||
} | ||
|
||
// This impl is *allowed* unless its assoc. consts are used | ||
impl Tr for () {} | ||
|
||
fn main() { | ||
// This triggers the cycle error | ||
assert_eq!(<() as Tr>::A, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
error[E0391]: cycle detected when const-evaluating + checking `Tr::A` | ||
--> $DIR/defaults-cyclic-fail.rs:5:5 | ||
| | ||
LL | const A: u8 = Self::B; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: ...which requires const-evaluating `Tr::A`... | ||
--> $DIR/defaults-cyclic-fail.rs:5:19 | ||
| | ||
LL | const A: u8 = Self::B; | ||
| ^^^^^^^ | ||
note: ...which requires const-evaluating + checking `Tr::B`... | ||
--> $DIR/defaults-cyclic-fail.rs:8:5 | ||
| | ||
LL | const B: u8 = Self::A; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
note: ...which requires const-evaluating `Tr::B`... | ||
--> $DIR/defaults-cyclic-fail.rs:8:19 | ||
| | ||
LL | const B: u8 = Self::A; | ||
| ^^^^^^^ | ||
= note: ...which again requires const-evaluating + checking `Tr::A`, completing the cycle | ||
note: cycle used when const-evaluating `main` | ||
--> $DIR/defaults-cyclic-fail.rs:16:16 | ||
| | ||
LL | assert_eq!(<() as Tr>::A, 0); | ||
| ^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0391`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// run-pass | ||
|
||
// Cyclic assoc. const defaults don't error unless *used* | ||
trait Tr { | ||
const A: u8 = Self::B; | ||
const B: u8 = Self::A; | ||
} | ||
|
||
// This impl is *allowed* unless its assoc. consts are used, matching the | ||
// behavior without defaults. | ||
impl Tr for () {} | ||
|
||
// Overriding either constant breaks the cycle | ||
impl Tr for u8 { | ||
const A: u8 = 42; | ||
} | ||
|
||
impl Tr for u16 { | ||
const B: u8 = 0; | ||
} | ||
|
||
impl Tr for u32 { | ||
const A: u8 = 100; | ||
const B: u8 = 123; | ||
} | ||
|
||
fn main() { | ||
assert_eq!(<u8 as Tr>::A, 42); | ||
assert_eq!(<u8 as Tr>::B, 42); | ||
|
||
assert_eq!(<u16 as Tr>::A, 0); | ||
assert_eq!(<u16 as Tr>::B, 0); | ||
|
||
assert_eq!(<u32 as Tr>::A, 100); | ||
assert_eq!(<u32 as Tr>::B, 123); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// build-fail | ||
|
||
trait Tr { | ||
const A: u8 = 255; | ||
|
||
// This should not be a constant evaluation error (overflow). The value of | ||
// `Self::A` must not be assumed to hold inside the trait. | ||
const B: u8 = Self::A + 1; | ||
//~^ ERROR any use of this value will cause an error | ||
} | ||
|
||
// An impl that doesn't override any constant will NOT cause a const eval error | ||
// just because it's defined, but only if the bad constant is used anywhere. | ||
// This matches the behavior without defaults. | ||
impl Tr for () {} | ||
|
||
// An impl that overrides either constant with a suitable value will be fine. | ||
impl Tr for u8 { | ||
const A: u8 = 254; | ||
} | ||
|
||
impl Tr for u16 { | ||
const B: u8 = 0; | ||
} | ||
|
||
impl Tr for u32 { | ||
const A: u8 = 254; | ||
const B: u8 = 0; | ||
} | ||
|
||
fn main() { | ||
assert_eq!(<() as Tr>::A, 255); | ||
assert_eq!(<() as Tr>::B, 0); // causes the error above | ||
//~^ ERROR evaluation of constant expression failed | ||
//~| ERROR erroneous constant used | ||
|
||
assert_eq!(<u8 as Tr>::A, 254); | ||
assert_eq!(<u8 as Tr>::B, 255); | ||
|
||
assert_eq!(<u16 as Tr>::A, 255); | ||
assert_eq!(<u16 as Tr>::B, 0); | ||
|
||
assert_eq!(<u32 as Tr>::A, 254); | ||
assert_eq!(<u32 as Tr>::B, 0); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/test/ui/associated-const/defaults-not-assumed-fail.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
error: any use of this value will cause an error | ||
--> $DIR/defaults-not-assumed-fail.rs:8:19 | ||
| | ||
LL | const B: u8 = Self::A + 1; | ||
| --------------^^^^^^^^^^^- | ||
| | | ||
| attempt to add with overflow | ||
| | ||
= note: `#[deny(const_err)]` on by default | ||
|
||
error[E0080]: evaluation of constant expression failed | ||
--> $DIR/defaults-not-assumed-fail.rs:33:5 | ||
| | ||
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above | ||
| ^^^^^^^^^^^-------------^^^^^ | ||
| | | ||
| referenced constant has errors | ||
| | ||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: erroneous constant used | ||
--> $DIR/defaults-not-assumed-fail.rs:33:5 | ||
| | ||
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors | ||
| | ||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info) | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0080`. |
Oops, something went wrong.