From 9a72b7bccde54d901f3d89300777e0685790aefd Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 10 Oct 2020 19:51:02 +0300 Subject: Add a test --- crates/ide/src/inlay_hints.rs | 62 +++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 7d716577e..08ef49a27 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -228,6 +228,8 @@ fn hint_iterator( _ => None, })?; if let Some(ty) = ty.normalize_trait_assoc_type(db, iter_trait, &[], assoc_type_item) { + // TODO kb also check for the iterator impls for this ty + dbg!(ty.display(db).to_string()); const LABEL_START: &str = "impl Iterator {} - impl Vec { - fn new() -> Self { Vec {} } - fn push(&mut self, t: T) { } - } - impl IntoIterator for Vec { - type Item=T; - } -} "#, ); } @@ -1043,17 +1033,6 @@ fn main() { //^ &str } } -//- /alloc.rs crate:alloc deps:core -mod collections { - struct Vec {} - impl Vec { - fn new() -> Self { Vec {} } - fn push(&mut self, t: T) { } - } - impl IntoIterator for Vec { - type Item=T; - } -} "#, ); } @@ -1183,4 +1162,41 @@ fn main() { "#]], ); } + + #[test] + fn shorten_iterators_in_associated_params() { + check_with_config( + InlayHintsConfig { + parameter_hints: false, + type_hints: true, + chaining_hints: true, + max_length: None, + }, + r#" +use core::iter; + +pub struct SomeIter {} + +impl SomeIter { + pub fn new() -> Self { SomeIter {} } + pub fn push(&mut self, t: T) {} +} + +impl Iterator for SomeIter { + type Item = T; + fn next(&mut self) -> Option { + None + } +} + +fn main() { + let mut some_iter = SomeIter::new(); + //^^^^^^^^^^^^^ SomeIter>> + some_iter.push(iter::repeat(2).take(2)); + let zz = some_iter.take(2); + //^^ impl Iterator>> +} +"#, + ); + } } -- cgit v1.2.3 From 2bb80a4f0350045503258518d354a4e63e4c68fd Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sat, 10 Oct 2020 21:37:20 +0300 Subject: Also replace the associated types with iter --- crates/hir/src/code_model.rs | 6 +++++- crates/ide/src/inlay_hints.rs | 25 ++++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index 031c91ccf..9aaa280c8 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -1372,7 +1372,7 @@ impl Type { r#trait: Trait, args: &[Type], alias: TypeAlias, - ) -> Option { + ) -> Option { let subst = Substs::build_for_def(db, r#trait.id) .push(self.ty.value.clone()) .fill(args.iter().map(|t| t.ty.value.clone())) @@ -1393,6 +1393,10 @@ impl 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 { diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 08ef49a27..2ed84095d 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -228,17 +228,20 @@ fn hint_iterator( _ => None, })?; if let Some(ty) = ty.normalize_trait_assoc_type(db, iter_trait, &[], assoc_type_item) { - // TODO kb also check for the iterator impls for this ty - dbg!(ty.display(db).to_string()); const LABEL_START: &str = "impl Iterator>> some_iter.push(iter::repeat(2).take(2)); - let zz = some_iter.take(2); - //^^ impl Iterator>> + let iter_of_iters = some_iter.take(2); + //^^^^^^^^^^^^^ impl Iterator> } "#, ); -- cgit v1.2.3