4
4
5
5
use std:: {
6
6
fmt:: { self , Debug } ,
7
- mem:: size_of,
7
+ mem:: { self , size_of} ,
8
8
} ;
9
9
10
10
use base_db:: CrateId ;
@@ -460,7 +460,7 @@ impl HirDisplay for ProjectionTy {
460
460
let proj_params_count =
461
461
self . substitution . len ( Interner ) - trait_ref. substitution . len ( Interner ) ;
462
462
let proj_params = & self . substitution . as_slice ( Interner ) [ ..proj_params_count] ;
463
- hir_fmt_generics ( f, proj_params, None )
463
+ hir_fmt_generics ( f, proj_params, None , None )
464
464
}
465
465
}
466
466
@@ -505,6 +505,7 @@ impl HirDisplay for Const {
505
505
f,
506
506
parameters. as_slice ( Interner ) ,
507
507
c. generic_def ( f. db . upcast ( ) ) ,
508
+ None ,
508
509
) ?;
509
510
Ok ( ( ) )
510
511
}
@@ -1004,11 +1005,11 @@ impl HirDisplay for Ty {
1004
1005
let fn_params = generic_args_sans_defaults ( f, Some ( def. into ( ) ) , fn_params) ;
1005
1006
1006
1007
write ! ( f, "<" ) ?;
1007
- hir_fmt_generic_arguments ( f, parent_params) ?;
1008
+ hir_fmt_generic_arguments ( f, parent_params, None ) ?;
1008
1009
if !parent_params. is_empty ( ) && !fn_params. is_empty ( ) {
1009
1010
write ! ( f, ", " ) ?;
1010
1011
}
1011
- hir_fmt_generic_arguments ( f, fn_params) ?;
1012
+ hir_fmt_generic_arguments ( f, fn_params, None ) ?;
1012
1013
write ! ( f, ">" ) ?;
1013
1014
}
1014
1015
}
@@ -1053,7 +1054,7 @@ impl HirDisplay for Ty {
1053
1054
1054
1055
let generic_def = self . as_generic_def ( db) ;
1055
1056
1056
- hir_fmt_generics ( f, parameters. as_slice ( Interner ) , generic_def) ?;
1057
+ hir_fmt_generics ( f, parameters. as_slice ( Interner ) , generic_def, None ) ?;
1057
1058
}
1058
1059
TyKind :: AssociatedType ( assoc_type_id, parameters) => {
1059
1060
let type_alias = from_assoc_type_id ( * assoc_type_id) ;
@@ -1076,7 +1077,7 @@ impl HirDisplay for Ty {
1076
1077
f. end_location_link ( ) ;
1077
1078
// Note that the generic args for the associated type come before those for the
1078
1079
// trait (including the self type).
1079
- hir_fmt_generics ( f, parameters. as_slice ( Interner ) , None )
1080
+ hir_fmt_generics ( f, parameters. as_slice ( Interner ) , None , None )
1080
1081
} else {
1081
1082
let projection_ty = ProjectionTy {
1082
1083
associated_ty_id : to_assoc_type_id ( type_alias) ,
@@ -1178,7 +1179,7 @@ impl HirDisplay for Ty {
1178
1179
}
1179
1180
ClosureStyle :: ClosureWithSubst => {
1180
1181
write ! ( f, "{{closure#{:?}}}" , id. 0 . as_u32( ) ) ?;
1181
- return hir_fmt_generics ( f, substs. as_slice ( Interner ) , None ) ;
1182
+ return hir_fmt_generics ( f, substs. as_slice ( Interner ) , None , None ) ;
1182
1183
}
1183
1184
_ => ( ) ,
1184
1185
}
@@ -1366,6 +1367,7 @@ fn hir_fmt_generics(
1366
1367
f : & mut HirFormatter < ' _ > ,
1367
1368
parameters : & [ GenericArg ] ,
1368
1369
generic_def : Option < hir_def:: GenericDefId > ,
1370
+ self_ : Option < & Ty > ,
1369
1371
) -> Result < ( ) , HirDisplayError > {
1370
1372
if parameters. is_empty ( ) {
1371
1373
return Ok ( ( ) ) ;
@@ -1385,7 +1387,7 @@ fn hir_fmt_generics(
1385
1387
} ) ;
1386
1388
if !parameters_to_write. is_empty ( ) && !only_err_lifetimes {
1387
1389
write ! ( f, "<" ) ?;
1388
- hir_fmt_generic_arguments ( f, parameters_to_write) ?;
1390
+ hir_fmt_generic_arguments ( f, parameters_to_write, self_ ) ?;
1389
1391
write ! ( f, ">" ) ?;
1390
1392
}
1391
1393
@@ -1447,6 +1449,7 @@ fn generic_args_sans_defaults<'ga>(
1447
1449
fn hir_fmt_generic_arguments (
1448
1450
f : & mut HirFormatter < ' _ > ,
1449
1451
parameters : & [ GenericArg ] ,
1452
+ self_ : Option < & Ty > ,
1450
1453
) -> Result < ( ) , HirDisplayError > {
1451
1454
let mut first = true ;
1452
1455
let lifetime_offset = parameters. iter ( ) . position ( |arg| arg. lifetime ( Interner ) . is_some ( ) ) ;
@@ -1468,11 +1471,13 @@ fn hir_fmt_generic_arguments(
1468
1471
continue ;
1469
1472
}
1470
1473
1471
- if !first {
1474
+ if !mem :: take ( & mut first) {
1472
1475
write ! ( f, ", " ) ?;
1473
1476
}
1474
- first = false ;
1475
- generic_arg. hir_fmt ( f) ?;
1477
+ match self_ {
1478
+ self_ @ Some ( _) if generic_arg. ty ( Interner ) == self_ => write ! ( f, "Self" ) ?,
1479
+ _ => generic_arg. hir_fmt ( f) ?,
1480
+ }
1476
1481
}
1477
1482
Ok ( ( ) )
1478
1483
}
@@ -1595,12 +1600,16 @@ fn write_bounds_like_dyn_trait(
1595
1600
write ! ( f, "{}" , f. db. trait_data( trait_) . name. display( f. db. upcast( ) ) ) ?;
1596
1601
f. end_location_link ( ) ;
1597
1602
if is_fn_trait {
1598
- if let [ _self , params @ ..] = trait_ref. substitution . as_slice ( Interner ) {
1603
+ if let [ self_ , params @ ..] = trait_ref. substitution . as_slice ( Interner ) {
1599
1604
if let Some ( args) =
1600
1605
params. first ( ) . and_then ( |it| it. assert_ty_ref ( Interner ) . as_tuple ( ) )
1601
1606
{
1602
1607
write ! ( f, "(" ) ?;
1603
- hir_fmt_generic_arguments ( f, args. as_slice ( Interner ) ) ?;
1608
+ hir_fmt_generic_arguments (
1609
+ f,
1610
+ args. as_slice ( Interner ) ,
1611
+ self_. ty ( Interner ) ,
1612
+ ) ?;
1604
1613
write ! ( f, ")" ) ?;
1605
1614
}
1606
1615
}
@@ -1610,10 +1619,10 @@ fn write_bounds_like_dyn_trait(
1610
1619
Some ( trait_. into ( ) ) ,
1611
1620
trait_ref. substitution . as_slice ( Interner ) ,
1612
1621
) ;
1613
- if let [ _self , params @ ..] = params {
1622
+ if let [ self_ , params @ ..] = params {
1614
1623
if !params. is_empty ( ) {
1615
1624
write ! ( f, "<" ) ?;
1616
- hir_fmt_generic_arguments ( f, params) ?;
1625
+ hir_fmt_generic_arguments ( f, params, self_ . ty ( Interner ) ) ?;
1617
1626
// there might be assoc type bindings, so we leave the angle brackets open
1618
1627
angle_open = true ;
1619
1628
}
@@ -1671,6 +1680,7 @@ fn write_bounds_like_dyn_trait(
1671
1680
hir_fmt_generic_arguments (
1672
1681
f,
1673
1682
& proj. substitution . as_slice ( Interner ) [ ..proj_arg_count] ,
1683
+ None ,
1674
1684
) ?;
1675
1685
write ! ( f, ">" ) ?;
1676
1686
}
@@ -1727,7 +1737,8 @@ fn fmt_trait_ref(
1727
1737
f. start_location_link ( trait_. into ( ) ) ;
1728
1738
write ! ( f, "{}" , f. db. trait_data( trait_) . name. display( f. db. upcast( ) ) ) ?;
1729
1739
f. end_location_link ( ) ;
1730
- hir_fmt_generics ( f, & tr. substitution . as_slice ( Interner ) [ 1 ..] , None )
1740
+ let substs = tr. substitution . as_slice ( Interner ) ;
1741
+ hir_fmt_generics ( f, & substs[ 1 ..] , None , substs[ 0 ] . ty ( Interner ) )
1731
1742
}
1732
1743
1733
1744
impl HirDisplay for TraitRef {
0 commit comments