-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #98933 - oli-obk:opaque_type_late_bound_lifetimes, r=…
…lcnr Opaque types' generic params do not imply anything about their hidden type's lifetimes fixes #97104 cc ```@aliemjay```
- Loading branch information
Showing
9 changed files
with
127 additions
and
9 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
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`. |