Skip to content

Commit 2f5df8a

Browse files
committed
Auto merge of #115366 - compiler-errors:associated-type-bound-implicit-lifetimes, r=jackh726
Capture lifetimes for associated type bounds destined to be lowered to opaques Some associated type bounds get lowered to opaques, but they're not represented in the AST as opaques. That means that we never collect lifetimes for them (`record_lifetime_params_for_impl_trait`) which are used currently for RPITITs, which capture all of their in-scope lifetimes[^1]. This means that the nested RPITITs that arise from some type like `impl Foo<Type: Bar>` (~> `impl Foo<Type = impl Bar>`) don't capture any lifetimes, leading to ICEs. This PR makes sure we collect the lifetimes for associated type bounds as well, and make sure that they are set up correctly for opaque type lowering later. Fixes #115360 [^1]: #114489
2 parents 9194213 + f1679f7 commit 2f5df8a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+11
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ trait ResolverAstLoweringExt {
153153
fn get_label_res(&self, id: NodeId) -> Option<NodeId>;
154154
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>;
155155
fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>;
156+
fn remap_extra_lifetime_params(&mut self, from: NodeId, to: NodeId);
156157
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind;
157158
}
158159

@@ -213,6 +214,11 @@ impl ResolverAstLoweringExt for ResolverAstLowering {
213214
self.extra_lifetime_params_map.remove(&id).unwrap_or_default()
214215
}
215216

217+
fn remap_extra_lifetime_params(&mut self, from: NodeId, to: NodeId) {
218+
let lifetimes = self.extra_lifetime_params_map.remove(&from).unwrap_or_default();
219+
self.extra_lifetime_params_map.insert(to, lifetimes);
220+
}
221+
216222
fn decl_macro_kind(&self, def_id: LocalDefId) -> MacroKind {
217223
self.builtin_macro_kinds.get(&def_id).copied().unwrap_or(MacroKind::Bang)
218224
}
@@ -1089,6 +1095,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
10891095
// constructing the HIR for `impl bounds...` and then lowering that.
10901096

10911097
let impl_trait_node_id = self.next_node_id();
1098+
// Shift `impl Trait` lifetime captures from the associated type bound's
1099+
// node id to the opaque node id, so that the opaque can actually use
1100+
// these lifetime bounds.
1101+
self.resolver
1102+
.remap_extra_lifetime_params(constraint.id, impl_trait_node_id);
10921103

10931104
self.with_dyn_type_scope(false, |this| {
10941105
let node_id = this.next_node_id();

compiler/rustc_resolve/src/late.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@ impl<'a: 'ast, 'ast, 'tcx> Visitor<'ast> for LateResolutionVisitor<'a, '_, 'ast,
11091109
}
11101110
},
11111111
AssocConstraintKind::Bound { ref bounds } => {
1112+
self.record_lifetime_params_for_impl_trait(constraint.id);
11121113
walk_list!(self, visit_param_bound, bounds, BoundKind::Bound);
11131114
}
11141115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// check-pass
2+
3+
#![feature(associated_type_bounds, return_position_impl_trait_in_trait)]
4+
5+
trait Trait {
6+
type Type;
7+
8+
fn method(&self) -> impl Trait<Type: '_>;
9+
}
10+
11+
impl Trait for () {
12+
type Type = ();
13+
14+
fn method(&self) -> impl Trait<Type: '_> {
15+
()
16+
}
17+
}
18+
19+
fn main() {}

0 commit comments

Comments
 (0)