mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
Merge #6195
6195: Shorten iterators in associated params r=matklad a=SomeoneToIgnore Applies the same iterator-shortening logic to the iterator associated types, recursively. Before:  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:
commit
fac59f4f28
2 changed files with 53 additions and 30 deletions
|
@ -1389,7 +1389,7 @@ impl Type {
|
||||||
r#trait: Trait,
|
r#trait: Trait,
|
||||||
args: &[Type],
|
args: &[Type],
|
||||||
alias: TypeAlias,
|
alias: TypeAlias,
|
||||||
) -> Option<Ty> {
|
) -> Option<Type> {
|
||||||
let subst = Substs::build_for_def(db, r#trait.id)
|
let subst = Substs::build_for_def(db, r#trait.id)
|
||||||
.push(self.ty.value.clone())
|
.push(self.ty.value.clone())
|
||||||
.fill(args.iter().map(|t| t.ty.value.clone()))
|
.fill(args.iter().map(|t| t.ty.value.clone()))
|
||||||
|
@ -1410,6 +1410,10 @@ impl Type {
|
||||||
Solution::Unique(SolutionVariables(subst)) => subst.value.first().cloned(),
|
Solution::Unique(SolutionVariables(subst)) => subst.value.first().cloned(),
|
||||||
Solution::Ambig(_) => None,
|
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 {
|
pub fn is_copy(&self, db: &dyn HirDatabase) -> bool {
|
||||||
|
|
|
@ -231,12 +231,17 @@ fn hint_iterator(
|
||||||
const LABEL_START: &str = "impl Iterator<Item = ";
|
const LABEL_START: &str = "impl Iterator<Item = ";
|
||||||
const LABEL_END: &str = ">";
|
const LABEL_END: &str = ">";
|
||||||
|
|
||||||
let ty_display = ty.display_truncated(
|
let ty_display = hint_iterator(sema, config, &ty)
|
||||||
db,
|
.map(|assoc_type_impl| assoc_type_impl.to_string())
|
||||||
config
|
.unwrap_or_else(|| {
|
||||||
.max_length
|
ty.display_truncated(
|
||||||
.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len())),
|
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());
|
return Some(format!("{}{}{}", LABEL_START, ty_display, LABEL_END).into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1002,18 +1007,6 @@ fn main() {
|
||||||
|
|
||||||
println!("Unit expr");
|
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
|
//^ &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>>
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue