Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 6a2f83a

Browse files
committed
Impl HirDisplay for SelfParam
1 parent 6918eb6 commit 6a2f83a

File tree

2 files changed

+36
-35
lines changed

2 files changed

+36
-35
lines changed

crates/hir/src/display.rs

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use hir_def::{
88
type_ref::{TypeBound, TypeRef},
99
AdtId, GenericDefId,
1010
};
11-
use hir_expand::name;
1211
use hir_ty::{
1312
display::{
1413
write_bounds_like_dyn_trait_with_prefix, write_visibility, HirDisplay, HirDisplayError,
@@ -19,8 +18,9 @@ use hir_ty::{
1918

2019
use 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

2626
impl 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+
140149
impl HirDisplay for Adt {
141150
fn hir_fmt(&self, f: &mut HirFormatter<'_>) -> Result<(), HirDisplayError> {
142151
match self {

crates/hir/src/lib.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,14 +2096,6 @@ impl SelfParam {
20962096
.unwrap_or(Access::Owned)
20972097
}
20982098

2099-
pub fn display(self, db: &dyn HirDatabase) -> &'static str {
2100-
match self.access(db) {
2101-
Access::Shared => "&self",
2102-
Access::Exclusive => "&mut self",
2103-
Access::Owned => "self",
2104-
}
2105-
}
2106-
21072099
pub fn source(&self, db: &dyn HirDatabase) -> Option<InFile<ast::SelfParam>> {
21082100
let InFile { file_id, value } = Function::from(self.func).source(db)?;
21092101
value

0 commit comments

Comments
 (0)