@@ -90,11 +90,26 @@ pub struct HirFormatter<'a> {
9090 show_container_bounds : bool ,
9191 omit_verbose_types : bool ,
9292 closure_style : ClosureStyle ,
93+ display_lifetimes : DisplayLifetime ,
9394 display_kind : DisplayKind ,
9495 display_target : DisplayTarget ,
9596 bounds_formatting_ctx : BoundsFormattingCtx ,
9697}
9798
99+ // FIXME: To consider, ref and dyn trait lifetimes can be omitted if they are `'_`, path args should
100+ // not be when in signatures
101+ // So this enum does not encode this well enough
102+ // Also 'static can be omitted for ref and dyn trait lifetimes in static/const item types
103+ // FIXME: Also named lifetimes may be rendered in places where their name is not in scope?
104+ #[ derive( Copy , Clone ) ]
105+ pub enum DisplayLifetime {
106+ Always ,
107+ OnlyStatic ,
108+ OnlyNamed ,
109+ OnlyNamedOrStatic ,
110+ Never ,
111+ }
112+
98113#[ derive( Default ) ]
99114enum BoundsFormattingCtx {
100115 Entered {
@@ -155,6 +170,21 @@ impl HirFormatter<'_> {
155170 }
156171 }
157172 }
173+
174+ fn render_lifetime ( & self , lifetime : & Lifetime ) -> bool {
175+ match self . display_lifetimes {
176+ DisplayLifetime :: Always => true ,
177+ DisplayLifetime :: OnlyStatic => matches ! ( * * * lifetime. interned( ) , LifetimeData :: Static ) ,
178+ DisplayLifetime :: OnlyNamed => {
179+ matches ! ( * * * lifetime. interned( ) , LifetimeData :: Placeholder ( _) )
180+ }
181+ DisplayLifetime :: OnlyNamedOrStatic => matches ! (
182+ * * * lifetime. interned( ) ,
183+ LifetimeData :: Static | LifetimeData :: Placeholder ( _)
184+ ) ,
185+ DisplayLifetime :: Never => false ,
186+ }
187+ }
158188}
159189
160190pub trait HirDisplay {
@@ -189,6 +219,7 @@ pub trait HirDisplay {
189219 display_kind,
190220 closure_style,
191221 show_container_bounds,
222+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
192223 }
193224 }
194225
@@ -212,6 +243,7 @@ pub trait HirDisplay {
212243 display_target,
213244 display_kind : DisplayKind :: Diagnostics ,
214245 show_container_bounds : false ,
246+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
215247 }
216248 }
217249
@@ -236,6 +268,7 @@ pub trait HirDisplay {
236268 display_target,
237269 display_kind : DisplayKind :: Diagnostics ,
238270 show_container_bounds : false ,
271+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
239272 }
240273 }
241274
@@ -260,6 +293,7 @@ pub trait HirDisplay {
260293 display_target,
261294 display_kind : DisplayKind :: Diagnostics ,
262295 show_container_bounds : false ,
296+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
263297 }
264298 }
265299
@@ -284,6 +318,7 @@ pub trait HirDisplay {
284318 display_target : DisplayTarget :: from_crate ( db, module_id. krate ( ) ) ,
285319 display_kind : DisplayKind :: SourceCode { target_module_id : module_id, allow_opaque } ,
286320 show_container_bounds : false ,
321+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
287322 bounds_formatting_ctx : Default :: default ( ) ,
288323 } ) {
289324 Ok ( ( ) ) => { }
@@ -312,6 +347,7 @@ pub trait HirDisplay {
312347 display_target,
313348 display_kind : DisplayKind :: Test ,
314349 show_container_bounds : false ,
350+ display_lifetimes : DisplayLifetime :: Always ,
315351 }
316352 }
317353
@@ -336,6 +372,7 @@ pub trait HirDisplay {
336372 display_target,
337373 display_kind : DisplayKind :: Diagnostics ,
338374 show_container_bounds,
375+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
339376 }
340377 }
341378}
@@ -480,6 +517,7 @@ pub struct HirDisplayWrapper<'a, T> {
480517 display_kind : DisplayKind ,
481518 display_target : DisplayTarget ,
482519 show_container_bounds : bool ,
520+ display_lifetimes : DisplayLifetime ,
483521}
484522
485523#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
@@ -502,7 +540,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
502540 self . t . hir_fmt ( & mut HirFormatter {
503541 db : self . db ,
504542 fmt : f,
505- buf : String :: with_capacity ( 20 ) ,
543+ buf : String :: with_capacity ( self . max_size . unwrap_or ( 20 ) ) ,
506544 curr_size : 0 ,
507545 max_size : self . max_size ,
508546 entity_limit : self . limited_size ,
@@ -511,6 +549,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
511549 display_target : self . display_target ,
512550 closure_style : self . closure_style ,
513551 show_container_bounds : self . show_container_bounds ,
552+ display_lifetimes : self . display_lifetimes ,
514553 bounds_formatting_ctx : Default :: default ( ) ,
515554 } )
516555 }
@@ -519,6 +558,11 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
519558 self . closure_style = c;
520559 self
521560 }
561+
562+ pub fn with_lifetime_display ( mut self , l : DisplayLifetime ) -> Self {
563+ self . display_lifetimes = l;
564+ self
565+ }
522566}
523567
524568impl < T > fmt:: Display for HirDisplayWrapper < ' _ , T >
@@ -1022,9 +1066,7 @@ impl HirDisplay for Ty {
10221066 kind @ ( TyKind :: Raw ( m, t) | TyKind :: Ref ( m, _, t) ) => {
10231067 if let TyKind :: Ref ( _, l, _) = kind {
10241068 f. write_char ( '&' ) ?;
1025- if cfg ! ( test) {
1026- // rendering these unconditionally is probably too much (at least for inlay
1027- // hints) so we gate it to testing only for the time being
1069+ if f. render_lifetime ( l) {
10281070 l. hir_fmt ( f) ?;
10291071 f. write_char ( ' ' ) ?;
10301072 }
@@ -1055,9 +1097,10 @@ impl HirDisplay for Ty {
10551097 } )
10561098 } ;
10571099 let ( preds_to_print, has_impl_fn_pred) = match t. kind ( Interner ) {
1058- TyKind :: Dyn ( dyn_ty) if dyn_ty . bounds . skip_binders ( ) . interned ( ) . len ( ) > 1 => {
1100+ TyKind :: Dyn ( dyn_ty) => {
10591101 let bounds = dyn_ty. bounds . skip_binders ( ) . interned ( ) ;
1060- ( bounds. len ( ) , contains_impl_fn ( bounds) )
1102+ let render_lifetime = f. render_lifetime ( & dyn_ty. lifetime ) ;
1103+ ( bounds. len ( ) + render_lifetime as usize , contains_impl_fn ( bounds) )
10611104 }
10621105 TyKind :: Alias ( AliasTy :: Opaque ( OpaqueTy {
10631106 opaque_ty_id,
@@ -1479,7 +1522,7 @@ impl HirDisplay for Ty {
14791522 TyKind :: BoundVar ( idx) => idx. hir_fmt ( f) ?,
14801523 TyKind :: Dyn ( dyn_ty) => {
14811524 // Reorder bounds to satisfy `write_bounds_like_dyn_trait()`'s expectation.
1482- // FIXME: `Iterator::partition_in_place()` or `Vec::drain_filter ()` may make it
1525+ // FIXME: `Iterator::partition_in_place()` or `Vec::extract_if ()` may make it
14831526 // more efficient when either of them hits stable.
14841527 let mut bounds: SmallVec < [ _ ; 4 ] > =
14851528 dyn_ty. bounds . skip_binders ( ) . iter ( Interner ) . cloned ( ) . collect ( ) ;
@@ -1488,6 +1531,17 @@ impl HirDisplay for Ty {
14881531 bounds. extend ( others) ;
14891532 bounds. extend ( auto_traits) ;
14901533
1534+ if f. render_lifetime ( & dyn_ty. lifetime ) {
1535+ // we skip the binders in `write_bounds_like_dyn_trait_with_prefix`
1536+ bounds. push ( Binders :: empty (
1537+ Interner ,
1538+ chalk_ir:: WhereClause :: TypeOutlives ( chalk_ir:: TypeOutlives {
1539+ ty : self . clone ( ) ,
1540+ lifetime : dyn_ty. lifetime . clone ( ) ,
1541+ } ) ,
1542+ ) ) ;
1543+ }
1544+
14911545 write_bounds_like_dyn_trait_with_prefix (
14921546 f,
14931547 "dyn" ,
@@ -1989,7 +2043,6 @@ impl HirDisplay for LifetimeData {
19892043 write ! ( f, "{}" , param_data. name. display( f. db, f. edition( ) ) ) ?;
19902044 Ok ( ( ) )
19912045 }
1992- _ if f. display_kind . is_source_code ( ) => write ! ( f, "'_" ) ,
19932046 LifetimeData :: BoundVar ( idx) => idx. hir_fmt ( f) ,
19942047 LifetimeData :: InferenceVar ( _) => write ! ( f, "_" ) ,
19952048 LifetimeData :: Static => write ! ( f, "'static" ) ,
0 commit comments