-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Implement RFC 2532 – Associated Type Defaults #61812
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
a323ff2
Implement RFC 2532 – Associated Type Defaults
jonas-schievink a549bbd
Add regression test for #54182
jonas-schievink 37686ed
Add comments and assertions to tests
jonas-schievink de447eb
Add tests for assoc. const defaults
jonas-schievink 1e3c020
Test interactions with specialization
jonas-schievink d3be26d
Improve test
jonas-schievink c73ee98
Check suitability of the provided default
jonas-schievink f403882
Add a `Self: Sized` bound
jonas-schievink 5e9317a
Put the check into its own function
jonas-schievink ff5d11e
Add comments and tests explaining the shallow substitution rule
jonas-schievink 07ad64f
Add tests for #62211
jonas-schievink fead458
Fix tests after rebase
jonas-schievink 485111c
Add regression tests for issues
jonas-schievink 1f61f36
Add comment about the shallow subst rule
jonas-schievink fd28614
Add regression test for #26681
jonas-schievink c964520
Update tests after compiletest changes
jonas-schievink 3f03d95
Improve associated-types-overridden-default.rs
jonas-schievink f408794
Improve defaults-in-other-trait-items-pass
jonas-schievink fbcd136
Improve the cycle tests
jonas-schievink c8da9ee
Improve specialization test
jonas-schievink 24ec364
Test mixed default and non-default
jonas-schievink f94eaea
Fix rebase damage
jonas-schievink 708f053
Add test for #65774
jonas-schievink a01846f
appease tidy
jonas-schievink ec50190
Bless test output
jonas-schievink 232c1f3
Format code
jonas-schievink 9930e1f
Fix tests that fail with `--emit metadata`
jonas-schievink 4d4da92
Fix rebase damage
jonas-schievink af2931b
Mark E0399 test as obsolete
jonas-schievink c605831
Fix rebase fallout
jonas-schievink 6cc268b
Mark E0399.md as obsolete
jonas-schievink File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Implement RFC 2532 – Associated Type Defaults
- Loading branch information
commit a323ff2c864801fdc8e044e88f11efb49a565ed1
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
9 changes: 0 additions & 9 deletions
9
src/test/ui/associated-types/associated-types-overridden-default.stderr
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,35 @@ | ||
#![feature(associated_type_defaults)] | ||
|
||
// Having a cycle in assoc. type defaults is okay... | ||
trait Tr { | ||
type A = Self::B; | ||
type B = Self::A; | ||
} | ||
|
||
// ...but is an error in any impl that doesn't override at least one of the defaults | ||
impl Tr for () {} | ||
//~^ ERROR overflow evaluating the requirement | ||
|
||
// As soon as at least one is redefined, it works: | ||
impl Tr for u8 { | ||
type A = u8; | ||
} | ||
|
||
impl Tr for u32 { | ||
type A = (); | ||
type B = u8; | ||
} | ||
|
||
// ...but only if this actually breaks the cycle | ||
impl Tr for bool { | ||
//~^ ERROR overflow evaluating the requirement | ||
type A = Box<Self::B>; | ||
//~^ ERROR overflow evaluating the requirement | ||
} | ||
// (the error is shown twice for some reason) | ||
|
||
fn main() { | ||
// Check that the overridden type propagates to the other | ||
let _a: <u8 as Tr>::A = 0u8; | ||
let _b: <u8 as Tr>::B = 0u8; | ||
} |
This file contains hidden or 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,21 @@ | ||
error[E0275]: overflow evaluating the requirement `<() as Tr>::B` | ||
--> $DIR/defaults-cyclic-fail.rs:10:6 | ||
| | ||
LL | impl Tr for () {} | ||
| ^^ | ||
|
||
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B` | ||
--> $DIR/defaults-cyclic-fail.rs:24:6 | ||
| | ||
LL | impl Tr for bool { | ||
| ^^ | ||
|
||
error[E0275]: overflow evaluating the requirement `<bool as Tr>::B` | ||
--> $DIR/defaults-cyclic-fail.rs:26:5 | ||
| | ||
LL | type A = Box<Self::B>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0275`. |
This file contains hidden or 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,22 @@ | ||
// check-pass | ||
|
||
#![feature(associated_type_defaults)] | ||
|
||
trait Tr { | ||
type Item = u8; | ||
type Container = Vec<Self::Item>; | ||
} | ||
|
||
impl Tr for () {} | ||
|
||
impl Tr for u16 { | ||
type Item = u16; | ||
} | ||
|
||
fn main() { | ||
let _container: <() as Tr>::Container = Vec::<u8>::new(); | ||
let _item: <() as Tr>::Item = 0u8; | ||
|
||
let _container: <u16 as Tr>::Container = Vec::<u16>::new(); | ||
let _item: <u16 as Tr>::Item = 0u16; | ||
} |
50 changes: 50 additions & 0 deletions
50
src/test/ui/associated-types/defaults-in-other-trait-items.rs
This file contains hidden or 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,50 @@ | ||
#![feature(associated_type_defaults)] | ||
|
||
// Associated type defaults may not be assumed inside the trait defining them. | ||
// ie. they only resolve to `<Self as Tr>::A`, not the actual type `()` | ||
trait Tr { | ||
type A = (); | ||
|
||
fn f(p: Self::A) { | ||
let () = p; | ||
//~^ ERROR mismatched types | ||
//~| NOTE expected associated type, found `()` | ||
//~| NOTE expected associated type `<Self as Tr>::A` | ||
//~| NOTE consider constraining | ||
//~| NOTE for more information, visit | ||
} | ||
} | ||
|
||
// An impl that doesn't override the type *can* assume the default. | ||
impl Tr for () { | ||
fn f(p: Self::A) { | ||
let () = p; | ||
} | ||
} | ||
|
||
impl Tr for u8 { | ||
type A = (); | ||
|
||
fn f(p: Self::A) { | ||
let () = p; | ||
} | ||
} | ||
|
||
trait AssocConst { | ||
type Ty = u8; | ||
|
||
// Assoc. consts also cannot assume that default types hold | ||
const C: Self::Ty = 0u8; | ||
jonas-schievink marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//~^ ERROR mismatched types | ||
//~| NOTE expected associated type, found `u8` | ||
//~| NOTE expected associated type `<Self as AssocConst>::Ty` | ||
//~| NOTE consider constraining | ||
//~| NOTE for more information, visit | ||
} | ||
|
||
// An impl can, however | ||
impl AssocConst for () { | ||
const C: Self::Ty = 0u8; | ||
} | ||
|
||
fn main() {} |
25 changes: 25 additions & 0 deletions
25
src/test/ui/associated-types/defaults-in-other-trait-items.stderr
This file contains hidden or 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,25 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/defaults-in-other-trait-items.rs:9:13 | ||
| | ||
LL | let () = p; | ||
| ^^ expected associated type, found `()` | ||
| | ||
= note: expected associated type `<Self as Tr>::A` | ||
found unit type `()` | ||
= note: consider constraining the associated type `<Self as Tr>::A` to `()` or calling a method that returns `<Self as Tr>::A` | ||
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/defaults-in-other-trait-items.rs:37:25 | ||
| | ||
LL | const C: Self::Ty = 0u8; | ||
| ^^^ expected associated type, found `u8` | ||
| | ||
= note: expected associated type `<Self as AssocConst>::Ty` | ||
found type `u8` | ||
= note: consider constraining the associated type `<Self as AssocConst>::Ty` to `u8` or calling a method that returns `<Self as AssocConst>::Ty` | ||
= note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.