Skip to content

Commit 73161cc

Browse files
committed
do not use associated types placeholder for inlay hint #6191
Signed-off-by: Benjamin Coenen <[email protected]>
1 parent a03f004 commit 73161cc

File tree

3 files changed

+61
-19
lines changed

3 files changed

+61
-19
lines changed

crates/hir_ty/src/display.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -390,11 +390,27 @@ impl HirDisplay for ApplicationTy {
390390
};
391391
let trait_ = f.db.trait_data(trait_);
392392
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, ">")?;
393+
394+
// Use placeholder associated types when the target is source code (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
395+
if f.display_target.is_source_code() || self.parameters.len() > 1 {
396+
write!(f, "{}::{}", trait_.name, type_alias.name)?;
397+
if self.parameters.len() > 0 {
398+
write!(f, "<")?;
399+
f.write_joined(&*self.parameters.0, ", ")?;
400+
write!(f, ">")?;
401+
}
402+
} else {
403+
if self.parameters.len() == 1 {
404+
write!(
405+
f,
406+
"<{} as {}>::{}",
407+
self.parameters.as_single().display(f.db),
408+
trait_.name,
409+
type_alias.name
410+
)?;
411+
} else {
412+
write!(f, "{}::{}", trait_.name, type_alias.name)?;
413+
}
398414
}
399415
}
400416
TypeCtor::ForeignType(type_alias) => {

crates/hir_ty/src/tests/regression.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,11 +831,11 @@ fn issue_4966() {
831831
356..362 'repeat': Repeat<Map<|&f64| -> f64>>
832832
365..390 'Repeat...nner }': Repeat<Map<|&f64| -> f64>>
833833
383..388 'inner': Map<|&f64| -> f64>
834-
401..404 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
835-
407..416 'from_iter': fn from_iter<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>, Repeat<Map<|&f64| -> f64>>>(Repeat<Map<|&f64| -> f64>>) -> Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
836-
407..424 'from_i...epeat)': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
834+
401..404 'vec': Vec<<Repeat<Map<|&f64| -> f64>> as IntoIterator>::Item>
835+
407..416 'from_iter': fn from_iter<<Repeat<Map<|&f64| -> f64>> as IntoIterator>::Item, Repeat<Map<|&f64| -> f64>>>(Repeat<Map<|&f64| -> f64>>) -> Vec<<Repeat<Map<|&f64| -> f64>> as IntoIterator>::Item>
836+
407..424 'from_i...epeat)': Vec<<Repeat<Map<|&f64| -> f64>> as IntoIterator>::Item>
837837
417..423 'repeat': Repeat<Map<|&f64| -> f64>>
838-
431..434 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
838+
431..434 'vec': Vec<<Repeat<Map<|&f64| -> f64>> as IntoIterator>::Item>
839839
431..444 'vec.foo_bar()': {unknown}
840840
"#]],
841841
);

crates/hir_ty/src/tests/traits.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ fn infer_project_associated_type() {
384384
108..261 '{ ...ter; }': ()
385385
118..119 'x': u32
386386
145..146 '1': u32
387-
156..157 'y': Iterable::Item<T>
388-
183..192 'no_matter': Iterable::Item<T>
389-
202..203 'z': Iterable::Item<T>
390-
215..224 'no_matter': Iterable::Item<T>
391-
234..235 'a': Iterable::Item<T>
392-
249..258 'no_matter': Iterable::Item<T>
387+
156..157 'y': <T as Iterable>::Item
388+
183..192 'no_matter': <T as Iterable>::Item
389+
202..203 'z': <T as Iterable>::Item
390+
215..224 'no_matter': <T as Iterable>::Item
391+
234..235 'a': <T as Iterable>::Item
392+
249..258 'no_matter': <T as Iterable>::Item
393393
"#]],
394394
);
395395
}
@@ -908,7 +908,6 @@ fn test<T: Trait>(t: T) { (*t); }
908908

909909
#[test]
910910
fn associated_type_placeholder() {
911-
// inside the generic function, the associated type gets normalized to a placeholder `ApplL::Out<T>` [https://rust-lang.github.io/rustc-guide/traits/associated-types.html#placeholder-associated-types].
912911
check_types(
913912
r#"
914913
pub trait ApplyL {
@@ -924,7 +923,7 @@ impl<T> ApplyL for RefMutL<T> {
924923
fn test<T: ApplyL>() {
925924
let y: <RefMutL<T> as ApplyL>::Out = no_matter;
926925
y;
927-
} //^ ApplyL::Out<T>
926+
} //^ <T as ApplyL>::Out
928927
"#,
929928
);
930929
}
@@ -941,7 +940,7 @@ fn foo<T: ApplyL>(t: T) -> <T as ApplyL>::Out;
941940
fn test<T: ApplyL>(t: T) {
942941
let y = foo(t);
943942
y;
944-
} //^ ApplyL::Out<T>
943+
} //^ <T as ApplyL>::Out
945944
"#,
946945
);
947946
}
@@ -2120,7 +2119,7 @@ fn unselected_projection_on_impl_self() {
21202119
"#,
21212120
expect![[r#"
21222121
40..44 'self': &Self
2123-
46..47 'x': Trait::Item<Self>
2122+
46..47 'x': <Self as Trait>::Item
21242123
126..130 'self': &S
21252124
132..133 'x': u32
21262125
147..161 '{ let y = x; }': ()
@@ -3151,3 +3150,30 @@ fn test() {
31513150
"#,
31523151
);
31533152
}
3153+
3154+
#[test]
3155+
fn infer_call_method_return_associated_types_with_generic() {
3156+
check_infer(
3157+
r#"
3158+
pub trait Default {
3159+
fn default() -> Self;
3160+
}
3161+
pub trait Foo {
3162+
type Bar: Default;
3163+
}
3164+
3165+
pub fn quux<T: Foo>() -> T::Bar {
3166+
let y = Default::default();
3167+
3168+
y
3169+
}
3170+
"#,
3171+
expect![[r#"
3172+
122..164 '{ ... y }': <T as Foo>::Bar
3173+
132..133 'y': <T as Foo>::Bar
3174+
136..152 'Defaul...efault': fn default<<T as Foo>::Bar>() -> <T as Foo>::Bar
3175+
136..154 'Defaul...ault()': <T as Foo>::Bar
3176+
161..162 'y': <T as Foo>::Bar
3177+
"#]],
3178+
);
3179+
}

0 commit comments

Comments
 (0)