6195: Shorten iterators in associated params r=matklad a=SomeoneToIgnore

Applies the same iterator-shortening logic to the iterator associated types, recursively.

Before: 
![image](https://user-images.githubusercontent.com/2690773/95662735-e6ecf200-0b41-11eb-8e54-28493ad4e644.png)

After:
<img width="1192" alt="image" src="https://user-images.githubusercontent.com/2690773/95662894-e9038080-0b42-11eb-897d-527571ccac58.png">


Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
bors[bot] 2020-10-12 15:06:45 +00:00 committed by GitHub
commit fac59f4f28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 30 deletions

View File

@ -1389,7 +1389,7 @@ pub fn normalize_trait_assoc_type(
r#trait: Trait,
args: &[Type],
alias: TypeAlias,
) -> Option<Ty> {
) -> Option<Type> {
let subst = Substs::build_for_def(db, r#trait.id)
.push(self.ty.value.clone())
.fill(args.iter().map(|t| t.ty.value.clone()))
@ -1410,6 +1410,10 @@ pub fn normalize_trait_assoc_type(
Solution::Unique(SolutionVariables(subst)) => subst.value.first().cloned(),
Solution::Ambig(_) => None,
}
.map(|ty| Type {
krate: self.krate,
ty: InEnvironment { value: ty, environment: Arc::clone(&self.ty.environment) },
})
}
pub fn is_copy(&self, db: &dyn HirDatabase) -> bool {

View File

@ -231,12 +231,17 @@ fn hint_iterator(
const LABEL_START: &str = "impl Iterator<Item = ";
const LABEL_END: &str = ">";
let ty_display = ty.display_truncated(
db,
config
.max_length
.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len())),
);
let ty_display = hint_iterator(sema, config, &ty)
.map(|assoc_type_impl| assoc_type_impl.to_string())
.unwrap_or_else(|| {
ty.display_truncated(
db,
config
.max_length
.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len())),
)
.to_string()
});
return Some(format!("{}{}{}", LABEL_START, ty_display, LABEL_END).into());
}
}
@ -1002,18 +1007,6 @@ fn main() {
println!("Unit expr");
}
//- /alloc.rs crate:alloc deps:core
mod collections {
struct Vec<T> {}
impl<T> Vec<T> {
fn new() -> Self { Vec {} }
fn push(&mut self, t: T) { }
}
impl<T> IntoIterator for Vec<T> {
type Item=T;
}
}
"#,
);
}
@ -1043,17 +1036,6 @@ fn main() {
//^ &str
}
}
//- /alloc.rs crate:alloc deps:core
mod collections {
struct Vec<T> {}
impl<T> Vec<T> {
fn new() -> Self { Vec {} }
fn push(&mut self, t: T) { }
}
impl<T> IntoIterator for Vec<T> {
type Item=T;
}
}
"#,
);
}
@ -1183,4 +1165,41 @@ fn main() {
"#]],
);
}
#[test]
fn shorten_iterators_in_associated_params() {
check_with_config(
InlayHintsConfig {
parameter_hints: false,
type_hints: true,
chaining_hints: false,
max_length: None,
},
r#"
use core::iter;
pub struct SomeIter<T> {}
impl<T> SomeIter<T> {
pub fn new() -> Self { SomeIter {} }
pub fn push(&mut self, t: T) {}
}
impl<T> Iterator for SomeIter<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
None
}
}
fn main() {
let mut some_iter = SomeIter::new();
//^^^^^^^^^^^^^ SomeIter<Take<Repeat<i32>>>
some_iter.push(iter::repeat(2).take(2));
let iter_of_iters = some_iter.take(2);
//^^^^^^^^^^^^^ impl Iterator<Item = impl Iterator<Item = i32>>
}
"#,
);
}
}