Resolve lifetimes using the regular logic for RPIT.

This commit is contained in:
Camille GILLOT 2022-10-25 16:10:19 +00:00
parent 44c10e4cb0
commit 3075f03513
11 changed files with 238 additions and 62 deletions

View File

@ -452,7 +452,9 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
intravisit::walk_item(this, item)
});
}
hir::ItemKind::OpaqueTy(hir::OpaqueTy { .. }) => {
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
origin: hir::OpaqueTyOrigin::TyAlias, ..
}) => {
// Opaque types are visited when we visit the
// `TyKind::OpaqueDef`, so that they have the lifetimes from
// their parent opaque_ty in scope.
@ -478,6 +480,37 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
});
}
}
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
origin: hir::OpaqueTyOrigin::FnReturn(_) | hir::OpaqueTyOrigin::AsyncFn(_),
generics,
..
}) => {
// We want to start our early-bound indices at the end of the parent scope,
// not including any parent `impl Trait`s.
let mut lifetimes = FxIndexMap::default();
debug!(?generics.params);
for param in generics.params {
match param.kind {
GenericParamKind::Lifetime { .. } => {
let (def_id, reg) = Region::early(self.tcx.hir(), &param);
lifetimes.insert(def_id, reg);
}
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {}
}
}
let scope = Scope::Binder {
hir_id: item.hir_id(),
lifetimes,
s: self.scope,
scope_type: BinderScopeType::Normal,
where_bound_origin: None,
};
self.with(scope, |this| {
let scope = Scope::TraitRefBoundary { s: this.scope };
this.with(scope, |this| intravisit::walk_item(this, item))
});
}
hir::ItemKind::TyAlias(_, ref generics)
| hir::ItemKind::Enum(_, ref generics)
| hir::ItemKind::Struct(_, ref generics)
@ -604,7 +637,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
// ^ ^ this gets resolved in the scope of
// the opaque_ty generics
let opaque_ty = self.tcx.hir().item(item_id);
let (generics, bounds) = match opaque_ty.kind {
match opaque_ty.kind {
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
origin: hir::OpaqueTyOrigin::TyAlias,
..
@ -625,10 +658,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
}
hir::ItemKind::OpaqueTy(hir::OpaqueTy {
origin: hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..),
ref generics,
bounds,
..
}) => (generics, bounds),
}) => {}
ref i => bug!("`impl Trait` pointed to non-opaque type?? {:#?}", i),
};
@ -681,38 +712,6 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
self.uninsert_lifetime_on_error(lifetime, def.unwrap());
}
}
// We want to start our early-bound indices at the end of the parent scope,
// not including any parent `impl Trait`s.
let mut lifetimes = FxIndexMap::default();
debug!(?generics.params);
for param in generics.params {
match param.kind {
GenericParamKind::Lifetime { .. } => {
let (def_id, reg) = Region::early(self.tcx.hir(), &param);
lifetimes.insert(def_id, reg);
}
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {}
}
}
self.record_late_bound_vars(ty.hir_id, vec![]);
let scope = Scope::Binder {
hir_id: ty.hir_id,
lifetimes,
s: self.scope,
scope_type: BinderScopeType::Normal,
where_bound_origin: None,
};
self.with(scope, |this| {
let scope = Scope::TraitRefBoundary { s: this.scope };
this.with(scope, |this| {
this.visit_generics(generics);
for bound in bounds {
this.visit_param_bound(bound);
}
})
});
}
_ => intravisit::walk_ty(self, ty),
}

View File

