From 7e0d4416873c3308f4824c417a29b83154503e17 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 16 Jun 2021 22:24:11 +0300 Subject: internal: switch some tests to minicore --- crates/ide_assists/src/handlers/fill_match_arms.rs | 34 ++++++++++------------ 1 file changed, 15 insertions(+), 19 deletions(-) (limited to 'crates') diff --git a/crates/ide_assists/src/handlers/fill_match_arms.rs b/crates/ide_assists/src/handlers/fill_match_arms.rs index 5a43bdd6f..cd0f6dba9 100644 --- a/crates/ide_assists/src/handlers/fill_match_arms.rs +++ b/crates/ide_assists/src/handlers/fill_match_arms.rs @@ -278,8 +278,6 @@ fn build_pat(db: &RootDatabase, module: hir::Module, var: ExtendedVariant) -> Op #[cfg(test)] mod tests { - use ide_db::helpers::FamousDefs; - use crate::tests::{ check_assist, check_assist_not_applicable, check_assist_target, check_assist_unresolved, }; @@ -716,7 +714,10 @@ fn main() { #[test] fn fill_match_arms_tuple_of_enum_partial_with_wildcards() { - let ra_fixture = r#" + check_assist( + fill_match_arms, + r#" +//- minicore: option fn main() { let a = Some(1); let b = Some(()); @@ -725,10 +726,7 @@ fn main() { (None, Some(_)) => {} } } -"#; - check_assist( - fill_match_arms, - &format!("//- /main.rs crate:main deps:core{}{}", ra_fixture, FamousDefs::FIXTURE), +"#, r#" fn main() { let a = Some(1); @@ -746,17 +744,17 @@ fn main() { #[test] fn fill_match_arms_partial_with_deep_pattern() { // Fixme: cannot handle deep patterns - let ra_fixture = r#" + check_assist_not_applicable( + fill_match_arms, + r#" +//- minicore: option fn main() { match $0Some(true) { Some(true) => {} None => {} } } -"#; - check_assist_not_applicable( - fill_match_arms, - &format!("//- /main.rs crate:main deps:core{}{}", ra_fixture, FamousDefs::FIXTURE), +"#, ); } @@ -1007,17 +1005,15 @@ fn foo(a: A) { #[test] fn option_order() { cov_mark::check!(option_order); - let before = r#" + check_assist( + fill_match_arms, + r#" +//- minicore: option fn foo(opt: Option) { match opt$0 { } } -"#; - let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE); - - check_assist( - fill_match_arms, - before, +"#, r#" fn foo(opt: Option) { match opt { -- cgit v1.2.3 From 7ba5482a04e2534890b85cf832616a08c1b1bf21 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 16 Jun 2021 22:26:46 +0300 Subject: internal: switch some tests to minicore --- crates/ide_assists/src/handlers/generate_deref.rs | 27 ++++++++++------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'crates') diff --git a/crates/ide_assists/src/handlers/generate_deref.rs b/crates/ide_assists/src/handlers/generate_deref.rs index 4998ff7a4..4e10fdb85 100644 --- a/crates/ide_assists/src/handlers/generate_deref.rs +++ b/crates/ide_assists/src/handlers/generate_deref.rs @@ -182,23 +182,17 @@ impl std::ops::Deref for B { ); } - fn check_not_applicable(ra_fixture: &str) { - let fixture = format!( - "//- /main.rs crate:main deps:core,std\n{}\n{}", - ra_fixture, - FamousDefs::FIXTURE - ); - check_assist_not_applicable(generate_deref, &fixture) - } - #[test] fn test_generate_record_deref_not_applicable_if_already_impl() { cov_mark::check!(test_add_record_deref_impl_already_exists); - check_not_applicable( - r#"struct A { } + check_assist_not_applicable( + generate_deref, + r#" +//- minicore: deref +struct A { } struct B { $0a: A } -impl std::ops::Deref for B { +impl core::ops::Deref for B { type Target = A; fn deref(&self) -> &Self::Target { @@ -211,11 +205,14 @@ impl std::ops::Deref for B { #[test] fn test_generate_field_deref_not_applicable_if_already_impl() { cov_mark::check!(test_add_field_deref_impl_already_exists); - check_not_applicable( - r#"struct A { } + check_assist_not_applicable( + generate_deref, + r#" +//- minicore: deref +struct A { } struct B($0A) -impl std::ops::Deref for B { +impl core::ops::Deref for B { type Target = A; fn deref(&self) -> &Self::Target { -- cgit v1.2.3 From 604267088c9ef6afa021807b437dab22d72dfc99 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 16 Jun 2021 22:48:48 +0300 Subject: internal: add iterator to minicore --- crates/hir_ty/src/tests/regression.rs | 47 ++++++----------------------------- crates/test_utils/src/minicore.rs | 30 ++++++++++++++++++++++ 2 files changed, 38 insertions(+), 39 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/regression.rs b/crates/hir_ty/src/tests/regression.rs index 1edec1615..e0ad41fb9 100644 --- a/crates/hir_ty/src/tests/regression.rs +++ b/crates/hir_ty/src/tests/regression.rs @@ -418,48 +418,17 @@ fn issue_2705() { fn issue_2683_chars_impl() { check_types( r#" -//- /main.rs crate:main deps:std +//- minicore: iterator +pub struct Chars<'a> {} +impl<'a> Iterator for Chars<'a> { + type Item = char; + fn next(&mut self) -> Option {} +} + fn test() { - let chars: std::str::Chars<'_>; + let chars: Chars<'_>; (chars.next(), chars.nth(1)); } //^ (Option, Option) - -//- /std.rs crate:std -#[prelude_import] -use self::prelude::rust_2018::*; -pub mod prelude { - pub mod rust_2018 { - pub use crate::iter::Iterator; - pub use crate::option::Option; - } -} - -pub mod iter { - pub use self::traits::Iterator; - pub mod traits { - pub use self::iterator::Iterator; - - pub mod iterator { - pub trait Iterator { - type Item; - fn next(&mut self) -> Option; - fn nth(&mut self, n: usize) -> Option {} - } - } - } -} - -pub mod option { - pub enum Option {} -} - -pub mod str { - pub struct Chars<'a> {} - impl<'a> Iterator for Chars<'a> { - type Item = char; - fn next(&mut self) -> Option {} - } -} "#, ); } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index e04ca58d2..011d460be 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -20,6 +20,7 @@ //! future: pin //! option: //! result: +//! iterator: option pub mod marker { // region:sized @@ -206,9 +207,38 @@ pub mod task { } // endregion:future +// region:iterator +pub mod iter { + mod traits { + mod iterator { + pub trait Iterator { + type Item; + #[lang = "next"] + fn next(&mut self) -> Option; + fn nth(&mut self, n: usize) -> Option { + loop {} + } + } + } + mod collect { + pub trait IntoIterator { + type Item; + type IntoIter: Iterator; + #[lang = "into_iter"] + fn into_iter(self) -> Self::IntoIter; + } + } + pub use self::collect::IntoIterator; + pub use self::iterator::Iterator; + } + pub use self::traits::{IntoIterator, Iterator}; +} +// endregion:iterator + pub mod prelude { pub mod v1 { pub use crate::{ + iter::{IntoIterator, Iterator}, // :iterator marker::Sized, // :sized ops::{Fn, FnMut, FnOnce}, // :fn option::Option::{self, None, Some}, // :option -- cgit v1.2.3 From ee7b649d445b9c33486f3b5c3bef29bdb28124bc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 16 Jun 2021 22:54:57 +0300 Subject: internal: switch some tests to minicore --- crates/hir_ty/src/tests/traits.rs | 16 +--------------- crates/test_utils/src/minicore.rs | 7 +++++++ 2 files changed, 8 insertions(+), 15 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 065cca74f..22e0bfc49 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -1492,7 +1492,7 @@ fn test>(x: T, y: impl Trait) { fn impl_trait_assoc_binding_projection_bug() { check_types( r#" -//- /main.rs crate:main deps:std +//- minicore: iterator pub trait Language { type Kind; } @@ -1512,20 +1512,6 @@ fn api_walkthrough() { node.clone(); } //^ {unknown} } - -//- /std.rs crate:std -#[prelude_import] use iter::*; -mod iter { - trait IntoIterator { - type Item; - } - trait Iterator { - type Item; - } - impl IntoIterator for T { - type Item = ::Item; - } -} "#, ); } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 011d460be..a861ff09c 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -227,6 +227,13 @@ pub mod iter { #[lang = "into_iter"] fn into_iter(self) -> Self::IntoIter; } + impl IntoIterator for I { + type Item = I::Item; + type IntoIter = I; + fn into_iter(self) -> I { + self + } + } } pub use self::collect::IntoIterator; pub use self::iterator::Iterator; -- cgit v1.2.3 From 35772256f8ff3c52e469fc2bd388ad80ff8d79c7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 16 Jun 2021 23:27:04 +0300 Subject: internal: cleanup tests --- .../src/handlers/replace_for_loop_with_for_each.rs | 185 +++++++++++++-------- crates/test_utils/src/minicore.rs | 27 ++- 2 files changed, 140 insertions(+), 72 deletions(-) (limited to 'crates') diff --git a/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs b/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs index 50b05ab0b..8e571723d 100644 --- a/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs +++ b/crates/ide_assists/src/handlers/replace_for_loop_with_for_each.rs @@ -85,38 +85,48 @@ fn is_ref_and_impls_iter_method( let krate = scope.module()?.krate(); let traits_in_scope = scope.traits_in_scope(); let iter_trait = FamousDefs(sema, Some(krate)).core_iter_Iterator()?; - let has_wanted_method = typ.iterate_method_candidates( - sema.db, - krate, - &traits_in_scope, - Some(&wanted_method), - |_, func| { - if func.ret_type(sema.db).impls_trait(sema.db, iter_trait, &[]) { - return Some(()); - } - None - }, - ); - has_wanted_method.and(Some((expr_behind_ref, wanted_method))) + + let has_wanted_method = typ + .iterate_method_candidates( + sema.db, + krate, + &traits_in_scope, + Some(&wanted_method), + |_, func| { + if func.ret_type(sema.db).impls_trait(sema.db, iter_trait, &[]) { + return Some(()); + } + None + }, + ) + .is_some(); + if !has_wanted_method { + return None; + } + + Some((expr_behind_ref, wanted_method)) } /// Whether iterable implements core::Iterator fn impls_core_iter(sema: &hir::Semantics, iterable: &ast::Expr) -> bool { - let it_typ = if let Some(i) = sema.type_of_expr(iterable) { - i - } else { - return false; + let it_typ = match sema.type_of_expr(iterable) { + Some(it) => it, + None => return false, }; - let module = if let Some(m) = sema.scope(iterable.syntax()).module() { - m - } else { - return false; + + let module = match sema.scope(iterable.syntax()).module() { + Some(it) => it, + None => return false, }; + let krate = module.krate(); - if let Some(iter_trait) = FamousDefs(sema, Some(krate)).core_iter_Iterator() { - return it_typ.impls_trait(sema.db, iter_trait, &[]); + match FamousDefs(sema, Some(krate)).core_iter_Iterator() { + Some(iter_trait) => { + cov_mark::hit!(test_already_impls_iterator); + it_typ.impls_trait(sema.db, iter_trait, &[]) + } + None => false, } - false } #[cfg(test)] @@ -125,33 +135,6 @@ mod tests { use super::*; - const EMPTY_ITER_FIXTURE: &'static str = r" -//- /lib.rs deps:core crate:empty_iter -pub struct EmptyIter; -impl Iterator for EmptyIter { - type Item = usize; - fn next(&mut self) -> Option { None } -} - -pub struct Empty; -impl Empty { - pub fn iter(&self) -> EmptyIter { EmptyIter } - pub fn iter_mut(&self) -> EmptyIter { EmptyIter } -} - -pub struct NoIterMethod; -"; - - fn check_assist_with_fixtures(before: &str, after: &str) { - let before = &format!( - "//- /main.rs crate:main deps:core,empty_iter{}{}{}", - before, - FamousDefs::FIXTURE, - EMPTY_ITER_FIXTURE - ); - check_assist(replace_for_loop_with_for_each, before, after); - } - #[test] fn test_not_for() { check_assist_not_applicable( @@ -201,20 +184,44 @@ fn main() { #[test] fn test_for_borrowed() { - check_assist_with_fixtures( + check_assist( + replace_for_loop_with_for_each, r" -use empty_iter::*; +//- minicore: iterator +struct Iter; +impl Iterator for Iter { + type Item = usize; + fn next(&mut self) -> Option { None } +} + +struct S; +impl S { + fn iter(&self) -> Iter { Iter } + fn iter_mut(&mut self) -> Iter { Iter } +} + fn main() { - let x = Empty; + let x = S; for $0v in &x { let a = v * 2; } } ", r" -use empty_iter::*; +struct Iter; +impl Iterator for Iter { + type Item = usize; + fn next(&mut self) -> Option { None } +} + +struct S; +impl S { + fn iter(&self) -> Iter { Iter } + fn iter_mut(&mut self) -> Iter { Iter } +} + fn main() { - let x = Empty; + let x = S; x.iter().for_each(|v| { let a = v * 2; }); @@ -225,9 +232,10 @@ fn main() { #[test] fn test_for_borrowed_no_iter_method() { - check_assist_with_fixtures( + check_assist( + replace_for_loop_with_for_each, r" -use empty_iter::*; +struct NoIterMethod; fn main() { let x = NoIterMethod; for $0v in &x { @@ -236,7 +244,7 @@ fn main() { } ", r" -use empty_iter::*; +struct NoIterMethod; fn main() { let x = NoIterMethod; (&x).into_iter().for_each(|v| { @@ -249,20 +257,44 @@ fn main() { #[test] fn test_for_borrowed_mut() { - check_assist_with_fixtures( + check_assist( + replace_for_loop_with_for_each, r" -use empty_iter::*; +//- minicore: iterator +struct Iter; +impl Iterator for Iter { + type Item = usize; + fn next(&mut self) -> Option { None } +} + +struct S; +impl S { + fn iter(&self) -> Iter { Iter } + fn iter_mut(&mut self) -> Iter { Iter } +} + fn main() { - let x = Empty; + let x = S; for $0v in &mut x { let a = v * 2; } } ", r" -use empty_iter::*; +struct Iter; +impl Iterator for Iter { + type Item = usize; + fn next(&mut self) -> Option { None } +} + +struct S; +impl S { + fn iter(&self) -> Iter { Iter } + fn iter_mut(&mut self) -> Iter { Iter } +} + fn main() { - let x = Empty; + let x = S; x.iter_mut().for_each(|v| { let a = v * 2; }); @@ -296,21 +328,32 @@ fn main() { #[test] fn test_already_impls_iterator() { - check_assist_with_fixtures( + cov_mark::check!(test_already_impls_iterator); + check_assist( + replace_for_loop_with_for_each, r#" -use empty_iter::*; +//- minicore: iterator +struct Iter; +impl Iterator for Iter { + type Item = usize; + fn next(&mut self) -> Option { None } +} + fn main() { - let x = Empty; - for$0 a in x.iter().take(1) { + for$0 a in Iter.take(1) { println!("{}", a); } } "#, r#" -use empty_iter::*; +struct Iter; +impl Iterator for Iter { + type Item = usize; + fn next(&mut self) -> Option { None } +} + fn main() { - let x = Empty; - x.iter().take(1).for_each(|a| { + Iter.take(1).for_each(|a| { println!("{}", a); }); } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index a861ff09c..2f0da7fe5 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -209,8 +209,29 @@ pub mod task { // region:iterator pub mod iter { + mod adapters { + pub struct Take { + iter: I, + n: usize, + } + + impl Iterator for Take + where + I: Iterator, + { + type Item = ::Item; + + fn next(&mut self) -> Option<::Item> { + loop {} + } + } + } + pub use self::adapters::Take; + mod traits { mod iterator { + use super::super::Take; + pub trait Iterator { type Item; #[lang = "next"] @@ -218,8 +239,13 @@ pub mod iter { fn nth(&mut self, n: usize) -> Option { loop {} } + fn take(self, n: usize) -> crate::iter::Take { + loop {} + } } } + pub use self::iterator::Iterator; + mod collect { pub trait IntoIterator { type Item; @@ -236,7 +262,6 @@ pub mod iter { } } pub use self::collect::IntoIterator; - pub use self::iterator::Iterator; } pub use self::traits::{IntoIterator, Iterator}; } -- cgit v1.2.3