Repository navigation
Improve diagnostics for non-well-formed associated items #162432
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 all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2e2cd54
Improve diagnostics for non-well-formed associated items
ozankenangungor 29d1ae1
Handle explicit associated item own bounds generally
ozankenangungor bee7ba8
Simplify associated type bound checks
ozankenangungor 192d095
Document associated type guard
ozankenangungor 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
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
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
92 changes: 92 additions & 0 deletions
92
tests/ui/traits/next-solver/assoc-item-own-bound-diagnostics.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,92 @@ | ||
| //@ compile-flags: -Znext-solver | ||
|
|
||
| // Tests the diagnostics for associated-item own clauses (e.g. `type Assoc | ||
| // where Self: Sized;`) that are not satisfied by an impl. The recovered error | ||
| // should point at the type that fails the clause and only suggest fixes that | ||
| // would actually satisfy it. | ||
|
|
||
| // The impl associated type already carries other where clauses, but is | ||
| // missing the `Self: Sized` clause required by the trait item. Relaxing the | ||
| // implicit `Sized` restriction of the associated type would not satisfy that | ||
| // clause. | ||
| trait TraitWithBound { | ||
| type Assoc | ||
| where | ||
| Self: Sized, | ||
| Self: 'static; | ||
| } | ||
|
|
||
| impl<T: ?Sized> TraitWithBound for T { | ||
| //~^ ERROR the size for values of type `T` cannot be known at compilation time | ||
| type Assoc = i32 | ||
| where | ||
| Self: 'static; | ||
| } | ||
|
|
||
| // The value of the impl associated type does not satisfy the implicit `Sized` | ||
| // restriction of the trait item. Relaxing that restriction is the right fix, | ||
| // so the suggestion must still be emitted even though the trait item carries | ||
| // other explicit where clauses. | ||
| trait TraitWithOtherClause { | ||
| type Assoc | ||
| where | ||
| Self: 'static; | ||
| } | ||
|
|
||
| impl TraitWithOtherClause for () { | ||
| type Assoc = [u8]; | ||
| //~^ ERROR the size for values of type `[u8]` cannot be known at compilation time | ||
| } | ||
|
|
||
| // A trait item without any explicit where clauses: the ordinary | ||
| // implicit-`Sized` diagnostic is unchanged. | ||
| trait NormalAssoc { | ||
| type Assoc; | ||
| } | ||
|
|
||
| impl NormalAssoc for () { | ||
| type Assoc = [u8]; | ||
| //~^ ERROR the size for values of type `[u8]` cannot be known at compilation time | ||
| } | ||
|
|
||
| // A projection use site outside of an impl: the recovery also applies there. | ||
| trait NonImplTrait { | ||
| type Assoc | ||
| where | ||
| Self: Sized; | ||
| } | ||
|
|
||
| fn non_impl_use<T: ?Sized>() { | ||
| let _: <T as NonImplTrait>::Assoc; | ||
| //~^ ERROR the size for values of type `T` cannot be known at compilation time | ||
| //~| ERROR the trait bound `T: NonImplTrait` is not satisfied | ||
| } | ||
|
|
||
| // Own clauses with traits other than `Sized` are handled the same way. | ||
| trait Marker {} | ||
|
|
||
| trait TraitWithMarker { | ||
| type Assoc | ||
| where | ||
| Self: Marker; | ||
| } | ||
|
|
||
| impl<T> TraitWithMarker for T { | ||
| //~^ ERROR the trait bound `T: Marker` is not satisfied | ||
| type Assoc = i32; | ||
| } | ||
|
|
||
| // Own clauses on traits with a custom `#[rustc_on_unimplemented]` diagnostic | ||
| // keep their custom label. | ||
| trait TraitWithSend { | ||
| type Assoc | ||
| where | ||
| Self: Send; | ||
| } | ||
|
|
||
| impl<T> TraitWithSend for T { | ||
| //~^ ERROR `T` cannot be sent between threads safely | ||
| type Assoc = i32; | ||
| } | ||
|
|
||
| fn main() {} |
135 changes: 135 additions & 0 deletions
135
tests/ui/traits/next-solver/assoc-item-own-bound-diagnostics.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,135 @@ | ||
| error[E0277]: the size for values of type `T` cannot be known at compilation time | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:19:6 | ||
| | | ||
| LL | impl<T: ?Sized> TraitWithBound for T { | ||
| | ^ | ||
| | | | ||
| | doesn't have a size known at compile-time | ||
| | this type parameter needs to be `Sized` | ||
| | | ||
| note: required by a bound in `TraitWithBound::Assoc` | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:15:15 | ||
| | | ||
| LL | trait TraitWithBound { | ||
| LL | type Assoc | ||
| LL | where | ||
| LL | Self: Sized, | ||
| | ^^^^^ required by this bound in `TraitWithBound::Assoc` | ||
| help: consider removing the `?Sized` bound to make the type parameter `Sized` | ||
| | | ||
| LL - impl<T: ?Sized> TraitWithBound for T { | ||
| LL + impl<T> TraitWithBound for T { | ||
| | | ||
|
|
||
| error[E0277]: the size for values of type `[u8]` cannot be known at compilation time | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:37:18 | ||
| | | ||
| LL | type Assoc = [u8]; | ||
| | ^^^^ doesn't have a size known at compile-time | ||
| | | ||
| = help: the trait `Sized` is not implemented for `[u8]` | ||
| note: required by a bound in `TraitWithOtherClause::Assoc` | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:31:5 | ||
| | | ||
| LL | trait TraitWithOtherClause { | ||
| LL | / type Assoc | ||
| LL | | where | ||
| LL | | Self: 'static; | ||
| | |______________________^ required by this bound in `TraitWithOtherClause::Assoc` | ||
| help: consider relaxing the implicit `Sized` restriction | ||
| | | ||
| LL | type Assoc: ?Sized | ||
| | ++++++++ | ||
|
|
||
| error[E0277]: the size for values of type `[u8]` cannot be known at compilation time | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:48:18 | ||
| | | ||
| LL | type Assoc = [u8]; | ||
| | ^^^^ doesn't have a size known at compile-time | ||
| | | ||
| = help: the trait `Sized` is not implemented for `[u8]` | ||
| note: required by a bound in `NormalAssoc::Assoc` | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:44:5 | ||
| | | ||
| LL | trait NormalAssoc { | ||
| LL | type Assoc; | ||
| | ^^^^^^^^^^^ required by this bound in `NormalAssoc::Assoc` | ||
| help: consider relaxing the implicit `Sized` restriction | ||
| | | ||
| LL | type Assoc: ?Sized; | ||
| | ++++++++ | ||
|
|
||
| error[E0277]: the trait bound `T: Marker` is not satisfied | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:74:6 | ||
| | | ||
| LL | impl<T> TraitWithMarker for T { | ||
| | ^ the trait `Marker` is not implemented for `T` | ||
| | | ||
| note: required by a bound in `TraitWithMarker::Assoc` | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:71:15 | ||
| | | ||
| LL | trait TraitWithMarker { | ||
| LL | type Assoc | ||
| LL | where | ||
| LL | Self: Marker; | ||
| | ^^^^^^ required by this bound in `TraitWithMarker::Assoc` | ||
| help: consider restricting type parameter `T` with trait `Marker` | ||
| | | ||
| LL | impl<T: Marker> TraitWithMarker for T { | ||
| | ++++++++ | ||
|
|
||
| error[E0277]: `T` cannot be sent between threads safely | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:87:6 | ||
| | | ||
| LL | impl<T> TraitWithSend for T { | ||
| | ^ `T` cannot be sent between threads safely | ||
| | | ||
| note: required by a bound in `TraitWithSend::Assoc` | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:84:15 | ||
| | | ||
| LL | trait TraitWithSend { | ||
| LL | type Assoc | ||
| LL | where | ||
| LL | Self: Send; | ||
| | ^^^^ required by this bound in `TraitWithSend::Assoc` | ||
| help: consider restricting type parameter `T` with trait `Send` | ||
| | | ||
| LL | impl<T: std::marker::Send> TraitWithSend for T { | ||
| | +++++++++++++++++++ | ||
|
|
||
| error[E0277]: the trait bound `T: NonImplTrait` is not satisfied | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:60:13 | ||
| | | ||
| LL | let _: <T as NonImplTrait>::Assoc; | ||
| | ^ the trait `NonImplTrait` is not implemented for `T` | ||
| | | ||
| help: consider further restricting type parameter `T` with trait `NonImplTrait` | ||
| | | ||
| LL | fn non_impl_use<T: ?Sized + NonImplTrait>() { | ||
| | ++++++++++++++ | ||
|
|
||
| error[E0277]: the size for values of type `T` cannot be known at compilation time | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:60:13 | ||
| | | ||
| LL | fn non_impl_use<T: ?Sized>() { | ||
| | - this type parameter needs to be `Sized` | ||
| LL | let _: <T as NonImplTrait>::Assoc; | ||
| | ^ doesn't have a size known at compile-time | ||
| | | ||
| note: required by a bound in `NonImplTrait::Assoc` | ||
| --> $DIR/assoc-item-own-bound-diagnostics.rs:56:15 | ||
| | | ||
| LL | trait NonImplTrait { | ||
| LL | type Assoc | ||
| LL | where | ||
| LL | Self: Sized; | ||
| | ^^^^^ required by this bound in `NonImplTrait::Assoc` | ||
| help: consider removing the `?Sized` bound to make the type parameter `Sized` | ||
| | | ||
| LL - fn non_impl_use<T: ?Sized>() { | ||
| LL + fn non_impl_use<T>() { | ||
| | | ||
|
|
||
| error: aborting due to 7 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
17 changes: 17 additions & 0 deletions
17
tests/ui/traits/next-solver/assoc-item-own-bound.current.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,17 @@ | ||
| error[E0277]: the size for values of type `T` cannot be known at compilation time | ||
| --> $DIR/assoc-item-own-bound.rs:18:18 | ||
| | | ||
| LL | impl<T: ?Sized> Trait for T { | ||
| | - this type parameter needs to be `Sized` | ||
| LL | type Assoc = i32; | ||
| | ^^^ doesn't have a size known at compile-time | ||
| | | ||
| help: consider removing the `?Sized` bound to make the type parameter `Sized` | ||
| | | ||
| LL - impl<T: ?Sized> Trait for T { | ||
| LL + impl<T> Trait for T { | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0277`. |
Oops, something went wrong.
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.