From 9b3aa591cd0672e9b7bdf3e5aab630cd09a4d546 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 17 Jun 2021 11:18:37 +0300 Subject: internal: switch some tests to minicore --- crates/ide/src/inlay_hints.rs | 31 +++++++++++++++---------------- crates/test_utils/src/fixture.rs | 2 +- crates/test_utils/src/minicore.rs | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 17 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index 9cd33d0e4..a6ba3d734 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -434,7 +434,6 @@ fn get_callable( #[cfg(test)] mod tests { use expect_test::{expect, Expect}; - use ide_db::helpers::FamousDefs; use test_utils::extract_annotations; use crate::{fixture, inlay_hints::InlayHintsConfig}; @@ -487,8 +486,6 @@ mod tests { } fn check_with_config(config: InlayHintsConfig, ra_fixture: &str) { - let ra_fixture = - format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); let (analysis, file_id) = fixture::file(&ra_fixture); let expected = extract_annotations(&*analysis.file_text(file_id).unwrap()); let inlay_hints = analysis.inlay_hints(file_id, &config).unwrap(); @@ -498,8 +495,6 @@ mod tests { } fn check_expect(config: InlayHintsConfig, ra_fixture: &str, expect: Expect) { - let ra_fixture = - format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE); let (analysis, file_id) = fixture::file(&ra_fixture); let inlay_hints = analysis.inlay_hints(file_id, &config).unwrap(); expect.assert_debug_eq(&inlay_hints) @@ -823,6 +818,7 @@ fn main() { fn shorten_iterators_in_associated_params() { check_types( r#" +//- minicore: iterator use core::iter; pub struct SomeIter {} @@ -875,7 +871,7 @@ fn main() { fn fn_hints() { check_types( r#" -trait Sized {} +//- minicore: fn, sized fn foo() -> impl Fn() { loop {} } fn foo1() -> impl Fn(f64) { loop {} } @@ -1073,6 +1069,7 @@ fn main() { fn complete_for_hint() { check_types( r#" +//- minicore: iterator pub struct Vec {} impl Vec { @@ -1129,6 +1126,7 @@ fn main() { fn shorten_iterator_hints() { check_types( r#" +//- minicore: iterator use core::iter; struct MyIter; @@ -1230,12 +1228,12 @@ fn main() { expect![[r#" [ InlayHint { - range: 148..173, + range: 147..172, kind: ChainingHint, label: "B", }, InlayHint { - range: 148..155, + range: 147..154, kind: ChainingHint, label: "A", }, @@ -1290,12 +1288,12 @@ fn main() { expect![[r#" [ InlayHint { - range: 144..191, + range: 143..190, kind: ChainingHint, label: "C", }, InlayHint { - range: 144..180, + range: 143..179, kind: ChainingHint, label: "B", }, @@ -1335,12 +1333,12 @@ fn main() { expect![[r#" [ InlayHint { - range: 247..284, + range: 246..283, kind: ChainingHint, label: "B>", }, InlayHint { - range: 247..266, + range: 246..265, kind: ChainingHint, label: "A>", }, @@ -1359,6 +1357,7 @@ fn main() { max_length: None, }, r#" +//- minicore: iterator use core::iter; struct MyIter; @@ -1381,22 +1380,22 @@ fn main() { expect![[r#" [ InlayHint { - range: 175..242, + range: 174..241, kind: ChainingHint, label: "impl Iterator", }, InlayHint { - range: 175..225, + range: 174..224, kind: ChainingHint, label: "impl Iterator", }, InlayHint { - range: 175..207, + range: 174..206, kind: ChainingHint, label: "impl Iterator", }, InlayHint { - range: 175..190, + range: 174..189, kind: ChainingHint, label: "&mut MyIter", }, diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index 005a5c092..8d8f3b560 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs @@ -154,7 +154,7 @@ impl Fixture { let components = meta.split_ascii_whitespace().collect::>(); let path = components[0].to_string(); - assert!(path.starts_with('/')); + assert!(path.starts_with('/'), "fixture path does not start with `/`: {:?}", path); let mut krate = None; let mut deps = Vec::new(); diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 2f0da7fe5..1a0573d7a 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -228,6 +228,28 @@ pub mod iter { } pub use self::adapters::Take; + mod sources { + mod repeat { + pub fn repeat(elt: T) -> Repeat { + loop {} + } + + pub struct Repeat { + element: A, + } + + impl Iterator for Repeat { + type Item = A; + + fn next(&mut self) -> Option { + loop {} + } + } + } + pub use self::repeat::{repeat, Repeat}; + } + pub use self::sources::{repeat, Repeat}; + mod traits { mod iterator { use super::super::Take; @@ -242,6 +264,18 @@ pub mod iter { fn take(self, n: usize) -> crate::iter::Take { loop {} } + fn by_ref(&mut self) -> &mut Self + where + Self: Sized, + { + self + } + } + impl Iterator for &mut I { + type Item = I::Item; + fn next(&mut self) -> Option { + (**self).next() + } } } pub use self::iterator::Iterator; -- cgit v1.2.3 From c42cdff3d2ed2e30add09dd0d602181b6f83534d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 17 Jun 2021 11:28:44 +0300 Subject: internal: minimize minicore We want to keep minicore small, so let's split out iterator adapters and sources into a separate `iterators` region, and use them only when needed. --- crates/ide/src/inlay_hints.rs | 6 +- .../src/handlers/replace_for_loop_with_for_each.rs | 78 +++++++--------------- crates/test_utils/src/minicore.rs | 11 ++- 3 files changed, 36 insertions(+), 59 deletions(-) diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs index a6ba3d734..335d57a0d 100644 --- a/crates/ide/src/inlay_hints.rs +++ b/crates/ide/src/inlay_hints.rs @@ -818,7 +818,7 @@ fn main() { fn shorten_iterators_in_associated_params() { check_types( r#" -//- minicore: iterator +//- minicore: iterators use core::iter; pub struct SomeIter {} @@ -1126,7 +1126,7 @@ fn main() { fn shorten_iterator_hints() { check_types( r#" -//- minicore: iterator +//- minicore: iterators use core::iter; struct MyIter; @@ -1357,7 +1357,7 @@ fn main() { max_length: None, }, r#" -//- minicore: iterator +//- minicore: iterators use core::iter; struct MyIter; 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 8e571723d..5f2aa016f 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 @@ -186,18 +186,14 @@ fn main() { fn test_for_borrowed() { check_assist( replace_for_loop_with_for_each, - r" -//- minicore: iterator -struct Iter; -impl Iterator for Iter { - type Item = usize; - fn next(&mut self) -> Option { None } -} + r#" +//- minicore: iterators +use core::iter::{Repeat, repeat}; struct S; impl S { - fn iter(&self) -> Iter { Iter } - fn iter_mut(&mut self) -> Iter { Iter } + fn iter(&self) -> Repeat { repeat(92) } + fn iter_mut(&mut self) -> Repeat { repeat(92) } } fn main() { @@ -206,18 +202,14 @@ fn main() { let a = v * 2; } } -", - r" -struct Iter; -impl Iterator for Iter { - type Item = usize; - fn next(&mut self) -> Option { None } -} +"#, + r#" +use core::iter::{Repeat, repeat}; struct S; impl S { - fn iter(&self) -> Iter { Iter } - fn iter_mut(&mut self) -> Iter { Iter } + fn iter(&self) -> Repeat { repeat(92) } + fn iter_mut(&mut self) -> Repeat { repeat(92) } } fn main() { @@ -226,7 +218,7 @@ fn main() { let a = v * 2; }); } -", +"#, ) } @@ -259,18 +251,14 @@ fn main() { fn test_for_borrowed_mut() { check_assist( replace_for_loop_with_for_each, - r" -//- minicore: iterator -struct Iter; -impl Iterator for Iter { - type Item = usize; - fn next(&mut self) -> Option { None } -} + r#" +//- minicore: iterators +use core::iter::{Repeat, repeat}; struct S; impl S { - fn iter(&self) -> Iter { Iter } - fn iter_mut(&mut self) -> Iter { Iter } + fn iter(&self) -> Repeat { repeat(92) } + fn iter_mut(&mut self) -> Repeat { repeat(92) } } fn main() { @@ -279,18 +267,14 @@ fn main() { let a = v * 2; } } -", - r" -struct Iter; -impl Iterator for Iter { - type Item = usize; - fn next(&mut self) -> Option { None } -} +"#, + r#" +use core::iter::{Repeat, repeat}; struct S; impl S { - fn iter(&self) -> Iter { Iter } - fn iter_mut(&mut self) -> Iter { Iter } + fn iter(&self) -> Repeat { repeat(92) } + fn iter_mut(&mut self) -> Repeat { repeat(92) } } fn main() { @@ -299,7 +283,7 @@ fn main() { let a = v * 2; }); } -", +"#, ) } @@ -332,28 +316,16 @@ fn main() { check_assist( replace_for_loop_with_for_each, r#" -//- minicore: iterator -struct Iter; -impl Iterator for Iter { - type Item = usize; - fn next(&mut self) -> Option { None } -} - +//- minicore: iterators fn main() { - for$0 a in Iter.take(1) { + for$0 a in core::iter::repeat(92).take(1) { println!("{}", a); } } "#, r#" -struct Iter; -impl Iterator for Iter { - type Item = usize; - fn next(&mut self) -> Option { None } -} - fn main() { - Iter.take(1).for_each(|a| { + core::iter::repeat(92).take(1).for_each(|a| { println!("{}", a); }); } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 1a0573d7a..e6d2301c7 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -21,6 +21,7 @@ //! option: //! result: //! iterator: option +//! iterators: iterator pub mod marker { // region:sized @@ -209,6 +210,7 @@ pub mod task { // region:iterator pub mod iter { + // region:iterators mod adapters { pub struct Take { iter: I, @@ -249,6 +251,7 @@ pub mod iter { pub use self::repeat::{repeat, Repeat}; } pub use self::sources::{repeat, Repeat}; + // endregion:iterators mod traits { mod iterator { @@ -261,15 +264,17 @@ pub mod iter { fn nth(&mut self, n: usize) -> Option { loop {} } - fn take(self, n: usize) -> crate::iter::Take { - loop {} - } fn by_ref(&mut self) -> &mut Self where Self: Sized, { self } + // region:iterators + fn take(self, n: usize) -> crate::iter::Take { + loop {} + } + // endregion:iterators } impl Iterator for &mut I { type Item = I::Item; -- cgit v1.2.3