Skip to content

Commit 1802d45

Browse files
committed
Record RPITs elided lifetimes in path segments
1 parent 81c4d23 commit 1802d45

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
14461446
hir::OwnerNode::Item(self.arena.alloc(opaque_ty_item))
14471447
}
14481448

1449-
fn create_and_capture_lifetime_defs(&mut self, lifetimes_in_bounds: &[&Lifetime]) {
1449+
fn create_and_capture_lifetime_defs(&mut self, lifetimes_in_bounds: &[Lifetime]) {
14501450
for lifetime in lifetimes_in_bounds {
14511451
let ident = lifetime.ident;
14521452
let span = ident.span;

compiler/rustc_ast_lowering/src/lifetime_collector.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
11
use super::ResolverAstLoweringExt;
22
use rustc_ast::visit::{self, BoundKind, LifetimeCtxt, Visitor};
33
use rustc_ast::{
4-
FnRetTy, GenericBounds, Lifetime, NodeId, PolyTraitRef, TraitBoundModifier, Ty, TyKind,
4+
FnRetTy, GenericBounds, Lifetime, NodeId, PathSegment, PolyTraitRef, TraitBoundModifier, Ty,
5+
TyKind,
56
};
67
use rustc_hir::def::LifetimeRes;
78
use rustc_middle::ty::ResolverAstLowering;
9+
use rustc_span::symbol::{kw, Ident};
10+
use rustc_span::Span;
811

9-
struct LifetimeCollectVisitor<'this, 'ast: 'this> {
10-
resolver: &'this ResolverAstLowering,
12+
struct LifetimeCollectVisitor<'ast> {
13+
resolver: &'ast ResolverAstLowering,
1114
current_binders: Vec<NodeId>,
12-
collected_lifetimes: Vec<&'ast Lifetime>,
15+
collected_lifetimes: Vec<Lifetime>,
1316
}
1417

15-
impl<'this, 'ast: 'this> LifetimeCollectVisitor<'this, 'ast> {
16-
fn new(resolver: &'this ResolverAstLowering) -> Self {
18+
impl<'ast> LifetimeCollectVisitor<'ast> {
19+
fn new(resolver: &'ast ResolverAstLowering) -> Self {
1720
Self { resolver, current_binders: Vec::new(), collected_lifetimes: Vec::new() }
1821
}
19-
}
2022

21-
impl<'this, 'ast: 'this> Visitor<'ast> for LifetimeCollectVisitor<'this, 'ast> {
22-
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
23+
fn record_lifetime_use(&mut self, lifetime: Lifetime) {
2324
let res = self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error);
2425

2526
if res.binder().map_or(true, |b| !self.current_binders.contains(&b)) {
@@ -28,6 +29,25 @@ impl<'this, 'ast: 'this> Visitor<'ast> for LifetimeCollectVisitor<'this, 'ast> {
2829
}
2930
}
3031
}
32+
}
33+
34+
impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> {
35+
fn visit_lifetime(&mut self, lifetime: &'ast Lifetime, _: LifetimeCtxt) {
36+
self.record_lifetime_use(*lifetime);
37+
}
38+
39+
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'ast PathSegment) {
40+
if let Some(LifetimeRes::ElidedAnchor { start, end }) =
41+
self.resolver.get_lifetime_res(path_segment.id)
42+
{
43+
for i in start..end {
44+
let lifetime =
45+
Lifetime { id: i, ident: Ident::new(kw::UnderscoreLifetime, path_span) };
46+
self.record_lifetime_use(lifetime);
47+
}
48+
}
49+
visit::walk_path_segment(self, path_span, path_segment);
50+
}
3151

3252
fn visit_poly_trait_ref(&mut self, t: &'ast PolyTraitRef, m: &'ast TraitBoundModifier) {
3353
self.current_binders.push(t.trait_ref.ref_id);
@@ -51,19 +71,16 @@ impl<'this, 'ast: 'this> Visitor<'ast> for LifetimeCollectVisitor<'this, 'ast> {
5171
}
5272
}
5373

54-
pub fn lifetimes_in_ret_ty<'this, 'ast: 'this>(
55-
resolver: &'this ResolverAstLowering,
56-
ret_ty: &'ast FnRetTy,
57-
) -> Vec<&'ast Lifetime> {
74+
pub fn lifetimes_in_ret_ty(resolver: &ResolverAstLowering, ret_ty: &FnRetTy) -> Vec<Lifetime> {
5875
let mut visitor = LifetimeCollectVisitor::new(resolver);
5976
visitor.visit_fn_ret_ty(ret_ty);
6077
visitor.collected_lifetimes
6178
}
6279

63-
pub fn lifetimes_in_bounds<'this, 'ast: 'this>(
64-
resolver: &'this ResolverAstLowering,
65-
bounds: &'ast GenericBounds,
66-
) -> Vec<&'ast Lifetime> {
80+
pub fn lifetimes_in_bounds(
81+
resolver: &ResolverAstLowering,
82+
bounds: &GenericBounds,
83+
) -> Vec<Lifetime> {
6784
let mut visitor = LifetimeCollectVisitor::new(resolver);
6885
for bound in bounds {
6986
visitor.visit_param_bound(bound, BoundKind::Bound);

0 commit comments

Comments
 (0)