Skip to content

Commit fac59f4

Browse files
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: ![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 <[email protected]>
2 parents 44df0e2 + 2bb80a4 commit fac59f4

File tree

2 files changed

+53
-30
lines changed

2 files changed

+53
-30
lines changed

crates/hir/src/code_model.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ impl Type {
13891389
r#trait: Trait,
13901390
args: &[Type],
13911391
alias: TypeAlias,
1392-
) -> Option<Ty> {
1392+
) -> Option<Type> {
13931393
let subst = Substs::build_for_def(db, r#trait.id)
13941394
.push(self.ty.value.clone())
13951395
.fill(args.iter().map(|t| t.ty.value.clone()))
@@ -1410,6 +1410,10 @@ impl Type {
14101410
Solution::Unique(SolutionVariables(subst)) => subst.value.first().cloned(),
14111411
Solution::Ambig(_) => None,
14121412
}
1413+
.map(|ty| Type {
1414+
krate: self.krate,
1415+
ty: InEnvironment { value: ty, environment: Arc::clone(&self.ty.environment) },
1416+
})
14131417
}
14141418

14151419
pub fn is_copy(&self, db: &dyn HirDatabase) -> bool {

crates/ide/src/inlay_hints.rs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,17 @@ fn hint_iterator(
231231
const LABEL_START: &str = "impl Iterator<Item = ";
232232
const LABEL_END: &str = ">";
233233

234-
let ty_display = ty.display_truncated(
235-
db,
236-
config
237-
.max_length
238-
.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len())),
239-
);
234+
let ty_display = hint_iterator(sema, config, &ty)
235+
.map(|assoc_type_impl| assoc_type_impl.to_string())
236+
.unwrap_or_else(|| {
237+
ty.display_truncated(
238+
db,
239+
config
240+
.max_length
241+
.map(|len| len.saturating_sub(LABEL_START.len() + LABEL_END.len())),
242+
)
243+
.to_string()
244+
});
240245
return Some(format!("{}{}{}", LABEL_START, ty_display, LABEL_END).into());
241246
}
242247
}
@@ -1002,18 +1007,6 @@ fn main() {
10021007
10031008
println!("Unit expr");
10041009
}
1005-
1006-
//- /alloc.rs crate:alloc deps:core
1007-
mod collections {
1008-
struct Vec<T> {}
1009-
impl<T> Vec<T> {
1010-
fn new() -> Self { Vec {} }
1011-
fn push(&mut self, t: T) { }
1012-
}
1013-
impl<T> IntoIterator for Vec<T> {
1014-
type Item=T;
1015-
}
1016-
}
10171010
"#,
10181011
);
10191012
}
@@ -1043,17 +1036,6 @@ fn main() {
10431036
//^ &str
10441037
}
10451038
}
1046-
//- /alloc.rs crate:alloc deps:core
1047-
mod collections {
1048-
struct Vec<T> {}
1049-
impl<T> Vec<T> {
1050-
fn new() -> Self { Vec {} }
1051-
fn push(&mut self, t: T) { }
1052-
}
1053-
impl<T> IntoIterator for Vec<T> {
1054-
type Item=T;
1055-
}
1056-
}
10571039
"#,
10581040
);
10591041
}
@@ -1183,4 +1165,41 @@ fn main() {
11831165
"#]],
11841166
);
11851167
}
1168+
1169+
#[test]
1170+
fn shorten_iterators_in_associated_params() {
1171+
check_with_config(
1172+
InlayHintsConfig {
1173+
parameter_hints: false,
1174+
type_hints: true,
1175+
chaining_hints: false,
1176+
max_length: None,
1177+
},
1178+
r#"
1179+
use core::iter;
1180+
1181+
pub struct SomeIter<T> {}
1182+
1183+
impl<T> SomeIter<T> {
1184+
pub fn new() -> Self { SomeIter {} }
1185+
pub fn push(&mut self, t: T) {}
1186+
}
1187+
1188+
impl<T> Iterator for SomeIter<T> {
1189+
type Item = T;
1190+
fn next(&mut self) -> Option<Self::Item> {
1191+
None
1192+
}
1193+
}
1194+
1195+
fn main() {
1196+
let mut some_iter = SomeIter::new();
1197+
//^^^^^^^^^^^^^ SomeIter<Take<Repeat<i32>>>
1198+
some_iter.push(iter::repeat(2).take(2));
1199+
let iter_of_iters = some_iter.take(2);
1200+
//^^^^^^^^^^^^^ impl Iterator<Item = impl Iterator<Item = i32>>
1201+
}
1202+
"#,
1203+
);
1204+
}
11861205
}

0 commit comments

Comments
 (0)