Skip to content

Commit f1679f7

Browse files
Capture lifetimes for associated type bounds destined to be lowered to opaques
1 parent b2515fa commit f1679f7

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)