do not use associated types placeholder for inlay hint #6191

Signed-off-by: Benjamin Coenen <5719034+bnjjj@users.noreply.github.com>
This commit is contained in:
Benjamin Coenen 2020-10-27 20:23:09 +01:00
parent a03f004766
commit 73161cc9cd
3 changed files with 61 additions and 19 deletions

View File

@ -390,11 +390,27 @@ impl HirDisplay for ApplicationTy {
};
let trait_ = f.db.trait_data(trait_);
let type_alias = f.db.type_alias_data(type_alias);
write!(f, "{}::{}", trait_.name, type_alias.name)?;
if self.parameters.len() > 0 {
write!(f, "<")?;
f.write_joined(&*self.parameters.0, ", ")?;
write!(f, ">")?;
// Use placeholder associated types when the target is source code (https://rust-lang.github.io/chalk/book/clauses/type_equality.html#placeholder-associated-types)
if f.display_target.is_source_code() || self.parameters.len() > 1 {
write!(f, "{}::{}", trait_.name, type_alias.name)?;
if self.parameters.len() > 0 {
write!(f, "<")?;
f.write_joined(&*self.parameters.0, ", ")?;
write!(f, ">")?;
}
} else {
if self.parameters.len() == 1 {
write!(
f,
"<{} as {}>::{}",
self.parameters.as_single().display(f.db),
trait_.name,
type_alias.name
)?;
} else {
write!(f, "{}::{}", trait_.name, type_alias.name)?;
}
}
}
TypeCtor::ForeignType(type_alias) => {

View File

@ -831,11 +831,11 @@ fn issue_4966() {
356..362 'repeat': Repeat<Map<|&f64| -> f64>>
365..390 'Repeat...nner }': Repeat<Map<|&f64| -> f64>>
383..388 'inner': Map<|&f64| -> f64>
401..404 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
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>>>>
407..424 'from_i...epeat)': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
401..404 'vec': Vec<<Repeat<Map<|&f64| -> f64>> as IntoIterator>::Item>
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>
407..424 'from_i...epeat)': Vec<<Repeat<Map<|&f64| -> f64>> as IntoIterator>::Item>
417..423 'repeat': Repeat<Map<|&f64| -> f64>>
431..434 'vec': Vec<IntoIterator::Item<Repeat<Map<|&f64| -> f64>>>>
431..434 'vec': Vec<<Repeat<Map<|&f64| -> f64>> as IntoIterator>::Item>
431..444 'vec.foo_bar()': {unknown}
"#]],
);

View File

@ -384,12 +384,12 @@ fn infer_project_associated_type() {
108..261 '{ ...ter; }': ()
118..119 'x': u32
145..146 '1': u32
156..157 'y': Iterable::Item<T>
183..192 'no_matter': Iterable::Item<T>
202..203 'z': Iterable::Item<T>
215..224 'no_matter': Iterable::Item<T>
234..235 'a': Iterable::Item<T>
249..258 'no_matter': Iterable::Item<T>
156..157 'y': <T as Iterable>::Item
183..192 'no_matter': <T as Iterable>::Item
202..203 'z': <T as Iterable>::Item
215..224 'no_matter': <T as Iterable>::Item
234..235 'a': <T as Iterable>::Item
249..258 'no_matter': <T as Iterable>::Item
"#]],
);
}
@ -908,7 +908,6 @@ fn test<T: Trait>(t: T) { (*t); }
#[test]
fn associated_type_placeholder() {
// 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].
check_types(
r#"
pub trait ApplyL {
@ -924,7 +923,7 @@ impl<T> ApplyL for RefMutL<T> {
fn test<T: ApplyL>() {
let y: <RefMutL<T> as ApplyL>::Out = no_matter;
y;
} //^ ApplyL::Out<T>
} //^ <T as ApplyL>::Out
"#,
);
}
@ -941,7 +940,7 @@ fn foo<T: ApplyL>(t: T) -> <T as ApplyL>::Out;
fn test<T: ApplyL>(t: T) {
let y = foo(t);
y;
} //^ ApplyL::Out<T>
} //^ <T as ApplyL>::Out
"#,
);
}
@ -2120,7 +2119,7 @@ fn unselected_projection_on_impl_self() {
"#,
expect![[r#"
40..44 'self': &Self
46..47 'x': Trait::Item<Self>
46..47 'x': <Self as Trait>::Item
126..130 'self': &S
132..133 'x': u32
147..161 '{ let y = x; }': ()
@ -3151,3 +3150,30 @@ fn test() {
"#,
);
}
#[test]
fn infer_call_method_return_associated_types_with_generic() {
check_infer(
r#"
pub trait Default {
fn default() -> Self;
}
pub trait Foo {
type Bar: Default;
}
pub fn quux<T: Foo>() -> T::Bar {
let y = Default::default();
y
}
"#,
expect![[r#"
122..164 '{ ... y }': <T as Foo>::Bar
132..133 'y': <T as Foo>::Bar
136..152 'Defaul...efault': fn default<<T as Foo>::Bar>() -> <T as Foo>::Bar
136..154 'Defaul...ault()': <T as Foo>::Bar
161..162 'y': <T as Foo>::Bar
"#]],
);
}