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 ;
@@ -423,7 +423,7 @@ impl HirDisplay for ProjectionTy {
423
423
let proj_params_count =
424
424
self . substitution . len ( Interner ) - trait_ref. substitution . len ( Interner ) ;
425
425
let proj_params = & self . substitution . as_slice ( Interner ) [ ..proj_params_count] ;
426
- hir_fmt_generics ( f, proj_params, None )
426
+ hir_fmt_generics ( f, proj_params, None , None )
427
427
}
428
428
}
429
429
@@ -468,6 +468,7 @@ impl HirDisplay for Const {
468
468
f,
469
469
parameters. as_slice ( Interner ) ,
470
470
c. generic_def ( f. db . upcast ( ) ) ,
471
+ None ,
471
472
) ?;
472
473
Ok ( ( ) )
473
474
}
@@ -967,11 +968,11 @@ impl HirDisplay for Ty {
967
968
let fn_params = generic_args_sans_defaults ( f, Some ( def. into ( ) ) , fn_params) ;
968
969
969
970
write ! ( f, "<" ) ?;
970
- hir_fmt_generic_arguments ( f, parent_params) ?;
971
+ hir_fmt_generic_arguments ( f, parent_params, None ) ?;
971
972
if !parent_params. is_empty ( ) && !fn_params. is_empty ( ) {
972
973
write ! ( f, ", " ) ?;
973
974
}
974
- hir_fmt_generic_arguments ( f, fn_params) ?;
975
+ hir_fmt_generic_arguments ( f, fn_params, None ) ?;
975
976
write ! ( f, ">" ) ?;
976
977
}
977
978
}
@@ -1016,7 +1017,7 @@ impl HirDisplay for Ty {
1016
1017
1017
1018
let generic_def = self . as_generic_def ( db) ;
1018
1019
1019
- hir_fmt_generics ( f, parameters. as_slice ( Interner ) , generic_def) ?;
1020
+ hir_fmt_generics ( f, parameters. as_slice ( Interner ) , generic_def, None ) ?;
1020
1021
}
1021
1022
TyKind :: AssociatedType ( assoc_type_id, parameters) => {
1022
1023
let type_alias = from_assoc_type_id ( * assoc_type_id) ;
@@ -1039,7 +1040,7 @@ impl HirDisplay for Ty {
1039
1040
f. end_location_link ( ) ;
1040
1041
// Note that the generic args for the associated type come before those for the
1041
1042
// trait (including the self type).
1042
- hir_fmt_generics ( f, parameters. as_slice ( Interner ) , None )
1043
+ hir_fmt_generics ( f, parameters. as_slice ( Interner ) , None , None )
1043
1044
} else {
1044
1045
let projection_ty = ProjectionTy {
1045
1046
associated_ty_id : to_assoc_type_id ( type_alias) ,
@@ -1141,7 +1142,7 @@ impl HirDisplay for Ty {
1141
1142
}
1142
1143
ClosureStyle :: ClosureWithSubst => {
1143
1144
write ! ( f, "{{closure#{:?}}}" , id. 0 . as_u32( ) ) ?;
1144
- return hir_fmt_generics ( f, substs. as_slice ( Interner ) , None ) ;
1145
+ return hir_fmt_generics ( f, substs. as_slice ( Interner ) , None , None ) ;
1145
1146
}
1146
1147
_ => ( ) ,
1147
1148
}
@@ -1329,6 +1330,7 @@ fn hir_fmt_generics(
1329
1330
f : & mut HirFormatter < ' _ > ,
1330
1331
parameters : & [ GenericArg ] ,
1331
1332
generic_def : Option < hir_def:: GenericDefId > ,
1333
+ self_ : Option < & Ty > ,
1332
1334
) -> Result < ( ) , HirDisplayError > {
1333
1335
if parameters. is_empty ( ) {
1334
1336
return Ok ( ( ) ) ;
@@ -1348,7 +1350,7 @@ fn hir_fmt_generics(
1348
1350
} ) ;
1349
1351
if !parameters_to_write. is_empty ( ) && !only_err_lifetimes {
1350
1352
write ! ( f, "<" ) ?;
1351
- hir_fmt_generic_arguments ( f, parameters_to_write) ?;
1353
+ hir_fmt_generic_arguments ( f, parameters_to_write, self_ ) ?;
1352
1354
write ! ( f, ">" ) ?;
1353
1355
}
1354
1356
@@ -1410,6 +1412,7 @@ fn generic_args_sans_defaults<'ga>(
1410
1412
fn hir_fmt_generic_arguments (
1411
1413
f : & mut HirFormatter < ' _ > ,
1412
1414
parameters : & [ GenericArg ] ,
1415
+ self_ : Option < & Ty > ,
1413
1416
) -> Result < ( ) , HirDisplayError > {
1414
1417
let mut first = true ;
1415
1418
let lifetime_offset = parameters. iter ( ) . position ( |arg| arg. lifetime ( Interner ) . is_some ( ) ) ;
@@ -1431,11 +1434,13 @@ fn hir_fmt_generic_arguments(
1431
1434
continue ;
1432
1435
}
1433
1436
1434
- if !first {
1437
+ if !mem :: take ( & mut first) {
1435
1438
write ! ( f, ", " ) ?;
1436
1439
}
1437
- first = false ;
1438
- generic_arg. hir_fmt ( f) ?;
1440
+ match self_ {
1441
+ self_ @ Some ( _) if generic_arg. ty ( Interner ) == self_ => write ! ( f, "Self" ) ?,
1442
+ _ => generic_arg. hir_fmt ( f) ?,
1443
+ }
1439
1444
}
1440
1445
Ok ( ( ) )
1441
1446
}
@@ -1558,12 +1563,16 @@ fn write_bounds_like_dyn_trait(
1558
1563
write ! ( f, "{}" , f. db. trait_data( trait_) . name. display( f. db. upcast( ) ) ) ?;
1559
1564
f. end_location_link ( ) ;
1560
1565
if is_fn_trait {
1561
- if let [ _self , params @ ..] = trait_ref. substitution . as_slice ( Interner ) {
1566
+ if let [ self_ , params @ ..] = trait_ref. substitution . as_slice ( Interner ) {
1562
1567
if let Some ( args) =
1563
1568
params. first ( ) . and_then ( |it| it. assert_ty_ref ( Interner ) . as_tuple ( ) )
1564
1569
{
1565
1570
write ! ( f, "(" ) ?;
1566
- hir_fmt_generic_arguments ( f, args. as_slice ( Interner ) ) ?;
1571
+ hir_fmt_generic_arguments (
1572
+ f,
1573
+ args. as_slice ( Interner ) ,
1574
+ self_. ty ( Interner ) ,
1575
+ ) ?;
1567
1576
write ! ( f, ")" ) ?;
1568
1577
}
1569
1578
}
@@ -1573,10 +1582,10 @@ fn write_bounds_like_dyn_trait(
1573
1582
Some ( trait_. into ( ) ) ,
1574
1583
trait_ref. substitution . as_slice ( Interner ) ,
1575
1584
) ;
1576
- if let [ _self , params @ ..] = params {
1585
+ if let [ self_ , params @ ..] = params {
1577
1586
if !params. is_empty ( ) {
1578
1587
write ! ( f, "<" ) ?;
1579
- hir_fmt_generic_arguments ( f, params) ?;
1588
+ hir_fmt_generic_arguments ( f, params, self_ . ty ( Interner ) ) ?;
1580
1589
// there might be assoc type bindings, so we leave the angle brackets open
1581
1590
angle_open = true ;
1582
1591
}
@@ -1634,6 +1643,7 @@ fn write_bounds_like_dyn_trait(
1634
1643
hir_fmt_generic_arguments (
1635
1644
f,
1636
1645
& proj. substitution . as_slice ( Interner ) [ ..proj_arg_count] ,
1646
+ None ,
1637
1647
) ?;
1638
1648
write ! ( f, ">" ) ?;
1639
1649
}
@@ -1690,7 +1700,8 @@ fn fmt_trait_ref(
1690
1700
f. start_location_link ( trait_. into ( ) ) ;
1691
1701
write ! ( f, "{}" , f. db. trait_data( trait_) . name. display( f. db. upcast( ) ) ) ?;
1692
1702
f. end_location_link ( ) ;
1693
- hir_fmt_generics ( f, & tr. substitution . as_slice ( Interner ) [ 1 ..] , None )
1703
+ let substs = tr. substitution . as_slice ( Interner ) ;
1704
+ hir_fmt_generics ( f, & substs[ 1 ..] , None , substs[ 0 ] . ty ( Interner ) )
1694
1705
}
1695
1706
1696
1707
impl HirDisplay for TraitRef {
0 commit comments