@@ -8,7 +8,6 @@ use hir_def::{
8
8
type_ref:: { TypeBound , TypeRef } ,
9
9
AdtId , GenericDefId ,
10
10
} ;
11
- use hir_expand:: name;
12
11
use hir_ty:: {
13
12
display:: {
14
13
write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay , HirDisplayError ,
@@ -19,8 +18,9 @@ use hir_ty::{
19
18
20
19
use crate :: {
21
20
Adt , AsAssocItem , AssocItemContainer , Const , ConstParam , Enum , ExternCrateDecl , Field ,
22
- Function , GenericParam , HasCrate , HasVisibility , LifetimeParam , Macro , Module , Static , Struct ,
23
- Trait , TraitAlias , TyBuilder , Type , TypeAlias , TypeOrConstParam , TypeParam , Union , Variant ,
21
+ Function , GenericParam , HasCrate , HasVisibility , LifetimeParam , Macro , Module , SelfParam ,
22
+ Static , Struct , Trait , TraitAlias , TyBuilder , Type , TypeAlias , TypeOrConstParam , TypeParam ,
23
+ Union , Variant ,
24
24
} ;
25
25
26
26
impl HirDisplay for Function {
@@ -57,37 +57,21 @@ impl HirDisplay for Function {
57
57
58
58
f. write_char ( '(' ) ?;
59
59
60
- let write_self_param = |ty : & TypeRef , f : & mut HirFormatter < ' _ > | match ty {
61
- TypeRef :: Path ( p) if p. is_self_type ( ) => f. write_str ( "self" ) ,
62
- TypeRef :: Reference ( inner, lifetime, mut_) if matches ! ( & * * inner, TypeRef :: Path ( p) if p. is_self_type( ) ) =>
63
- {
64
- f. write_char ( '&' ) ?;
65
- if let Some ( lifetime) = lifetime {
66
- write ! ( f, "{} " , lifetime. name. display( f. db. upcast( ) ) ) ?;
67
- }
68
- if let hir_def:: type_ref:: Mutability :: Mut = mut_ {
69
- f. write_str ( "mut " ) ?;
70
- }
71
- f. write_str ( "self" )
72
- }
73
- _ => {
74
- f. write_str ( "self: " ) ?;
75
- ty. hir_fmt ( f)
76
- }
77
- } ;
78
-
79
60
let mut first = true ;
61
+ let mut skip_self = 0 ;
62
+ if let Some ( self_param) = self . self_param ( db) {
63
+ self_param. hir_fmt ( f) ?;
64
+ first = false ;
65
+ skip_self = 1 ;
66
+ }
67
+
80
68
// FIXME: Use resolved `param.ty` once we no longer discard lifetimes
81
- for ( type_ref, param) in data. params . iter ( ) . zip ( self . assoc_fn_params ( db) ) {
69
+ for ( type_ref, param) in data. params . iter ( ) . zip ( self . assoc_fn_params ( db) ) . skip ( skip_self ) {
82
70
let local = param. as_local ( db) . map ( |it| it. name ( db) ) ;
83
71
if !first {
84
72
f. write_str ( ", " ) ?;
85
73
} else {
86
74
first = false ;
87
- if local == Some ( name ! ( self ) ) {
88
- write_self_param ( type_ref, f) ?;
89
- continue ;
90
- }
91
75
}
92
76
match local {
93
77
Some ( name) => write ! ( f, "{}: " , name. display( f. db. upcast( ) ) ) ?,
@@ -137,6 +121,31 @@ impl HirDisplay for Function {
137
121
}
138
122
}
139
123
124
+ impl HirDisplay for SelfParam {
125
+ fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
126
+ let data = f. db . function_data ( self . func ) ;
127
+ let param = data. params . first ( ) . unwrap ( ) ;
128
+ match & * * param {
129
+ TypeRef :: Path ( p) if p. is_self_type ( ) => f. write_str ( "self" ) ,
130
+ TypeRef :: Reference ( inner, lifetime, mut_) if matches ! ( & * * inner, TypeRef :: Path ( p) if p. is_self_type( ) ) =>
131
+ {
132
+ f. write_char ( '&' ) ?;
133
+ if let Some ( lifetime) = lifetime {
134
+ write ! ( f, "{} " , lifetime. name. display( f. db. upcast( ) ) ) ?;
135
+ }
136
+ if let hir_def:: type_ref:: Mutability :: Mut = mut_ {
137
+ f. write_str ( "mut " ) ?;
138
+ }
139
+ f. write_str ( "self" )
140
+ }
141
+ ty => {
142
+ f. write_str ( "self: " ) ?;
143
+ ty. hir_fmt ( f)
144
+ }
145
+ }
146
+ }
147
+ }
148
+
140
149
impl HirDisplay for Adt {
141
150
fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
142
151
match self {
0 commit comments