-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Enforce well formedness for type alias impl trait's hidden type #95519
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
28 changes: 28 additions & 0 deletions
28
src/test/ui/type-alias-impl-trait/underconstrained_generic.rs
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,28 @@ | ||
#![feature(type_alias_impl_trait)] | ||
|
||
use std::marker::PhantomData; | ||
|
||
trait Trait { | ||
fn foo<T, U>(t: T) -> U; | ||
} | ||
|
||
trait ProofForConversion<X> { | ||
fn convert<T, U>(_: PhantomData<Self>, r: T) -> U; | ||
} | ||
|
||
impl<X: Trait> ProofForConversion<X> for () { | ||
fn convert<T, U>(_: PhantomData<Self>, r: T) -> U { | ||
X::foo(r) | ||
} | ||
} | ||
|
||
type Converter<T> = impl ProofForConversion<T>; | ||
//~^ ERROR the trait bound `T: Trait` is not satisfied | ||
|
||
fn _defining_use<T: Trait>() -> Converter<T> { | ||
() | ||
} | ||
|
||
|
||
fn main() { | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/ui/type-alias-impl-trait/underconstrained_generic.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,19 @@ | ||
error[E0277]: the trait bound `T: Trait` is not satisfied | ||
--> $DIR/underconstrained_generic.rs:19:21 | ||
| | ||
LL | type Converter<T> = impl ProofForConversion<T>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Trait` is not implemented for `T` | ||
| | ||
note: required because of the requirements on the impl of `ProofForConversion<T>` for `()` | ||
--> $DIR/underconstrained_generic.rs:13:16 | ||
| | ||
LL | impl<X: Trait> ProofForConversion<X> for () { | ||
| ^^^^^^^^^^^^^^^^^^^^^ ^^ | ||
help: consider restricting type parameter `T` | ||
| | ||
LL | type Converter<T: Trait> = impl ProofForConversion<T>; | ||
| +++++++ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
34 changes: 34 additions & 0 deletions
34
src/test/ui/type-alias-impl-trait/underconstrained_lifetime.rs
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,34 @@ | ||
#![feature(type_alias_impl_trait)] | ||
|
||
use std::marker::PhantomData; | ||
|
||
trait ProofForConversion<'a, 'b> { | ||
fn convert<T: ?Sized>(_: PhantomData<Self>, r: &'a T) -> &'b T; | ||
} | ||
|
||
impl<'a, 'b> ProofForConversion<'a, 'b> for &'b &'a () { | ||
fn convert<T: ?Sized>(_: PhantomData<Self>, r: &'a T) -> &'b T { | ||
r | ||
} | ||
} | ||
|
||
type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>; | ||
//~^ ERROR reference has a longer lifetime than the data it references | ||
|
||
// Even _defining_use with an explicit `'a: 'b` compiles fine, too. | ||
fn _defining_use<'a, 'b>(x: &'b &'a ()) -> Converter<'a, 'b> { | ||
x | ||
} | ||
|
||
fn extend_lifetime<'a, 'b, T: ?Sized>(x: &'a T) -> &'b T { | ||
Converter::<'a, 'b>::convert(PhantomData, x) | ||
} | ||
|
||
fn main() { | ||
let d; | ||
{ | ||
let x = "Hello World".to_string(); | ||
d = extend_lifetime(&x); | ||
} | ||
println!("{}", d); | ||
} |
20 changes: 20 additions & 0 deletions
20
src/test/ui/type-alias-impl-trait/underconstrained_lifetime.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,20 @@ | ||
error[E0491]: in type `&'b &'a ()`, reference has a longer lifetime than the data it references | ||
--> $DIR/underconstrained_lifetime.rs:15:26 | ||
| | ||
LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: the pointer is valid for the lifetime `'b` as defined here | ||
--> $DIR/underconstrained_lifetime.rs:15:20 | ||
| | ||
LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>; | ||
| ^^ | ||
note: but the referenced data is only valid for the lifetime `'a` as defined here | ||
--> $DIR/underconstrained_lifetime.rs:15:16 | ||
| | ||
LL | type Converter<'a, 'b> = impl ProofForConversion<'a, 'b>; | ||
| ^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0491`. |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason this is required is that associated type alias impl trait does not imply bounds from the type that the impl is for.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be concerned for breakage and get a lang team signoff?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, this is just a nightly feature. We can add more implicit magic later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, i think this is something we can fix later