Skip to content

Commit ac2d840

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#97104 cc `@aliemjay`
2 parents ccb5595 + 64d11fc commit ac2d840

File tree

9 files changed

+127
-9
lines changed

9 files changed

+127
-9
lines changed

compiler/rustc_middle/src/ty/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ impl<'tcx> TypeVisitor<'tcx> for LateBoundRegionsCollector {
666666
// ignore the inputs to a projection, as they may not appear
667667
// in the normalized form
668668
if self.just_constrained {
669-
if let ty::Projection(..) = t.kind() {
669+
if let ty::Projection(..) | ty::Opaque(..) = t.kind() {
670670
return ControlFlow::CONTINUE;
671671
}
672672
}

compiler/rustc_typeck/src/astconv/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2940,8 +2940,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
29402940
// though we can easily give a hint that ought to be
29412941
// relevant.
29422942
err.note(
2943-
"lifetimes appearing in an associated type are not considered constrained",
2943+
"lifetimes appearing in an associated or opaque type are not considered constrained",
29442944
);
2945+
err.note("consider introducing a named lifetime parameter");
29452946
}
29462947

29472948
err.emit();

src/test/ui/associated-types/issue-62200.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ impl T<'_> for S {
1010

1111
fn foo(x: impl Fn(<S as T<'_>>::A) -> <S as T<'_>>::A) {}
1212
//~^ ERROR binding for associated type `Output` references an anonymous lifetime
13-
//~^^ NOTE lifetimes appearing in an associated type are not considered constrained
13+
//~| NOTE lifetimes appearing in an associated or opaque type are not considered constrained
14+
//~| NOTE consider introducing a named lifetime parameter
1415

1516
fn main() {}

src/test/ui/associated-types/issue-62200.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0582]: binding for associated type `Output` references an anonymous lifet
44
LL | fn foo(x: impl Fn(<S as T<'_>>::A) -> <S as T<'_>>::A) {}
55
| ^^^^^^^^^^^^^^^
66
|
7-
= note: lifetimes appearing in an associated type are not considered constrained
7+
= note: lifetimes appearing in an associated or opaque type are not considered constrained
8+
= note: consider introducing a named lifetime parameter
89

910
error: aborting due to previous error
1011

src/test/ui/issues/issue-47511.stderr

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ error[E0581]: return type references an anonymous lifetime, which is not constra
44
LL | fn f(_: X) -> X {
55
| ^
66
|
7-
= note: lifetimes appearing in an associated type are not considered constrained
7+
= note: lifetimes appearing in an associated or opaque type are not considered constrained
8+
= note: consider introducing a named lifetime parameter
89

910
error[E0581]: return type references lifetime `'a`, which is not constrained by the fn input types
1011
--> $DIR/issue-47511.rs:12:23
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,33 @@
1-
// check-pass
2-
31
#![feature(type_alias_impl_trait)]
42

5-
mod foo {
3+
mod lifetime_params {
64
type Ty<'a> = impl Sized;
75
fn defining(s: &str) -> Ty<'_> { s }
86
fn execute(ty: Ty<'_>) -> &str { todo!() }
7+
//~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types
8+
9+
type BadFnSig = fn(Ty<'_>) -> &str;
10+
//~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types
11+
type BadTraitRef = dyn Fn(Ty<'_>) -> &str;
12+
//~^ ERROR binding for associated type `Output` references an anonymous lifetime
913
}
1014

11-
mod bar {
15+
mod lifetime_params_2 {
1216
type Ty<'a> = impl FnOnce() -> &'a str;
1317
fn defining(s: &str) -> Ty<'_> { move || s }
1418
fn execute(ty: Ty<'_>) -> &str { ty() }
19+
//~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types
20+
}
21+
22+
// regression test for https://github.com/rust-lang/rust/issues/97104
23+
mod type_params {
24+
type Ty<T> = impl Sized;
25+
fn define<T>(s: T) -> Ty<T> { s }
26+
27+
type BadFnSig = fn(Ty<&str>) -> &str;
28+
//~^ ERROR return type references an anonymous lifetime, which is not constrained by the fn input types
29+
type BadTraitRef = dyn Fn(Ty<&str>) -> &str;
30+
//~^ ERROR binding for associated type `Output` references an anonymous lifetime
1531
}
1632

1733
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types
2+
--> $DIR/constrain_inputs.rs:6:31
3+
|
4+
LL | fn execute(ty: Ty<'_>) -> &str { todo!() }
5+
| ^^^^
6+
|
7+
= note: lifetimes appearing in an associated or opaque type are not considered constrained
8+
= note: consider introducing a named lifetime parameter
9+
10+
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types
11+
--> $DIR/constrain_inputs.rs:9:35
12+
|
13+
LL | type BadFnSig = fn(Ty<'_>) -> &str;
14+
| ^^^^
15+
|
16+
= note: lifetimes appearing in an associated or opaque type are not considered constrained
17+
= note: consider introducing a named lifetime parameter
18+
19+
error[E0582]: binding for associated type `Output` references an anonymous lifetime, which does not appear in the trait input types
20+
--> $DIR/constrain_inputs.rs:11:42
21+
|
22+
LL | type BadTraitRef = dyn Fn(Ty<'_>) -> &str;
23+
| ^^^^
24+
|
25+
= note: lifetimes appearing in an associated or opaque type are not considered constrained
26+
= note: consider introducing a named lifetime parameter
27+
28+
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types
29+
--> $DIR/constrain_inputs.rs:18:31
30+
|
31+
LL | fn execute(ty: Ty<'_>) -> &str { ty() }
32+
| ^^^^
33+
|
34+
= note: lifetimes appearing in an associated or opaque type are not considered constrained
35+
= note: consider introducing a named lifetime parameter
36+
37+
error[E0581]: return type references an anonymous lifetime, which is not constrained by the fn input types
38+
--> $DIR/constrain_inputs.rs:27:37
39+
|
40+
LL | type BadFnSig = fn(Ty<&str>) -> &str;
41+
| ^^^^
42+
|
43+
= note: lifetimes appearing in an associated or opaque type are not considered constrained
44+
= note: consider introducing a named lifetime parameter
45+
46+
error[E0582]: binding for associated type `Output` references an anonymous lifetime, which does not appear in the trait input types
47+
--> $DIR/constrain_inputs.rs:29:44
48+
|
49+
LL | type BadTraitRef = dyn Fn(Ty<&str>) -> &str;
50+
| ^^^^
51+
|
52+
= note: lifetimes appearing in an associated or opaque type are not considered constrained
53+
= note: consider introducing a named lifetime parameter
54+
55+
error: aborting due to 6 previous errors
56+
57+
Some errors have detailed explanations: E0581, E0582.
58+
For more information about an error, try `rustc --explain E0581`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
trait Static: 'static {}
4+
impl Static for () {}
5+
6+
type Gal<T> = impl Static;
7+
fn _defining<T>() -> Gal<T> {}
8+
9+
trait Callable<Arg> { type Output; }
10+
11+
/// We can infer `<C as Callable<Arg>>::Output: 'static`,
12+
/// because we know `C: 'static` and `Arg: 'static`,
13+
fn box_str<C, Arg>(s: C::Output) -> Box<dyn AsRef<str> + 'static>
14+
where
15+
Arg: Static,
16+
C: ?Sized + Callable<Arg> + 'static,
17+
C::Output: AsRef<str>,
18+
{
19+
Box::new(s)
20+
}
21+
22+
fn extend_lifetime(s: &str) -> Box<dyn AsRef<str> + 'static> {
23+
type MalformedTy = dyn for<'a> Callable<Gal<&'a ()>, Output = &'a str>;
24+
//~^ ERROR binding for associated type `Output` references lifetime `'a`
25+
box_str::<MalformedTy, _>(s)
26+
}
27+
28+
fn main() {
29+
let extended = extend_lifetime(&String::from("hello"));
30+
println!("{}", extended.as_ref().as_ref());
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0582]: binding for associated type `Output` references lifetime `'a`, which does not appear in the trait input types
2+
--> $DIR/constrain_inputs_unsound.rs:23:58
3+
|
4+
LL | type MalformedTy = dyn for<'a> Callable<Gal<&'a ()>, Output = &'a str>;
5+
| ^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0582`.

0 commit comments

Comments
 (0)