@@ -29,15 +29,15 @@ use std::fmt;
2929#[ derive( Debug , Copy , Clone , Encodable , HashStable_Generic ) ]
3030pub struct Lifetime {
3131 pub hir_id : HirId ,
32- pub span : Span ,
32+ pub ident : Ident ,
3333
3434 /// Either "`'a`", referring to a named lifetime definition,
3535 /// or "``" (i.e., `kw::Empty`), for elision placeholders.
3636 ///
3737 /// HIR lowering inserts these placeholders in type paths that
3838 /// refer to type definitions needing lifetime parameters,
3939 /// `&T` and `&mut T`, and trait objects without `... + 'a`.
40- pub name : LifetimeName ,
40+ pub res : LifetimeName ,
4141}
4242
4343#[ derive( Debug , Clone , PartialEq , Eq , Encodable , Hash , Copy ) ]
@@ -88,7 +88,7 @@ impl ParamName {
8888#[ derive( HashStable_Generic ) ]
8989pub enum LifetimeName {
9090 /// User-given names or fresh (synthetic) names.
91- Param ( LocalDefId , ParamName ) ,
91+ Param ( LocalDefId ) ,
9292
9393 /// Implicit lifetime in a context like `dyn Foo`. This is
9494 /// distinguished from implicit lifetimes elsewhere because the
@@ -116,25 +116,6 @@ pub enum LifetimeName {
116116}
117117
118118impl 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-
138119 pub fn is_elided ( & self ) -> bool {
139120 match self {
140121 LifetimeName :: ImplicitObjectLifetimeDefault | LifetimeName :: Infer => true ,
@@ -146,34 +127,25 @@ impl LifetimeName {
146127 LifetimeName :: Error | LifetimeName :: Param ( ..) | LifetimeName :: Static => false ,
147128 }
148129 }
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- }
162130}
163131
164132impl fmt:: Display for Lifetime {
165133 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
166- self . name . ident ( ) . fmt ( f)
134+ self . ident . fmt ( f)
167135 }
168136}
169137
170138impl Lifetime {
171139 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
173145 }
174146
175147 pub fn is_static ( & self ) -> bool {
176- self . name . is_static ( )
148+ self . res == LifetimeName :: Static
177149 }
178150}
179151
@@ -267,7 +239,7 @@ pub enum GenericArg<'hir> {
267239impl GenericArg < ' _ > {
268240 pub fn span ( & self ) -> Span {
269241 match self {
270- GenericArg :: Lifetime ( l) => l. span ,
242+ GenericArg :: Lifetime ( l) => l. ident . span ,
271243 GenericArg :: Type ( t) => t. span ,
272244 GenericArg :: Const ( c) => c. span ,
273245 GenericArg :: Infer ( i) => i. span ,
@@ -284,7 +256,7 @@ impl GenericArg<'_> {
284256 }
285257
286258 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( ) )
288260 }
289261
290262 pub fn descr ( & self ) -> & ' static str {
@@ -446,7 +418,7 @@ impl GenericBound<'_> {
446418 match self {
447419 GenericBound :: Trait ( t, ..) => t. span ,
448420 GenericBound :: LangItemTrait ( _, span, ..) => * span,
449- GenericBound :: Outlives ( l) => l. span ,
421+ GenericBound :: Outlives ( l) => l. ident . span ,
450422 }
451423 }
452424}
@@ -559,6 +531,19 @@ impl<'hir> Generics<'hir> {
559531 }
560532 }
561533
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+
562547 /// If there are generic parameters, return where to introduce a new one.
563548 pub fn span_for_param_suggestion ( & self ) -> Option < Span > {
564549 if self . params . iter ( ) . any ( |p| self . span . contains ( p. span ) ) {
@@ -765,10 +750,7 @@ pub struct WhereRegionPredicate<'hir> {
765750impl < ' hir > WhereRegionPredicate < ' hir > {
766751 /// Returns `true` if `param_def_id` matches the `lifetime` of this predicate.
767752 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)
772754 }
773755}
774756
@@ -3453,7 +3435,7 @@ impl<'hir> Node<'hir> {
34533435 | Node :: Variant ( Variant { ident, .. } )
34543436 | Node :: Item ( Item { ident, .. } )
34553437 | Node :: PathSegment ( PathSegment { ident, .. } ) => Some ( * ident) ,
3456- Node :: Lifetime ( lt) => Some ( lt. name . ident ( ) ) ,
3438+ Node :: Lifetime ( lt) => Some ( lt. ident ) ,
34573439 Node :: GenericParam ( p) => Some ( p. name . ident ( ) ) ,
34583440 Node :: TypeBinding ( b) => Some ( b. ident ) ,
34593441 Node :: Param ( ..)
0 commit comments