1
1
use super :: ResolverAstLoweringExt ;
2
2
use rustc_ast:: visit:: { self , BoundKind , LifetimeCtxt , Visitor } ;
3
3
use rustc_ast:: {
4
- FnRetTy , GenericBounds , Lifetime , NodeId , PolyTraitRef , TraitBoundModifier , Ty , TyKind ,
4
+ FnRetTy , GenericBounds , Lifetime , NodeId , PathSegment , PolyTraitRef , TraitBoundModifier , Ty ,
5
+ TyKind ,
5
6
} ;
6
7
use rustc_hir:: def:: LifetimeRes ;
7
8
use rustc_middle:: ty:: ResolverAstLowering ;
9
+ use rustc_span:: symbol:: { kw, Ident } ;
10
+ use rustc_span:: Span ;
8
11
9
- struct LifetimeCollectVisitor < ' this , ' ast : ' this > {
10
- resolver : & ' this ResolverAstLowering ,
12
+ struct LifetimeCollectVisitor < ' ast > {
13
+ resolver : & ' ast ResolverAstLowering ,
11
14
current_binders : Vec < NodeId > ,
12
- collected_lifetimes : Vec < & ' ast Lifetime > ,
15
+ collected_lifetimes : Vec < Lifetime > ,
13
16
}
14
17
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 {
17
20
Self { resolver, current_binders : Vec :: new ( ) , collected_lifetimes : Vec :: new ( ) }
18
21
}
19
- }
20
22
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 ) {
23
24
let res = self . resolver . get_lifetime_res ( lifetime. id ) . unwrap_or ( LifetimeRes :: Error ) ;
24
25
25
26
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> {
28
29
}
29
30
}
30
31
}
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
+ }
31
51
32
52
fn visit_poly_trait_ref ( & mut self , t : & ' ast PolyTraitRef , m : & ' ast TraitBoundModifier ) {
33
53
self . current_binders . push ( t. trait_ref . ref_id ) ;
@@ -51,19 +71,16 @@ impl<'this, 'ast: 'this> Visitor<'ast> for LifetimeCollectVisitor<'this, 'ast> {
51
71
}
52
72
}
53
73
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 > {
58
75
let mut visitor = LifetimeCollectVisitor :: new ( resolver) ;
59
76
visitor. visit_fn_ret_ty ( ret_ty) ;
60
77
visitor. collected_lifetimes
61
78
}
62
79
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 > {
67
84
let mut visitor = LifetimeCollectVisitor :: new ( resolver) ;
68
85
for bound in bounds {
69
86
visitor. visit_param_bound ( bound, BoundKind :: Bound ) ;
0 commit comments