Skip to content

Commit e341832

Browse files
bors[bot]bnjjj
andauthored
Merge #6387
6387: do not use associated types placeholder for inlay hint r=flodiebold a=bnjjj close #6191 Co-authored-by: Benjamin Coenen <[email protected]>
2 parents 85cda15 + ec3638a commit e341832

File tree

3 files changed

+72
-13
lines changed

3 files changed

+72
-13
lines changed

crates/hir_ty/src/display.rs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ pub trait HirDisplay {
8282
};
8383
Ok(result)
8484
}
85+
86+
/// Returns a String representation of `self` for test purposes
87+
fn display_test<'a>(&'a self, db: &'a dyn HirDatabase) -> HirDisplayWrapper<'a, Self>
88+
where
89+
Self: Sized,
90+
{
91+
HirDisplayWrapper {
92+
db,
93+
t: self,
94+
max_size: None,
95+
omit_verbose_types: false,
96+
display_target: DisplayTarget::Test,
97+
}
98+
}
8599
}
86100

87101
impl<'a> HirFormatter<'a> {
@@ -134,12 +148,17 @@ enum DisplayTarget {
134148
/// Display types for inserting them in source files.
135149
/// The generated code should compile, so paths need to be qualified.
136150
SourceCode { module_id: ModuleId },
151+
/// Only for test purpose to keep real types
152+
Test,
137153
}
138154

139155
impl DisplayTarget {
140156
fn is_source_code(&self) -> bool {
141157
matches!(self, Self::SourceCode {..})
142158
}
159+
fn is_test(&self) -> bool {
160+
matches!(self, Self::Test)
161+
}
143162
}
144163

145164
#[derive(Debug)]
@@ -313,14 +332,18 @@ impl HirDisplay for ApplicationTy {
313332
let ret_display = if f.omit_verbose_types() {
314333
ret.display_truncated(f.db, f.max_size)
315334
} else {
316-
ret.display(f.db)
335+
if f.display_target.is_test() {
336+
ret.display_test(f.db)
337+
} else {
338+
ret.display(f.db)
339+
}
317340
};
318341
write!(f, " -> {}", ret_display)?;
319342
}
320343
}
321344
TypeCtor::Adt(def_id) => {
322345
match f.display_target {
323-
DisplayTarget::Diagnostics => {
346+
DisplayTarget::Diagnostics | DisplayTarget::Test => {
324347
let name = match def_id {
325348
AdtId::StructId(it) => f.db.struct_data(it).name.clone(),
326349
AdtId::UnionId(it) => f.db.union_data(it).name.clone(),
@@ -389,12 +412,23 @@ impl HirDisplay for ApplicationTy {
389412
_ => panic!("not an associated type"),
390413
};
391414
let trait_ = f.db.trait_data(trait_);
392-
let type_alias = f.db.type_alias_data(type_alias);
393-
write!(f, "{}::{}", trait_.name, type_alias.name)?;
394-
if self.parameters.len() > 0 {
395-
write!(f, "<")?;
396-
f.write_joined(&*self.parameters.0, ", ")?;
397-
write!(f, ">")?;
415+
let type_alias_data = f.db.type_alias_data(type_alias);
416+
417+
// Use placeholder associated types when the target is test (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
418+
if f.display_target.is_test() {
419+
write!(f, "{}::{}", trait_.name, type_alias_data.name)?;
420+
if self.parameters.len() > 0 {
421+
write!(f, "<")?;
422+
f.write_joined(&*self.parameters.0, ", ")?;
423+
write!(f, ">")?;
424+
}
425+
} else {
426+
let projection_ty = ProjectionTy {
427+
associated_ty: type_alias,
428+
parameters: self.parameters.clone(),
429+
};
430+
431+
projection_ty.hir_fmt(f)?;
398432
}
399433
}
400434
TypeCtor::ForeignType(type_alias) => {
@@ -442,7 +476,11 @@ impl HirDisplay for ApplicationTy {
442476
let ret_display = if f.omit_verbose_types() {
443477
sig.ret().display_truncated(f.db, f.max_size)
444478
} else {
445-
sig.ret().display(f.db)
479+
if f.display_target.is_test() {
480+
sig.ret().display_test(f.db)
481+
} else {
482+
sig.ret().display(f.db)
483+
}
446484
};
447485
write!(f, " -> {}", ret_display)?;
448486
} else {

crates/hir_ty/src/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ fn check_types_impl(ra_fixture: &str, display_source: bool) {
7474
let module = db.module_for_file(file_id);
7575
ty.display_source_code(&db, module).unwrap()
7676
} else {
77-
ty.display(&db).to_string()
77+
ty.display_test(&db).to_string()
7878
};
7979
assert_eq!(expected, actual);
8080
checked_one = true;
@@ -163,7 +163,7 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
163163
macro_prefix,
164164
range,
165165
ellipsize(text, 15),
166-
ty.display(&db)
166+
ty.display_test(&db)
167167
);
168168
}
169169
if include_mismatches {
@@ -179,8 +179,8 @@ fn infer_with_mismatches(content: &str, include_mismatches: bool) -> String {
179179
"{}{:?}: expected {}, got {}\n",
180180
macro_prefix,
181181
range,
182-
mismatch.expected.display(&db),
183-
mismatch.actual.display(&db),
182+
mismatch.expected.display_test(&db),
183+
mismatch.actual.display_test(&db),
184184
);
185185
}
186186
}

crates/ide/src/inlay_hints.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,4 +1235,25 @@ fn main() {
12351235
"#,
12361236
);
12371237
}
1238+
1239+
#[test]
1240+
fn infer_call_method_return_associated_types_with_generic() {
1241+
check(
1242+
r#"
1243+
pub trait Default {
1244+
fn default() -> Self;
1245+
}
1246+
pub trait Foo {
1247+
type Bar: Default;
1248+
}
1249+
1250+
pub fn quux<T: Foo>() -> T::Bar {
1251+
let y = Default::default();
1252+
//^ <T as Foo>::Bar
1253+
1254+
y
1255+
}
1256+
"#,
1257+
);
1258+
}
12381259
}

0 commit comments

Comments
 (0)