@@ -91,11 +91,26 @@ pub struct HirFormatter<'a> {
91
91
show_container_bounds : bool ,
92
92
omit_verbose_types : bool ,
93
93
closure_style : ClosureStyle ,
94
+ display_lifetimes : DisplayLifetime ,
94
95
display_kind : DisplayKind ,
95
96
display_target : DisplayTarget ,
96
97
bounds_formatting_ctx : BoundsFormattingCtx ,
97
98
}
98
99
100
+ // FIXME: To consider, ref and dyn trait lifetimes can be omitted if they are `'_`, path args should
101
+ // not be when in signatures
102
+ // So this enum does not encode this well enough
103
+ // Also 'static can be omitted for ref and dyn trait lifetimes in static/const item types
104
+ // FIXME: Also named lifetimes may be rendered in places where their name is not in scope?
105
+ #[ derive( Copy , Clone ) ]
106
+ pub enum DisplayLifetime {
107
+ Always ,
108
+ OnlyStatic ,
109
+ OnlyNamed ,
110
+ OnlyNamedOrStatic ,
111
+ Never ,
112
+ }
113
+
99
114
#[ derive( Default ) ]
100
115
enum BoundsFormattingCtx {
101
116
Entered {
@@ -156,6 +171,21 @@ impl HirFormatter<'_> {
156
171
}
157
172
}
158
173
}
174
+
175
+ fn render_lifetime ( & self , lifetime : & Lifetime ) -> bool {
176
+ match self . display_lifetimes {
177
+ DisplayLifetime :: Always => true ,
178
+ DisplayLifetime :: OnlyStatic => matches ! ( * * * lifetime. interned( ) , LifetimeData :: Static ) ,
179
+ DisplayLifetime :: OnlyNamed => {
180
+ matches ! ( * * * lifetime. interned( ) , LifetimeData :: Placeholder ( _) )
181
+ }
182
+ DisplayLifetime :: OnlyNamedOrStatic => matches ! (
183
+ * * * lifetime. interned( ) ,
184
+ LifetimeData :: Static | LifetimeData :: Placeholder ( _)
185
+ ) ,
186
+ DisplayLifetime :: Never => false ,
187
+ }
188
+ }
159
189
}
160
190
161
191
pub trait HirDisplay {
@@ -190,6 +220,7 @@ pub trait HirDisplay {
190
220
display_kind,
191
221
closure_style,
192
222
show_container_bounds,
223
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
193
224
}
194
225
}
195
226
@@ -213,6 +244,7 @@ pub trait HirDisplay {
213
244
display_target,
214
245
display_kind : DisplayKind :: Diagnostics ,
215
246
show_container_bounds : false ,
247
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
216
248
}
217
249
}
218
250
@@ -237,6 +269,7 @@ pub trait HirDisplay {
237
269
display_target,
238
270
display_kind : DisplayKind :: Diagnostics ,
239
271
show_container_bounds : false ,
272
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
240
273
}
241
274
}
242
275
@@ -261,6 +294,7 @@ pub trait HirDisplay {
261
294
display_target,
262
295
display_kind : DisplayKind :: Diagnostics ,
263
296
show_container_bounds : false ,
297
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
264
298
}
265
299
}
266
300
@@ -285,6 +319,7 @@ pub trait HirDisplay {
285
319
display_target : DisplayTarget :: from_crate ( db, module_id. krate ( ) ) ,
286
320
display_kind : DisplayKind :: SourceCode { target_module_id : module_id, allow_opaque } ,
287
321
show_container_bounds : false ,
322
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
288
323
bounds_formatting_ctx : Default :: default ( ) ,
289
324
} ) {
290
325
Ok ( ( ) ) => { }
@@ -313,6 +348,7 @@ pub trait HirDisplay {
313
348
display_target,
314
349
display_kind : DisplayKind :: Test ,
315
350
show_container_bounds : false ,
351
+ display_lifetimes : DisplayLifetime :: Always ,
316
352
}
317
353
}
318
354
@@ -337,6 +373,7 @@ pub trait HirDisplay {
337
373
display_target,
338
374
display_kind : DisplayKind :: Diagnostics ,
339
375
show_container_bounds,
376
+ display_lifetimes : DisplayLifetime :: OnlyNamedOrStatic ,
340
377
}
341
378
}
342
379
}
@@ -481,6 +518,7 @@ pub struct HirDisplayWrapper<'a, T> {
481
518
display_kind : DisplayKind ,
482
519
display_target : DisplayTarget ,
483
520
show_container_bounds : bool ,
521
+ display_lifetimes : DisplayLifetime ,
484
522
}
485
523
486
524
#[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
@@ -503,7 +541,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
503
541
self . t . hir_fmt ( & mut HirFormatter {
504
542
db : self . db ,
505
543
fmt : f,
506
- buf : String :: with_capacity ( 20 ) ,
544
+ buf : String :: with_capacity ( self . max_size . unwrap_or ( 20 ) ) ,
507
545
curr_size : 0 ,
508
546
max_size : self . max_size ,
509
547
entity_limit : self . limited_size ,
@@ -512,6 +550,7 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
512
550
display_target : self . display_target ,
513
551
closure_style : self . closure_style ,
514
552
show_container_bounds : self . show_container_bounds ,
553
+ display_lifetimes : self . display_lifetimes ,
515
554
bounds_formatting_ctx : Default :: default ( ) ,
516
555
} )
517
556
}
@@ -520,6 +559,11 @@ impl<T: HirDisplay> HirDisplayWrapper<'_, T> {
520
559
self . closure_style = c;
521
560
self
522
561
}
562
+
563
+ pub fn with_lifetime_display ( mut self , l : DisplayLifetime ) -> Self {
564
+ self . display_lifetimes = l;
565
+ self
566
+ }
523
567
}
524
568
525
569
impl < T > fmt:: Display for HirDisplayWrapper < ' _ , T >
@@ -1024,9 +1068,7 @@ impl HirDisplay for Ty {
1024
1068
kind @ ( TyKind :: Raw ( m, t) | TyKind :: Ref ( m, _, t) ) => {
1025
1069
if let TyKind :: Ref ( _, l, _) = kind {
1026
1070
f. write_char ( '&' ) ?;
1027
- if cfg ! ( test) {
1028
- // rendering these unconditionally is probably too much (at least for inlay
1029
- // hints) so we gate it to testing only for the time being
1071
+ if f. render_lifetime ( l) {
1030
1072
l. hir_fmt ( f) ?;
1031
1073
f. write_char ( ' ' ) ?;
1032
1074
}
@@ -1057,9 +1099,10 @@ impl HirDisplay for Ty {
1057
1099
} )
1058
1100
} ;
1059
1101
let ( preds_to_print, has_impl_fn_pred) = match t. kind ( Interner ) {
1060
- TyKind :: Dyn ( dyn_ty) if dyn_ty . bounds . skip_binders ( ) . interned ( ) . len ( ) > 1 => {
1102
+ TyKind :: Dyn ( dyn_ty) => {
1061
1103
let bounds = dyn_ty. bounds . skip_binders ( ) . interned ( ) ;
1062
- ( bounds. len ( ) , contains_impl_fn ( bounds) )
1104
+ let render_lifetime = f. render_lifetime ( & dyn_ty. lifetime ) ;
1105
+ ( bounds. len ( ) + render_lifetime as usize , contains_impl_fn ( bounds) )
1063
1106
}
1064
1107
TyKind :: Alias ( AliasTy :: Opaque ( OpaqueTy {
1065
1108
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" ,
@@ -1991,7 +2045,6 @@ impl HirDisplay for LifetimeData {
1991
2045
write ! ( f, "{}" , param_data. name. display( f. db, f. edition( ) ) ) ?;
1992
2046
Ok ( ( ) )
1993
2047
}
1994
- _ if f. display_kind . is_source_code ( ) => write ! ( f, "'_" ) ,
1995
2048
LifetimeData :: BoundVar ( idx) => idx. hir_fmt ( f) ,
1996
2049
LifetimeData :: InferenceVar ( _) => write ! ( f, "_" ) ,
1997
2050
LifetimeData :: Static => write ! ( f, "'static" ) ,
0 commit comments