Skip to content

Commit 0aca7b7

Browse files
committed
do not use associated types placeholder for inlay hint
Signed-off-by: Benjamin Coenen <[email protected]>
1 parent ef2f7bb commit 0aca7b7

File tree

3 files changed

+44
-60
lines changed

3 files changed

+44
-60
lines changed

crates/hir_ty/src/display.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,26 +84,17 @@ pub trait HirDisplay {
8484
}
8585

8686
/// Returns a String representation of `self` for test purposes
87-
fn display_test<'a>(
88-
&'a self,
89-
db: &'a dyn HirDatabase,
90-
module_id: ModuleId,
91-
) -> Result<String, DisplaySourceCodeError> {
92-
let mut result = String::new();
93-
match self.hir_fmt(&mut HirFormatter {
87+
fn display_test<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self>
88+
where
89+
Self: Sized,
90+
{
91+
HirDisplayWrapper {
9492
db,
95-
fmt: &mut result,
96-
buf: String::with_capacity(20),
97-
curr_size: 0,
93+
t: self,
9894
max_size: None,
9995
omit_verbose_types: false,
100-
display_target: DisplayTarget::Test { module_id },
101-
}) {
102-
Ok(()) => {}
103-
Err(HirDisplayError::FmtError) => panic!("Writing to String can't fail!"),
104-
Err(HirDisplayError::DisplaySourceCodeError(e)) => return Err(e),
105-
};
106-
Ok(result)
96+
display_target: DisplayTarget::Test,
97+
}
10798
}
10899
}
109100

@@ -158,15 +149,15 @@ enum DisplayTarget {
158149
/// The generated code should compile, so paths need to be qualified.
159150
SourceCode { module_id: ModuleId },
160151
/// Only for test purpose to keep real types
161-
Test { module_id: ModuleId },
152+
Test,
162153
}
163154

164155
impl DisplayTarget {
165156
fn is_source_code(&self) -> bool {
166157
matches!(self, Self::SourceCode {..})
167158
}
168159
fn is_test(&self) -> bool {
169-
matches!(self, Self::Test {..})
160+
matches!(self, Self::Test)
170161
}
171162
}
172163

@@ -348,15 +339,15 @@ impl HirDisplay for ApplicationTy {
348339
}
349340
TypeCtor::Adt(def_id) => {
350341
match f.display_target {
351-
DisplayTarget::Diagnostics => {
342+
DisplayTarget::Diagnostics | DisplayTarget::Test => {
352343
let name = match def_id {
353344
AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
354345
AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
355346
AdtId::EnumId(it) => f.db.enum_data(it).name.clone(),
356347
};
357348
write!(f, "{}", name)?;
358349
}
359-
DisplayTarget::SourceCode { module_id } | DisplayTarget::Test { module_id } => {
350+
DisplayTarget::SourceCode { module_id } => {
360351
if let Some(path) = find_path::find_path(
361352
f.db.upcast(),
362353
ItemInNs::Types(def_id.into()),
@@ -417,28 +408,23 @@ impl HirDisplay for ApplicationTy {
417408
_ => panic!("not an associated type"),
418409
};
419410
let trait_ = f.db.trait_data(trait_);
420-
let type_alias = f.db.type_alias_data(type_alias);
411+
let type_alias_data = f.db.type_alias_data(type_alias);
421412

422413
// Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
423-
if f.display_target.is_test() || self.parameters.len() > 1 {
424-
write!(f, "{}::{}", trait_.name, type_alias.name)?;
414+
if f.display_target.is_test() {
415+
write!(f, "{}::{}", trait_.name, type_alias_data.name)?;
425416
if self.parameters.len() > 0 {
426417
write!(f, "<")?;
427418
f.write_joined(&*self.parameters.0, ", ")?;
428419
write!(f, ">")?;
429420
}
430421
} else {
431-
if self.parameters.len() == 1 {
432-
write!(
433-
f,
434-
"<{} as {}>::{}",
435-
self.parameters.as_single().display(f.db),
436-
trait_.name,
437-
type_alias.name
438-
)?;
439-
} else {
440-
write!(f, "{}::{}", trait_.name, type_alias.name)?;
441-
}
422+
let projection_ty = ProjectionTy {
423+
associated_ty: type_alias,
424+
parameters: self.parameters.clone(),
425+
};
426+
427+
projection_ty.hir_fmt(f)?;
442428
}
443429
}
444430
TypeCtor::ForeignType(type_alias) => {

crates/hir_ty/src/tests.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,14 +157,13 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
157157
(node.value.text_range(), node.value.text().to_string().replace("\n", " "))
158158
};
159159
let macro_prefix = if node.file_id != file_id.into() { "!" } else { "" };
160-
let module = db.module_for_file(node.file_id.original_file(&db));
161160
format_to!(
162161
buf,
163162
"{}{:?} '{}': {}\n",
164163
macro_prefix,
165164
range,
166165
ellipsize(text, 15),
167-
ty.display_test(&db, module).unwrap()
166+
ty.display_test(&db)
168167
);
169168
}
170169
if include_mismatches {
@@ -175,14 +174,13 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
175174
for (src_ptr, mismatch) in &mismatches {
176175
let range = src_ptr.value.text_range();
177176
let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
178-
let module = db.module_for_file(src_ptr.file_id.original_file(&db));
179177
format_to!(
180178
buf,
181179
"{}{:?}: expected {}, got {}\n",
182180
macro_prefix,
183181
range,
184-
mismatch.expected.display_test(&db, module).unwrap(),
185-
mismatch.actual.display_test(&db, module).unwrap(),
182+
mismatch.expected.display_test(&db),
183+
mismatch.actual.display_test(&db),
186184
);
187185
}
188186
}

crates/hir_ty/src/tests/method_resolution.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,16 @@ fn infer_associated_method_with_modules() {
108108
check_infer(
109109
r#"
110110
mod a {
111-
pub struct A;
111+
struct A;
112112
impl A { pub fn thing() -> A { A {} }}
113113
}
114114
115115
mod b {
116-
pub struct B;
116+
struct B;
117117
impl B { pub fn thing() -> u32 { 99 }}
118118
119-
pub mod c {
120-
pub struct C;
119+
mod c {
120+
struct C;
121121
impl C { pub fn thing() -> C { C {} }}
122122
}
123123
}
@@ -130,22 +130,22 @@ fn infer_associated_method_with_modules() {
130130
}
131131
"#,
132132
expect![[r#"
133-
59..67 '{ A {} }': a::A
134-
61..65 'A {}': a::A
135-
133..139 '{ 99 }': u32
136-
135..137 '99': u32
137-
217..225 '{ C {} }': c::C
138-
219..223 'C {}': c::C
139-
256..340 '{ ...g(); }': ()
140-
266..267 'x': a::A
141-
270..281 'a::A::thing': fn thing() -> A
142-
270..283 'a::A::thing()': a::A
143-
293..294 'y': u32
144-
297..308 'b::B::thing': fn thing() -> u32
145-
297..310 'b::B::thing()': u32
146-
320..321 'z': c::C
147-
324..335 'c::C::thing': fn thing() -> C
148-
324..337 'c::C::thing()': c::C
133+
55..63 '{ A {} }': A
134+
57..61 'A {}': A
135+
125..131 '{ 99 }': u32
136+
127..129 '99': u32
137+
201..209 '{ C {} }': C
138+
203..207 'C {}': C
139+
240..324 '{ ...g(); }': ()
140+
250..251 'x': A
141+
254..265 'a::A::thing': fn thing() -> A
142+
254..267 'a::A::thing()': A
143+
277..278 'y': u32
144+
281..292 'b::B::thing': fn thing() -> u32
145+
281..294 'b::B::thing()': u32
146+
304..305 'z': C
147+
308..319 'c::C::thing': fn thing() -> C
148+
308..321 'c::C::thing()': C
149149
"#]],
150150
);
151151
}

0 commit comments

Comments
 (0)