@ -4,16 +4,19 @@ use std::fmt::Debug;
fn a() -> impl Fn(&u8) -> (impl Debug + '_) {
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
|x| x
//~^ ERROR lifetime may not live long enough
}
fn b() -> impl for<'a> Fn(&'a u8) -> (impl Debug + 'a) {
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
|x| x
//~^ ERROR lifetime may not live long enough
}
fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) {
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
|x| x
//~^ ERROR lifetime may not live long enough
}
fn d() -> impl Fn() -> (impl Debug + '_) {

View File

@ -1,5 +1,5 @@
error[E0106]: missing lifetime specifier
--> $DIR/impl-fn-hrtb-bounds.rs:19:38
--> $DIR/impl-fn-hrtb-bounds.rs:22:38
|
LL | fn d() -> impl Fn() -> (impl Debug + '_) {
| ^^ expected named lifetime parameter
@ -22,30 +22,57 @@ note: lifetime declared here
LL | fn a() -> impl Fn(&u8) -> (impl Debug + '_) {
| ^
error: lifetime may not live long enough
--> $DIR/impl-fn-hrtb-bounds.rs:6:9
|
LL | |x| x
| -- ^ returning this value requires that `'1` must outlive `'2`
| ||
| |return type of closure is impl Debug + '2
| has type `&'1 u8`
error: higher kinded lifetime bounds on nested opaque types are not supported yet
--> $DIR/impl-fn-hrtb-bounds.rs:9:52
--> $DIR/impl-fn-hrtb-bounds.rs:10:52
|
LL | fn b() -> impl for<'a> Fn(&'a u8) -> (impl Debug + 'a) {
| ^^
|
note: lifetime declared here
--> $DIR/impl-fn-hrtb-bounds.rs:9:20
--> $DIR/impl-fn-hrtb-bounds.rs:10:20
|
LL | fn b() -> impl for<'a> Fn(&'a u8) -> (impl Debug + 'a) {
| ^^
error: lifetime may not live long enough
--> $DIR/impl-fn-hrtb-bounds.rs:12:9
|
LL | |x| x
| -- ^ returning this value requires that `'1` must outlive `'2`
| ||
| |return type of closure is impl Debug + '2
| has type `&'1 u8`
error: higher kinded lifetime bounds on nested opaque types are not supported yet
--> $DIR/impl-fn-hrtb-bounds.rs:14:52
--> $DIR/impl-fn-hrtb-bounds.rs:16:52
|
LL | fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) {
| ^^
|
note: lifetime declared here
--> $DIR/impl-fn-hrtb-bounds.rs:14:20
--> $DIR/impl-fn-hrtb-bounds.rs:16:20
|
LL | fn c() -> impl for<'a> Fn(&'a u8) -> (impl Debug + '_) {
| ^^
error: aborting due to 4 previous errors
error: lifetime may not live long enough
--> $DIR/impl-fn-hrtb-bounds.rs:18:9
|
LL | |x| x
| -- ^ returning this value requires that `'1` must outlive `'2`
| ||
| |return type of closure is impl Debug + '2
| has type `&'1 u8`
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0106`.

View File

@ -3,8 +3,9 @@ use std::fmt::Debug;
fn a() -> impl Fn(&u8) -> impl Debug + '_ {
//~^ ERROR ambiguous `+` in a type
//~^^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
//~| ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
|x| x
//~^ ERROR lifetime may not live long enough
}
fn b() -> impl Fn() -> impl Debug + Send {

View File

@ -5,7 +5,7 @@ LL | fn a() -> impl Fn(&u8) -> impl Debug + '_ {
| ^^^^^^^^^^^^^^^ help: use parentheses to disambiguate: `(impl Debug + '_)`
error: ambiguous `+` in a type
--> $DIR/impl-fn-parsing-ambiguities.rs:10:24
--> $DIR/impl-fn-parsing-ambiguities.rs:11:24
|
LL | fn b() -> impl Fn() -> impl Debug + Send {
| ^^^^^^^^^^^^^^^^^ help: use parentheses to disambiguate: `(impl Debug + Send)`
@ -22,5 +22,14 @@ note: lifetime declared here
LL | fn a() -> impl Fn(&u8) -> impl Debug + '_ {
| ^
error: aborting due to 3 previous errors
error: lifetime may not live long enough
--> $DIR/impl-fn-parsing-ambiguities.rs:7:9
|
LL | |x| x
| -- ^ returning this value requires that `'1` must outlive `'2`
| ||
| |return type of closure is impl Debug + '2
| has type `&'1 u8`
error: aborting due to 4 previous errors

View File

@ -21,6 +21,8 @@ struct A;
fn test() -> impl for<'a> MyFn<&'a A, Output=impl Iterator + 'a> {
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
Wrap(|a| Some(a).into_iter())
//~^ ERROR implementation of `FnOnce` is not general enough
//~| ERROR implementation of `FnOnce` is not general enough
}
fn main() {}

View File

@ -10,5 +10,23 @@ note: lifetime declared here
LL | fn test() -> impl for<'a> MyFn<&'a A, Output=impl Iterator + 'a> {
| ^^
error: aborting due to previous error
error: implementation of `FnOnce` is not general enough
--> $DIR/issue-67830.rs:23:5
|
LL | Wrap(|a| Some(a).into_iter())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
|
= note: closure with signature `fn(&'2 A) -> std::option::IntoIter<&A>` must implement `FnOnce<(&'1 A,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&'2 A,)>`, for some specific lifetime `'2`
error: implementation of `FnOnce` is not general enough
--> $DIR/issue-67830.rs:23:5
|
LL | Wrap(|a| Some(a).into_iter())
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `FnOnce` is not general enough
|
= note: closure with signature `fn(&'2 A) -> std::option::IntoIter<&A>` must implement `FnOnce<(&'1 A,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&'2 A,)>`, for some specific lifetime `'2`
error: aborting due to 3 previous errors

View File

@ -18,11 +18,16 @@ fn make_impl() -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a> {}
fn make_weird_impl<'b>(x: &'b ()) -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a> {
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
&()
//~^ ERROR implementation of `Hrtb` is not general enough
//~| ERROR implementation of `Hrtb` is not general enough
}
fn make_bad_impl<'b>(x: &'b ()) -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a> {
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
x
//~^ ERROR implementation of `Hrtb` is not general enough
//~| ERROR implementation of `Hrtb` is not general enough
//~| ERROR lifetime may not live long enough
}
fn main() {}

View File

@ -22,17 +22,71 @@ note: lifetime declared here
LL | fn make_weird_impl<'b>(x: &'b ()) -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a> {
| ^^
error: implementation of `Hrtb` is not general enough
--> $DIR/issue-88236-2.rs:20:5
|
LL | &()
| ^^^ implementation of `Hrtb` is not general enough
|
= note: `Hrtb<'0>` would have to be implemented for the type `&()`, for any lifetime `'0`...
= note: ...but `Hrtb<'1>` is actually implemented for the type `&'1 ()`, for some specific lifetime `'1`
error: implementation of `Hrtb` is not general enough
--> $DIR/issue-88236-2.rs:20:5
|
LL | &()
| ^^^ implementation of `Hrtb` is not general enough
|
= note: `Hrtb<'0>` would have to be implemented for the type `&()`, for any lifetime `'0`...
= note: ...but `Hrtb<'1>` is actually implemented for the type `&'1 ()`, for some specific lifetime `'1`
error: higher kinded lifetime bounds on nested opaque types are not supported yet
--> $DIR/issue-88236-2.rs:23:78
--> $DIR/issue-88236-2.rs:25:78
|
LL | fn make_bad_impl<'b>(x: &'b ()) -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a> {
| ^^
|
note: lifetime declared here
--> $DIR/issue-88236-2.rs:23:45
--> $DIR/issue-88236-2.rs:25:45
|
LL | fn make_bad_impl<'b>(x: &'b ()) -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a> {
| ^^
error: aborting due to 3 previous errors
error: lifetime may not live long enough
--> $DIR/issue-88236-2.rs:27:5
|
LL | fn make_bad_impl<'b>(x: &'b ()) -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a> {
| -- lifetime `'b` defined here
LL |
LL | x
| ^ returning this value requires that `'b` must outlive `'static`
|
help: to declare that `impl for<'a> Hrtb<'a, Assoc = impl Send + 'static>` captures data from argument `x`, you can add an explicit `'b` lifetime bound
|
LL | fn make_bad_impl<'b>(x: &'b ()) -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a> + 'b {
| ++++
help: to declare that `impl Send + 'a` captures data from argument `x`, you can add an explicit `'b` lifetime bound
|
LL | fn make_bad_impl<'b>(x: &'b ()) -> impl for<'a> Hrtb<'a, Assoc = impl Send + 'a + 'b> {
| ++++
error: implementation of `Hrtb` is not general enough
--> $DIR/issue-88236-2.rs:27:5
|
LL | x
| ^ implementation of `Hrtb` is not general enough
|
= note: `Hrtb<'0>` would have to be implemented for the type `&()`, for any lifetime `'0`...
= note: ...but `Hrtb<'1>` is actually implemented for the type `&'1 ()`, for some specific lifetime `'1`
error: implementation of `Hrtb` is not general enough
--> $DIR/issue-88236-2.rs:27:5
|
LL | x
| ^ implementation of `Hrtb` is not general enough
|
= note: `Hrtb<'0>` would have to be implemented for the type `&()`, for any lifetime `'0`...
= note: ...but `Hrtb<'1>` is actually implemented for the type `&'1 ()`, for some specific lifetime `'1`
error: aborting due to 8 previous errors

View File

@ -31,34 +31,40 @@ fn one_hrtb_trait_param() -> impl for<'a> Foo<'a, Assoc = impl Qux<'a>> {}
fn one_hrtb_outlives_uses() -> impl for<'a> Bar<'a, Assoc = impl Sized + 'a> {}
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
//~| ERROR implementation of `Bar` is not general enough
fn one_hrtb_trait_param_uses() -> impl for<'a> Bar<'a, Assoc = impl Qux<'a>> {}
//~^ ERROR higher kinded lifetime bounds on nested opaque types are not supported yet
//~| ERROR the trait bound `&(): Qux<'static>` is not satisfied
// This should pass.
// This should resolve.
fn one_hrtb_mention_fn_trait_param<'b>() -> impl for<'a> Foo<'a, Assoc = impl Qux<'b>> {}
// This should pass.
// This should resolve.
fn one_hrtb_mention_fn_outlives<'b>() -> impl for<'a> Foo<'a, Assoc = impl Sized + 'b> {}
// This should pass.
// This should resolve.
fn one_hrtb_mention_fn_trait_param_uses<'b>() -> impl for<'a> Bar<'a, Assoc = impl Qux<'b>> {}
//~^ ERROR the trait bound `&(): Qux<'b>` is not satisfied
// This should pass.
// This should resolve.
fn one_hrtb_mention_fn_outlives_uses<'b>() -> impl for<'a> Bar<'a, Assoc = impl Sized + 'b> {}
//~^ ERROR implementation of `Bar` is not general enough
// This should pass.
// This should resolve.
fn two_htrb_trait_param() -> impl for<'a> Foo<'a, Assoc = impl for<'b> Qux<'b>> {}
// `'b` is not in scope for the outlives bound.
fn two_htrb_outlives() -> impl for<'a> Foo<'a, Assoc = impl for<'b> Sized + 'b> {}
//~^ ERROR use of undeclared lifetime name `'b` [E0261]
// This should pass.
// This should resolve.
fn two_htrb_trait_param_uses() -> impl for<'a> Bar<'a, Assoc = impl for<'b> Qux<'b>> {}
//~^ ERROR the trait bound `for<'b> &(): Qux<'b>` is not satisfied
// `'b` is not in scope for the outlives bound.
fn two_htrb_outlives_uses() -> impl for<'a> Bar<'a, Assoc = impl for<'b> Sized + 'b> {}
//~^ ERROR use of undeclared lifetime name `'b` [E0261]
//~| ERROR implementation of `Bar` is not general enough
fn main() {}

View File

@ -1,5 +1,5 @@
error[E0261]: use of undeclared lifetime name `'b`
--> $DIR/nested-rpit-hrtb.rs:54:77
--> $DIR/nested-rpit-hrtb.rs:58:77
|
LL | fn two_htrb_outlives() -> impl for<'a> Foo<'a, Assoc = impl for<'b> Sized + 'b> {}
| ^^ undeclared lifetime
@ -15,7 +15,7 @@ LL | fn two_htrb_outlives<'b>() -> impl for<'a> Foo<'a, Assoc = impl for<'b> Siz
| ++++
error[E0261]: use of undeclared lifetime name `'b`
--> $DIR/nested-rpit-hrtb.rs:61:82
--> $DIR/nested-rpit-hrtb.rs:66:82
|
LL | fn two_htrb_outlives_uses() -> impl for<'a> Bar<'a, Assoc = impl for<'b> Sized + 'b> {}
| ^^ undeclared lifetime
@ -65,18 +65,70 @@ note: lifetime declared here
LL | fn one_hrtb_outlives_uses() -> impl for<'a> Bar<'a, Assoc = impl Sized + 'a> {}
| ^^
error: implementation of `Bar` is not general enough
--> $DIR/nested-rpit-hrtb.rs:32:78
|
LL | fn one_hrtb_outlives_uses() -> impl for<'a> Bar<'a, Assoc = impl Sized + 'a> {}
| ^^ implementation of `Bar` is not general enough
|
= note: `()` must implement `Bar<'0>`, for any lifetime `'0`...
= note: ...but it actually implements `Bar<'1>`, for some specific lifetime `'1`
error: higher kinded lifetime bounds on nested opaque types are not supported yet
--> $DIR/nested-rpit-hrtb.rs:35:73
--> $DIR/nested-rpit-hrtb.rs:36:73
|
LL | fn one_hrtb_trait_param_uses() -> impl for<'a> Bar<'a, Assoc = impl Qux<'a>> {}
| ^^
|
note: lifetime declared here
--> $DIR/nested-rpit-hrtb.rs:35:44
--> $DIR/nested-rpit-hrtb.rs:36:44
|
LL | fn one_hrtb_trait_param_uses() -> impl for<'a> Bar<'a, Assoc = impl Qux<'a>> {}
| ^^
error: aborting due to 6 previous errors
error[E0277]: the trait bound `&(): Qux<'static>` is not satisfied
--> $DIR/nested-rpit-hrtb.rs:36:64
|
LL | fn one_hrtb_trait_param_uses() -> impl for<'a> Bar<'a, Assoc = impl Qux<'a>> {}
| ^^^^^^^^^^^^ the trait `Qux<'static>` is not implemented for `&()`
|
= help: the trait `Qux<'_>` is implemented for `()`
For more information about this error, try `rustc --explain E0261`.
error[E0277]: the trait bound `&(): Qux<'b>` is not satisfied
--> $DIR/nested-rpit-hrtb.rs:47:79
|
LL | fn one_hrtb_mention_fn_trait_param_uses<'b>() -> impl for<'a> Bar<'a, Assoc = impl Qux<'b>> {}
| ^^^^^^^^^^^^ the trait `Qux<'b>` is not implemented for `&()`
|
= help: the trait `Qux<'_>` is implemented for `()`
error: implementation of `Bar` is not general enough
--> $DIR/nested-rpit-hrtb.rs:51:93
|
LL | fn one_hrtb_mention_fn_outlives_uses<'b>() -> impl for<'a> Bar<'a, Assoc = impl Sized + 'b> {}
| ^^ implementation of `Bar` is not general enough
|
= note: `()` must implement `Bar<'0>`, for any lifetime `'0`...
= note: ...but it actually implements `Bar<'1>`, for some specific lifetime `'1`
error[E0277]: the trait bound `for<'b> &(): Qux<'b>` is not satisfied
--> $DIR/nested-rpit-hrtb.rs:62:64
|
LL | fn two_htrb_trait_param_uses() -> impl for<'a> Bar<'a, Assoc = impl for<'b> Qux<'b>> {}
| ^^^^^^^^^^^^^^^^^^^^ the trait `for<'b> Qux<'b>` is not implemented for `&()`
|
= help: the trait `Qux<'_>` is implemented for `()`
error: implementation of `Bar` is not general enough
--> $DIR/nested-rpit-hrtb.rs:66:86
|
LL | fn two_htrb_outlives_uses() -> impl for<'a> Bar<'a, Assoc = impl for<'b> Sized + 'b> {}
| ^^ implementation of `Bar` is not general enough
|
= note: `()` must implement `Bar<'0>`, for any lifetime `'0`...
= note: ...but it actually implements `Bar<'1>`, for some specific lifetime `'1`
error: aborting due to 12 previous errors
Some errors have detailed explanations: E0261, E0277.
For more information about an error, try `rustc --explain E0261`.