@@ -29,15 +29,15 @@ use std::fmt;
29
29
#[ derive( Debug , Copy , Clone , Encodable , HashStable_Generic ) ]
30
30
pub struct Lifetime {
31
31
pub hir_id : HirId ,
32
- pub span : Span ,
32
+ pub ident : Ident ,
33
33
34
34
/// Either "`'a`", referring to a named lifetime definition,
35
35
/// or "``" (i.e., `kw::Empty`), for elision placeholders.
36
36
///
37
37
/// HIR lowering inserts these placeholders in type paths that
38
38
/// refer to type definitions needing lifetime parameters,
39
39
/// `&T` and `&mut T`, and trait objects without `... + 'a`.
40
- pub name : LifetimeName ,
40
+ pub res : LifetimeName ,
41
41
}
42
42
43
43
#[ derive( Debug , Clone , PartialEq , Eq , Encodable , Hash , Copy ) ]
@@ -88,7 +88,7 @@ impl ParamName {
88
88
#[ derive( HashStable_Generic ) ]
89
89
pub enum LifetimeName {
90
90
/// User-given names or fresh (synthetic) names.
91
- Param ( LocalDefId , ParamName ) ,
91
+ Param ( LocalDefId ) ,
92
92
93
93
/// Implicit lifetime in a context like `dyn Foo`. This is
94
94
/// distinguished from implicit lifetimes elsewhere because the
@@ -116,25 +116,6 @@ pub enum LifetimeName {
116
116
}
117
117
118
118
impl LifetimeName {
119
- pub fn ident ( & self ) -> Ident {
120
- match * self {
121
- LifetimeName :: ImplicitObjectLifetimeDefault | LifetimeName :: Error => Ident :: empty ( ) ,
122
- LifetimeName :: Infer => Ident :: with_dummy_span ( kw:: UnderscoreLifetime ) ,
123
- LifetimeName :: Static => Ident :: with_dummy_span ( kw:: StaticLifetime ) ,
124
- LifetimeName :: Param ( _, param_name) => param_name. ident ( ) ,
125
- }
126
- }
127
-
128
- pub fn is_anonymous ( & self ) -> bool {
129
- match * self {
130
- LifetimeName :: ImplicitObjectLifetimeDefault
131
- | LifetimeName :: Infer
132
- | LifetimeName :: Param ( _, ParamName :: Fresh )
133
- | LifetimeName :: Error => true ,
134
- LifetimeName :: Static | LifetimeName :: Param ( ..) => false ,
135
- }
136
- }
137
-
138
119
pub fn is_elided ( & self ) -> bool {
139
120
match self {
140
121
LifetimeName :: ImplicitObjectLifetimeDefault | LifetimeName :: Infer => true ,
@@ -146,34 +127,25 @@ impl LifetimeName {
146
127
LifetimeName :: Error | LifetimeName :: Param ( ..) | LifetimeName :: Static => false ,
147
128
}
148
129
}
149
-
150
- fn is_static ( & self ) -> bool {
151
- self == & LifetimeName :: Static
152
- }
153
-
154
- pub fn normalize_to_macros_2_0 ( & self ) -> LifetimeName {
155
- match * self {
156
- LifetimeName :: Param ( def_id, param_name) => {
157
- LifetimeName :: Param ( def_id, param_name. normalize_to_macros_2_0 ( ) )
158
- }
159
- lifetime_name => lifetime_name,
160
- }
161
- }
162
130
}
163
131
164
132
impl fmt:: Display for Lifetime {
165
133
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
166
- self . name . ident ( ) . fmt ( f)
134
+ self . ident . fmt ( f)
167
135
}
168
136
}
169
137
170
138
impl Lifetime {
171
139
pub fn is_elided ( & self ) -> bool {
172
- self . name . is_elided ( )
140
+ self . res . is_elided ( )
141
+ }
142
+
143
+ pub fn is_anonymous ( & self ) -> bool {
144
+ self . ident . name == kw:: Empty || self . ident . name == kw:: UnderscoreLifetime
173
145
}
174
146
175
147
pub fn is_static ( & self ) -> bool {
176
- self . name . is_static ( )
148
+ self . res == LifetimeName :: Static
177
149
}
178
150
}
179
151
@@ -267,7 +239,7 @@ pub enum GenericArg<'hir> {
267
239
impl GenericArg < ' _ > {
268
240
pub fn span ( & self ) -> Span {
269
241
match self {
270
- GenericArg :: Lifetime ( l) => l. span ,
242
+ GenericArg :: Lifetime ( l) => l. ident . span ,
271
243
GenericArg :: Type ( t) => t. span ,
272
244
GenericArg :: Const ( c) => c. span ,
273
245
GenericArg :: Infer ( i) => i. span ,
@@ -284,7 +256,7 @@ impl GenericArg<'_> {
284
256
}
285
257
286
258
pub fn is_synthetic ( & self ) -> bool {
287
- matches ! ( self , GenericArg :: Lifetime ( lifetime) if lifetime. name . ident( ) == Ident :: empty( ) )
259
+ matches ! ( self , GenericArg :: Lifetime ( lifetime) if lifetime. ident == Ident :: empty( ) )
288
260
}
289
261
290
262
pub fn descr ( & self ) -> & ' static str {
@@ -446,7 +418,7 @@ impl GenericBound<'_> {
446
418
match self {
447
419
GenericBound :: Trait ( t, ..) => t. span ,
448
420
GenericBound :: LangItemTrait ( _, span, ..) => * span,
449
- GenericBound :: Outlives ( l) => l. span ,
421
+ GenericBound :: Outlives ( l) => l. ident . span ,
450
422
}
451
423
}
452
424
}
@@ -559,6 +531,19 @@ impl<'hir> Generics<'hir> {
559
531
}
560
532
}
561
533
534
+ /// If there are generic parameters, return where to introduce a new one.
535
+ pub fn span_for_lifetime_suggestion ( & self ) -> Option < Span > {
536
+ if let Some ( first) = self . params . first ( )
537
+ && self . span . contains ( first. span )
538
+ {
539
+ // `fn foo<A>(t: impl Trait)`
540
+ // ^ suggest `'a, ` here
541
+ Some ( first. span . shrink_to_lo ( ) )
542
+ } else {
543
+ None
544
+ }
545
+ }
546
+
562
547
/// If there are generic parameters, return where to introduce a new one.
563
548
pub fn span_for_param_suggestion ( & self ) -> Option < Span > {
564
549
if self . params . iter ( ) . any ( |p| self . span . contains ( p. span ) ) {
@@ -765,10 +750,7 @@ pub struct WhereRegionPredicate<'hir> {
765
750
impl < ' hir > WhereRegionPredicate < ' hir > {
766
751
/// Returns `true` if `param_def_id` matches the `lifetime` of this predicate.
767
752
pub fn is_param_bound ( & self , param_def_id : LocalDefId ) -> bool {
768
- match self . lifetime . name {
769
- LifetimeName :: Param ( id, _) => id == param_def_id,
770
- _ => false ,
771
- }
753
+ self . lifetime . res == LifetimeName :: Param ( param_def_id)
772
754
}
773
755
}
774
756
@@ -3453,7 +3435,7 @@ impl<'hir> Node<'hir> {
3453
3435
| Node :: Variant ( Variant { ident, .. } )
3454
3436
| Node :: Item ( Item { ident, .. } )
3455
3437
| Node :: PathSegment ( PathSegment { ident, .. } ) => Some ( * ident) ,
3456
- Node :: Lifetime ( lt) => Some ( lt. name . ident ( ) ) ,
3438
+ Node :: Lifetime ( lt) => Some ( lt. ident ) ,
3457
3439
Node :: GenericParam ( p) => Some ( p. name . ident ( ) ) ,
3458
3440
Node :: TypeBinding ( b) => Some ( b. ident ) ,
3459
3441
Node :: Param ( ..)
0 commit comments