@@ -8,7 +8,6 @@ use hir_def::{
88 type_ref:: { TypeBound , TypeRef } ,
99 AdtId , GenericDefId ,
1010} ;
11- use hir_expand:: name;
1211use hir_ty:: {
1312 display:: {
1413 write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay , HirDisplayError ,
@@ -19,8 +18,9 @@ use hir_ty::{
1918
2019use crate :: {
2120 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 ,
2424} ;
2525
2626impl HirDisplay for Function {
@@ -57,37 +57,21 @@ impl HirDisplay for Function {
5757
5858 f. write_char ( '(' ) ?;
5959
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-
7960 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+
8068 // 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 ) {
8270 let local = param. as_local ( db) . map ( |it| it. name ( db) ) ;
8371 if !first {
8472 f. write_str ( ", " ) ?;
8573 } else {
8674 first = false ;
87- if local == Some ( name ! ( self ) ) {
88- write_self_param ( type_ref, f) ?;
89- continue ;
90- }
9175 }
9276 match local {
9377 Some ( name) => write ! ( f, "{}: " , name. display( f. db. upcast( ) ) ) ?,
@@ -137,6 +121,31 @@ impl HirDisplay for Function {
137121 }
138122}
139123
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+
140149impl HirDisplay for Adt {
141150 fn hir_fmt ( & self , f : & mut HirFormatter < ' _ > ) -> Result < ( ) , HirDisplayError > {
142151 match self {
0 commit comments