@@ -64,6 +64,14 @@ use crate::{
64
64
pub trait HirWrite : fmt:: Write {
65
65
fn start_location_link ( & mut self , _location : ModuleDefId ) { }
66
66
fn end_location_link ( & mut self ) { }
67
+ /// Returns whether a new truncated part is created during this call. If so,
68
+ /// the caller should call `end_truncated` at the end. Otherwise, the caller
69
+ /// should not call `end_truncated`. This ensures that the truncated part is
70
+ /// not nested.
71
+ fn start_truncated ( & mut self ) -> bool {
72
+ false
73
+ }
74
+ fn end_truncated ( & mut self ) { }
67
75
}
68
76
69
77
// String will ignore link metadata
@@ -392,19 +400,23 @@ impl HirFormatter<'_> {
392
400
sep : & str ,
393
401
) -> Result < ( ) , HirDisplayError > {
394
402
let mut first = true ;
403
+ let mut in_truncated = false ;
395
404
for e in iter {
396
405
if !first {
397
406
write ! ( self , "{sep}" ) ?;
398
407
}
399
408
first = false ;
400
409
401
410
// Abbreviate multiple omitted types with a single ellipsis.
402
- if self . should_truncate ( ) {
403
- return write ! ( self , "{TYPE_HINT_TRUNCATION}" ) ;
404
- }
411
+ in_truncated = !in_truncated && self . should_truncate ( ) && self . fmt . start_truncated ( ) ;
405
412
406
413
e. hir_fmt ( self ) ?;
407
414
}
415
+
416
+ if in_truncated {
417
+ self . fmt . end_truncated ( ) ;
418
+ }
419
+
408
420
Ok ( ( ) )
409
421
}
410
422
@@ -599,9 +611,8 @@ impl<T: HirDisplay + Internable> HirDisplay for Interned<T> {
599
611
600
612
impl HirDisplay for ProjectionTy {
601
613
fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
602
- if f. should_truncate ( ) {
603
- return write ! ( f, "{TYPE_HINT_TRUNCATION}" ) ;
604
- }
614
+ let in_truncated = f. should_truncate ( ) && f. fmt . start_truncated ( ) ;
615
+
605
616
let trait_ref = self . trait_ref ( f. db ) ;
606
617
let self_ty = trait_ref. self_type_parameter ( Interner ) ;
607
618
@@ -664,19 +675,30 @@ impl HirDisplay for ProjectionTy {
664
675
. name
665
676
. display( f. db, f. edition( ) )
666
677
) ?;
678
+
667
679
let proj_params =
668
680
& self . substitution . as_slice ( Interner ) [ trait_ref. substitution . len ( Interner ) ..] ;
669
- hir_fmt_generics ( f, proj_params, None , None )
681
+ hir_fmt_generics ( f, proj_params, None , None ) ?;
682
+
683
+ if in_truncated {
684
+ f. fmt . end_truncated ( ) ;
685
+ }
686
+
687
+ Ok ( ( ) )
670
688
}
671
689
}
672
690
673
691
impl HirDisplay for OpaqueTy {
674
692
fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
675
- if f. should_truncate ( ) {
676
- return write ! ( f, "{TYPE_HINT_TRUNCATION}" ) ;
693
+ let in_truncated = f. should_truncate ( ) && f. fmt . start_truncated ( ) ;
694
+
695
+ self . substitution . at ( Interner , 0 ) . hir_fmt ( f) ?;
696
+
697
+ if in_truncated {
698
+ f. fmt . end_truncated ( ) ;
677
699
}
678
700
679
- self . substitution . at ( Interner , 0 ) . hir_fmt ( f )
701
+ Ok ( ( ) )
680
702
}
681
703
}
682
704
@@ -1039,9 +1061,7 @@ impl HirDisplay for Ty {
1039
1061
& self ,
1040
1062
f @ & mut HirFormatter { db, .. } : & mut HirFormatter < ' _ > ,
1041
1063
) -> Result < ( ) , HirDisplayError > {
1042
- if f. should_truncate ( ) {
1043
- return write ! ( f, "{TYPE_HINT_TRUNCATION}" ) ;
1044
- }
1064
+ let in_truncated = f. should_truncate ( ) && f. fmt . start_truncated ( ) ;
1045
1065
1046
1066
match self . kind ( Interner ) {
1047
1067
TyKind :: Never => write ! ( f, "!" ) ?,
@@ -1451,11 +1471,15 @@ impl HirDisplay for Ty {
1451
1471
_ => unreachable ! ( ) ,
1452
1472
}
1453
1473
if sig. params ( ) . is_empty ( ) {
1454
- } else if f. should_truncate ( ) {
1455
- write ! ( f, "{TYPE_HINT_TRUNCATION}" ) ?;
1456
1474
} else {
1475
+ let in_truncated = f. should_truncate ( ) && f. fmt . start_truncated ( ) ;
1476
+
1457
1477
f. write_joined ( sig. params ( ) , ", " ) ?;
1458
- } ;
1478
+
1479
+ if in_truncated {
1480
+ f. fmt . end_truncated ( ) ;
1481
+ }
1482
+ }
1459
1483
match f. closure_style {
1460
1484
ClosureStyle :: ImplFn => write ! ( f, ")" ) ?,
1461
1485
ClosureStyle :: RANotation => write ! ( f, "|" ) ?,
@@ -1632,6 +1656,11 @@ impl HirDisplay for Ty {
1632
1656
}
1633
1657
TyKind :: CoroutineWitness ( ..) => write ! ( f, "{{coroutine witness}}" ) ?,
1634
1658
}
1659
+
1660
+ if in_truncated {
1661
+ f. fmt . end_truncated ( ) ;
1662
+ }
1663
+
1635
1664
Ok ( ( ) )
1636
1665
}
1637
1666
}
@@ -1983,9 +2012,7 @@ impl HirDisplay for TraitRef {
1983
2012
1984
2013
impl HirDisplay for WhereClause {
1985
2014
fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
1986
- if f. should_truncate ( ) {
1987
- return write ! ( f, "{TYPE_HINT_TRUNCATION}" ) ;
1988
- }
2015
+ let in_truncated = f. should_truncate ( ) && f. fmt . start_truncated ( ) ;
1989
2016
1990
2017
match self {
1991
2018
WhereClause :: Implemented ( trait_ref) => {
@@ -2017,6 +2044,11 @@ impl HirDisplay for WhereClause {
2017
2044
WhereClause :: TypeOutlives ( ..) => { }
2018
2045
WhereClause :: LifetimeOutlives ( ..) => { }
2019
2046
}
2047
+
2048
+ if in_truncated {
2049
+ f. fmt . end_truncated ( ) ;
2050
+ }
2051
+
2020
2052
Ok ( ( ) )
2021
2053
}
2022
2054
}
0 commit comments