-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Opaque types' generic params do not imply anything about their hidden type's lifetimes #98933
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
bors
merged 3 commits into
rust-lang:master
from
oli-obk:opaque_type_late_bound_lifetimes
Sep 8, 2022
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
@@ -1,17 +1,33 @@ | ||
// check-pass | ||
|
||
#![feature(type_alias_impl_trait)] | ||
|
||
mod foo { | ||
mod lifetime_params { | ||
type Ty<'a> = impl Sized; | ||
fn defining(s: &str) -> Ty<'_> { s } | ||
fn execute(ty: Ty<'_>) -> &str { todo!() } | ||
//~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types | ||
|
||
type BadFnSig = fn(Ty<'_>) -> &str; | ||
//~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types | ||
type BadTraitRef = dyn Fn(Ty<'_>) -> &str; | ||
//~^ ERROR binding for associated type `Output` references an anonymous lifetime | ||
} | ||
|
||
mod bar { | ||
mod lifetime_params_2 { | ||
type Ty<'a> = impl FnOnce() -> &'a str; | ||
fn defining(s: &str) -> Ty<'_> { move || s } | ||
fn execute(ty: Ty<'_>) -> &str { ty() } | ||
//~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types | ||
} | ||
|
||
// regression test for https://github.com/rust-lang/rust/issues/97104 | ||
mod type_params { | ||
type Ty<T> = impl Sized; | ||
fn define<T>(s: T) -> Ty<T> { s } | ||
|
||
type BadFnSig = fn(Ty<&str>) -> &str; | ||
//~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types | ||
type BadTraitRef = dyn Fn(Ty<&str>) -> &str; | ||
//~^ ERROR binding for associated type `Output` references an anonymous lifetime | ||
} | ||
|
||
fn main() {} |
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,58 @@ | ||
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types | ||
--> $DIR/constrain_inputs.rs:6:31 | ||
| | ||
LL | fn execute(ty: Ty<'_>) -> &str { todo!() } | ||
| ^^^^ | ||
| | ||
= note: lifetimes appearing in an associated or opaque type are not considered constrained | ||
= note: consider introducing a named lifetime parameter | ||
|
||
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types | ||
--> $DIR/constrain_inputs.rs:9:35 | ||
| | ||
LL | type BadFnSig = fn(Ty<'_>) -> &str; | ||
| ^^^^ | ||
| | ||
= note: lifetimes appearing in an associated or opaque type are not considered constrained | ||
= note: consider introducing a named lifetime parameter | ||
|
||
error[E0582]: binding for associated type `Output` references an anonymous lifetime, which does not appear in the trait input types | ||
--> $DIR/constrain_inputs.rs:11:42 | ||
| | ||
LL | type BadTraitRef = dyn Fn(Ty<'_>) -> &str; | ||
| ^^^^ | ||
| | ||
= note: lifetimes appearing in an associated or opaque type are not considered constrained | ||
= note: consider introducing a named lifetime parameter | ||
|
||
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types | ||
--> $DIR/constrain_inputs.rs:18:31 | ||
| | ||
LL | fn execute(ty: Ty<'_>) -> &str { ty() } | ||
| ^^^^ | ||
| | ||
= note: lifetimes appearing in an associated or opaque type are not considered constrained | ||
= note: consider introducing a named lifetime parameter | ||
|
||
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types | ||
--> $DIR/constrain_inputs.rs:27:37 | ||
| | ||
LL | type BadFnSig = fn(Ty<&str>) -> &str; | ||
| ^^^^ | ||
| | ||
= note: lifetimes appearing in an associated or opaque type are not considered constrained | ||
= note: consider introducing a named lifetime parameter | ||
|
||
error[E0582]: binding for associated type `Output` references an anonymous lifetime, which does not appear in the trait input types | ||
--> $DIR/constrain_inputs.rs:29:44 | ||
| | ||
LL | type BadTraitRef = dyn Fn(Ty<&str>) -> &str; | ||
| ^^^^ | ||
| | ||
= note: lifetimes appearing in an associated or opaque type are not considered constrained | ||
= note: consider introducing a named lifetime parameter | ||
|
||
error: aborting due to 6 previous errors | ||
|
||
Some errors have detailed explanations: E0581, E0582. | ||
For more information about an error, try `rustc --explain E0581`. |
31 changes: 31 additions & 0 deletions
31
src/test/ui/type-alias-impl-trait/constrain_inputs_unsound.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,31 @@ | ||
#![feature(type_alias_impl_trait)] | ||
|
||
trait Static: 'static {} | ||
impl Static for () {} | ||
|
||
type Gal<T> = impl Static; | ||
fn _defining<T>() -> Gal<T> {} | ||
|
||
trait Callable<Arg> { type Output; } | ||
|
||
/// We can infer `<C as Callable<Arg>>::Output: 'static`, | ||
/// because we know `C: 'static` and `Arg: 'static`, | ||
fn box_str<C, Arg>(s: C::Output) -> Box<dyn AsRef<str> + 'static> | ||
where | ||
Arg: Static, | ||
C: ?Sized + Callable<Arg> + 'static, | ||
C::Output: AsRef<str>, | ||
{ | ||
Box::new(s) | ||
} | ||
|
||
fn extend_lifetime(s: &str) -> Box<dyn AsRef<str> + 'static> { | ||
type MalformedTy = dyn for<'a> Callable<Gal<&'a ()>, Output = &'a str>; | ||
//~^ ERROR binding for associated type `Output` references lifetime `'a` | ||
box_str::<MalformedTy, _>(s) | ||
} | ||
|
||
fn main() { | ||
let extended = extend_lifetime(&String::from("hello")); | ||
println!("{}", extended.as_ref().as_ref()); | ||
} |
9 changes: 9 additions & 0 deletions
9
src/test/ui/type-alias-impl-trait/constrain_inputs_unsound.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,9 @@ | ||
error[E0582]: binding for associated type `Output` references lifetime `'a`, which does not appear in the trait input types | ||
--> $DIR/constrain_inputs_unsound.rs:23:58 | ||
| | ||
LL | type MalformedTy = dyn for<'a> Callable<Gal<&'a ()>, Output = &'a str>; | ||
| ^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0582`. |
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.
why is the note "lifetimes appearing in an associated type are not considered constrained" not emitted here?
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.
We're not checking for higher kinded lifetimes in the diagnostic, we're just checking for names. It would be a larger refactoring to handle the difference between lifetimes on the type alias and lifetimes in a binder.