@@ -90,11 +90,26 @@ pub struct HirFormatter<'a> {
90
90
show_container_bounds : bool ,
91
91
omit_verbose_types : bool ,
92
92
closure_style : ClosureStyle ,
93
+ display_lifetimes : DisplayLifetime ,
93
94
display_kind : DisplayKind ,
94
95
display_target : DisplayTarget ,
95
96
bounds_formatting_ctx : BoundsFormattingCtx ,
96
97
}
97
98
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
+
98
113
#[ derive( Default ) ]
99
114
enum BoundsFormattingCtx {
100
115
Entered {
@@ -155,6 +170,21 @@ impl HirFormatter<'_> {
155
170
}
156
171
}
157
172
}
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
+ }
158
188
}
159
189
160
190
pub trait HirDisplay {
@@ -189,6 +219,7 @@ pub trait HirDisplay {
189
219
display_kind,
190
220
closure_style,
191
221
show_container_bounds,
222
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
192
223
}
193
224
}
194
225
@@ -212,6 +243,7 @@ pub trait HirDisplay {
212
243
display_target,
213
244
display_kind : DisplayKind :: Diagnostics ,
214
245
show_container_bounds : false ,
246
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
215
247
}
216
248
}
217
249
@@ -236,6 +268,7 @@ pub trait HirDisplay {
236
268
display_target,
237
269
display_kind : DisplayKind :: Diagnostics ,
238
270
show_container_bounds : false ,
271
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
239
272
}
240
273
}
241
274
@@ -260,6 +293,7 @@ pub trait HirDisplay {
260
293
display_target,
261
294
display_kind : DisplayKind :: Diagnostics ,
262
295
show_container_bounds : false ,
296
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
263
297
}
264
298
}
265
299
@@ -284,6 +318,7 @@ pub trait HirDisplay {
284
318
display_target : DisplayTarget :: from_crate ( db, module_id. krate ( ) ) ,
285
319
display_kind : DisplayKind :: SourceCode { target_module_id : module_id, allow_opaque } ,
286
320
show_container_bounds : false ,
321
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
287
322
bounds_formatting_ctx : Default :: default ( ) ,
288
323
} ) {
289
324
Ok ( ( ) ) => { }
@@ -312,6 +347,7 @@ pub trait HirDisplay {
312
347
display_target,
313
348
display_kind : DisplayKind :: Test ,
314
349
show_container_bounds : false ,
350
+ display_lifetimes : DisplayLifetime :: Always ,
315
351
}
316
352
}
317
353
@@ -336,6 +372,7 @@ pub trait HirDisplay {
336
372
display_target,
337
373
display_kind : DisplayKind :: Diagnostics ,
338
374
show_container_bounds,
375
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
339
376
}
340
377
}
341
378
}
@@ -480,6 +517,7 @@ pub struct HirDisplayWrapper<'a, T> {
480
517
display_kind : DisplayKind ,
481
518
display_target : DisplayTarget ,
482
519
show_container_bounds : bool ,
520
+ display_lifetimes : DisplayLifetime ,
483
521
}
484
522
485
523
#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
@@ -502,7 +540,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
502
540
self . t . hir_fmt ( & mut HirFormatter {
503
541
db : self . db ,
504
542
fmt : f,
505
- buf : String :: with_capacity ( 20 ) ,
543
+ buf : String :: with_capacity ( self . max_size . unwrap_or ( 20 ) ) ,
506
544
curr_size : 0 ,
507
545
max_size : self . max_size ,
508
546
entity_limit : self . limited_size ,
@@ -511,6 +549,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
511
549
display_target : self . display_target ,
512
550
closure_style : self . closure_style ,
513
551
show_container_bounds : self . show_container_bounds ,
552
+ display_lifetimes : self . display_lifetimes ,
514
553
bounds_formatting_ctx : Default :: default ( ) ,
515
554
} )
516
555
}
@@ -519,6 +558,11 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
519
558
self . closure_style = c;
520
559
self
521
560
}
561
+
562
+ pub fn with_lifetime_display ( mut self , l : DisplayLifetime ) -> Self {
563
+ self . display_lifetimes = l;
564
+ self
565
+ }
522
566
}
523
567
524
568
impl < T > fmt:: Display for HirDisplayWrapper < ' _ , T >
@@ -1022,9 +1066,7 @@ impl HirDisplay for Ty {
1022
1066
kind @ ( TyKind :: Raw ( m, t) | TyKind :: Ref ( m, _, t) ) => {
1023
1067
if let TyKind :: Ref ( _, l, _) = kind {
1024
1068
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) {
1028
1070
l. hir_fmt ( f) ?;
1029
1071
f. write_char ( ' ' ) ?;
1030
1072
}
@@ -1055,9 +1097,10 @@ impl HirDisplay for Ty {
1055
1097
} )
1056
1098
} ;
1057
1099
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) => {
1059
1101
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) )
1061
1104
}
1062
1105
TyKind :: Alias ( AliasTy :: Opaque ( OpaqueTy {
1063
1106
opaque_ty_id,
@@ -1479,7 +1522,7 @@ impl HirDisplay for Ty {
1479
1522
TyKind :: BoundVar ( idx) => idx. hir_fmt ( f) ?,
1480
1523
TyKind :: Dyn ( dyn_ty) => {
1481
1524
// 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
1483
1526
// more efficient when either of them hits stable.
1484
1527
let mut bounds: SmallVec < [ _ ; 4 ] > =
1485
1528
dyn_ty. bounds . skip_binders ( ) . iter ( Interner ) . cloned ( ) . collect ( ) ;
@@ -1488,6 +1531,17 @@ impl HirDisplay for Ty {
1488
1531
bounds. extend ( others) ;
1489
1532
bounds. extend ( auto_traits) ;
1490
1533
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
+
1491
1545
write_bounds_like_dyn_trait_with_prefix (
1492
1546
f,
1493
1547
"dyn" ,
@@ -1989,7 +2043,6 @@ impl HirDisplay for LifetimeData {
1989
2043
write ! ( f, "{}" , param_data. name. display( f. db, f. edition( ) ) ) ?;
1990
2044
Ok ( ( ) )
1991
2045
}
1992
- _ if f. display_kind . is_source_code ( ) => write ! ( f, "'_" ) ,
1993
2046
LifetimeData :: BoundVar ( idx) => idx. hir_fmt ( f) ,
1994
2047
LifetimeData :: InferenceVar ( _) => write ! ( f, "_" ) ,
1995
2048
LifetimeData :: Static => write ! ( f, "'static" ) ,
0 commit comments