From 991919e71f048f9321e702512248e11c6c5fef70 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Jun 2021 22:37:03 +0300 Subject: internal: add index to minicore --- crates/hir_ty/src/tests/traits.rs | 45 ++++++--------------------------------- crates/test_utils/src/minicore.rs | 43 +++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 38 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index dd1ea817f..0b6a3a1e9 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -567,11 +567,11 @@ fn indexing_arrays() { fn infer_ops_index() { check_types( r#" -//- /main.rs crate:main deps:std +//- minicore: index struct Bar; struct Foo; -impl std::ops::Index for Bar { +impl core::ops::Index for Bar { type Output = Foo; } @@ -580,15 +580,6 @@ fn test() { let b = a[1u32]; b; } //^ Foo - -//- /std.rs crate:std -#[prelude_import] use ops::*; -mod ops { - #[lang = "index"] - pub trait Index { - type Output; - } -} "#, ); } @@ -597,16 +588,16 @@ mod ops { fn infer_ops_index_int() { check_types( r#" -//- /main.rs crate:main deps:std +//- minicore: index struct Bar; struct Foo; -impl std::ops::Index for Bar { +impl core::ops::Index for Bar { type Output = Foo; } struct Range; -impl std::ops::Index for Bar { +impl core::ops::Index for Bar { type Output = Bar; } @@ -616,15 +607,6 @@ fn test() { b; //^ Foo } - -//- /std.rs crate:std -#[prelude_import] use ops::*; -mod ops { - #[lang = "index"] - pub trait Index { - type Output; - } -} "#, ); } @@ -633,25 +615,12 @@ mod ops { fn infer_ops_index_autoderef() { check_types( r#" -//- /main.rs crate:main deps:std +//- minicore: index, slice fn test() { let a = &[1u32, 2, 3]; - let b = a[1u32]; + let b = a[1]; b; } //^ u32 - -//- /std.rs crate:std -impl ops::Index for [T] { - type Output = T; -} - -#[prelude_import] use ops::*; -mod ops { - #[lang = "index"] - pub trait Index { - type Output; - } -} "#, ); } diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index 769028580..71f07d38a 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs @@ -15,6 +15,7 @@ //! range: //! deref: sized //! deref_mut: deref +//! index: sized //! fn: //! pin: //! future: pin @@ -167,6 +168,48 @@ pub mod ops { }; // endregion:deref + // region:index + mod index { + #[lang = "index"] + pub trait Index { + type Output: ?Sized; + fn index(&self, index: Idx) -> &Self::Output; + } + #[lang = "index_mut"] + pub trait IndexMut: Index { + fn index_mut(&mut self, index: Idx) -> &mut Self::Output; + } + + // region:slice + impl Index for [T] + where + I: SliceIndex<[T]>, + { + type Output = I::Output; + fn index(&self, index: I) -> &I::Output { + loop {} + } + } + impl IndexMut for [T] + where + I: SliceIndex<[T]>, + { + fn index_mut(&mut self, index: I) -> &mut I::Output { + loop {} + } + } + + pub unsafe trait SliceIndex { + type Output: ?Sized; + } + unsafe impl SliceIndex<[T]> for usize { + type Output = T; + } + // endregion:slice + } + pub use self::index::{Index, IndexMut}; + // endregion:index + // region:range mod range { #[lang = "RangeFull"] -- cgit v1.2.3 From 89a0e58393de0ae39fc1f33a33cec87bc084a9f1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Jun 2021 22:47:02 +0300 Subject: internal: use minicore deref more --- crates/base_db/src/fixture.rs | 4 + crates/hir_ty/src/tests/regression.rs | 42 +++++------ crates/hir_ty/src/tests/traits.rs | 138 ++++++++++++---------------------- crates/ide/src/hover.rs | 4 +- crates/ide_completion/src/render.rs | 20 ++--- 5 files changed, 81 insertions(+), 127 deletions(-) (limited to 'crates') diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index d56b20b83..d0c946d83 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs @@ -114,6 +114,9 @@ impl ChangeFixture { let meta = FileMeta::from(entry); assert!(meta.path.starts_with(&source_root_prefix)); + if !meta.deps.is_empty() { + assert!(meta.krate.is_some(), "can't specify deps without naming the crate") + } if meta.introduce_new_source_root { roots.push(SourceRoot::new_local(mem::take(&mut file_set))); @@ -199,6 +202,7 @@ impl ChangeFixture { } } +#[derive(Debug)] struct FileMeta { path: String, krate: Option, diff --git a/crates/hir_ty/src/tests/regression.rs b/crates/hir_ty/src/tests/regression.rs index 94b628fb8..0f418ea49 100644 --- a/crates/hir_ty/src/tests/regression.rs +++ b/crates/hir_ty/src/tests/regression.rs @@ -792,6 +792,7 @@ fn issue_4800() { fn issue_4966() { check_infer( r#" + //- minicore: deref pub trait IntoIterator { type Item; } @@ -802,12 +803,7 @@ fn issue_4966() { struct Vec {} - #[lang = "deref"] - pub trait Deref { - type Target; - } - - impl Deref for Vec { + impl core::ops::Deref for Vec { type Target = [T]; } @@ -824,23 +820,23 @@ fn issue_4966() { } "#, expect![[r#" - 270..274 'iter': T - 289..291 '{}': () - 303..447 '{ ...r(); }': () - 313..318 'inner': Map<|&f64| -> f64> - 321..345 'Map { ... 0.0 }': Map<|&f64| -> f64> - 330..343 '|_: &f64| 0.0': |&f64| -> f64 - 331..332 '_': &f64 - 340..343 '0.0': f64 - 356..362 'repeat': Repeat f64>> - 365..390 'Repeat...nner }': Repeat f64>> - 383..388 'inner': Map<|&f64| -> f64> - 401..404 'vec': Vec f64>>>> - 407..416 'from_iter': fn from_iter f64>>>, Repeat f64>>>(Repeat f64>>) -> Vec f64>>>> - 407..424 'from_i...epeat)': Vec f64>>>> - 417..423 'repeat': Repeat f64>> - 431..434 'vec': Vec f64>>>> - 431..444 'vec.foo_bar()': {unknown} + 225..229 'iter': T + 244..246 '{}': () + 258..402 '{ ...r(); }': () + 268..273 'inner': Map<|&f64| -> f64> + 276..300 'Map { ... 0.0 }': Map<|&f64| -> f64> + 285..298 '|_: &f64| 0.0': |&f64| -> f64 + 286..287 '_': &f64 + 295..298 '0.0': f64 + 311..317 'repeat': Repeat f64>> + 320..345 'Repeat...nner }': Repeat f64>> + 338..343 'inner': Map<|&f64| -> f64> + 356..359 'vec': Vec f64>>>> + 362..371 'from_iter': fn from_iter f64>>>, Repeat f64>>>(Repeat f64>>) -> Vec f64>>>> + 362..379 'from_i...epeat)': Vec f64>>>> + 372..378 'repeat': Repeat f64>> + 386..389 'vec': Vec f64>>>> + 386..399 'vec.foo_bar()': {unknown} "#]], ); } diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs index 0b6a3a1e9..279a1354a 100644 --- a/crates/hir_ty/src/tests/traits.rs +++ b/crates/hir_ty/src/tests/traits.rs @@ -853,12 +853,9 @@ fn test(t: T) { t.foo(); } fn generic_param_env_deref() { check_types( r#" -#[lang = "deref"] -trait Deref { - type Target; -} +//- minicore: deref trait Trait {} -impl Deref for T where T: Trait { +impl core::ops::Deref for T where T: Trait { type Target = i128; } fn test(t: T) { (*t); } @@ -1727,20 +1724,7 @@ fn test() { fn fn_trait_deref_with_ty_default() { check_infer( r#" -#[lang = "deref"] -trait Deref { - type Target; - - fn deref(&self) -> &Self::Target; -} - -#[lang="fn_once"] -trait FnOnce { - type Output; - - fn call_once(self, args: Args) -> Self::Output; -} - +//- minicore: deref, fn struct Foo; impl Foo { @@ -1753,7 +1737,7 @@ impl Lazy { pub fn new(f: F) -> Lazy {} } -impl T> Deref for Lazy { +impl T> core::ops::Deref for Lazy { type Target = T; } @@ -1767,32 +1751,29 @@ fn test() { let r2 = lazy2.foo(); }"#, expect![[r#" - 64..68 'self': &Self - 165..169 'self': Self - 171..175 'args': Args - 239..243 'self': &Foo - 254..256 '{}': () - 334..335 'f': F - 354..356 '{}': () - 443..689 '{ ...o(); }': () - 453..458 'lazy1': Lazy Foo> - 475..484 'Lazy::new': fn new Foo>(|| -> Foo) -> Lazy Foo> - 475..492 'Lazy::...| Foo)': Lazy Foo> - 485..491 '|| Foo': || -> Foo - 488..491 'Foo': Foo - 502..504 'r1': usize - 507..512 'lazy1': Lazy Foo> - 507..518 'lazy1.foo()': usize - 560..575 'make_foo_fn_ptr': fn() -> Foo - 591..602 'make_foo_fn': fn make_foo_fn() -> Foo - 612..617 'lazy2': Lazy Foo> - 634..643 'Lazy::new': fn new Foo>(fn() -> Foo) -> Lazy Foo> - 634..660 'Lazy::...n_ptr)': Lazy Foo> - 644..659 'make_foo_fn_ptr': fn() -> Foo - 670..672 'r2': usize - 675..680 'lazy2': Lazy Foo> - 675..686 'lazy2.foo()': usize - 549..551 '{}': () + 36..40 'self': &Foo + 51..53 '{}': () + 131..132 'f': F + 151..153 '{}': () + 251..497 '{ ...o(); }': () + 261..266 'lazy1': Lazy Foo> + 283..292 'Lazy::new': fn new Foo>(|| -> Foo) -> Lazy Foo> + 283..300 'Lazy::...| Foo)': Lazy Foo> + 293..299 '|| Foo': || -> Foo + 296..299 'Foo': Foo + 310..312 'r1': usize + 315..320 'lazy1': Lazy Foo> + 315..326 'lazy1.foo()': usize + 368..383 'make_foo_fn_ptr': fn() -> Foo + 399..410 'make_foo_fn': fn make_foo_fn() -> Foo + 420..425 'lazy2': Lazy Foo> + 442..451 'Lazy::new': fn new Foo>(fn() -> Foo) -> Lazy Foo> + 442..468 'Lazy::...n_ptr)': Lazy Foo> + 452..467 'make_foo_fn_ptr': fn() -> Foo + 478..480 'r2': usize + 483..488 'lazy2': Lazy Foo> + 483..494 'lazy2.foo()': usize + 357..359 '{}': () "#]], ); } @@ -2941,28 +2922,13 @@ fn infer_box_fn_arg() { // The type mismatch is because we don't define Unsize and CoerceUnsized check_infer_with_mismatches( r#" -//- /lib.rs deps:std - -#[lang = "fn_once"] -pub trait FnOnce { - type Output; - - extern "rust-call" fn call_once(self, args: Args) -> Self::Output; -} - -#[lang = "deref"] -pub trait Deref { - type Target: ?Sized; - - fn deref(&self) -> &Self::Target; -} - +//- minicore: fn, deref, option #[lang = "owned_box"] pub struct Box { inner: *mut T, } -impl Deref for Box { +impl core::ops::Deref for Box { type Target = T; fn deref(&self) -> &T { @@ -2970,38 +2936,30 @@ impl Deref for Box { } } -enum Option { - None, - Some(T) -} - fn foo() { - let s = Option::None; + let s = None; let f: Box)> = box (|ps| {}); f(&s); }"#, expect![[r#" - 100..104 'self': Self - 106..110 'args': Args - 214..218 'self': &Self - 384..388 'self': &Box - 396..423 '{ ... }': &T - 406..417 '&self.inner': &*mut T - 407..411 'self': &Box - 407..417 'self.inner': *mut T - 478..576 '{ ...&s); }': () - 488..489 's': Option - 492..504 'Option::None': Option - 514..515 'f': Box)> - 549..562 'box (|ps| {})': Box<|{unknown}| -> ()> - 554..561 '|ps| {}': |{unknown}| -> () - 555..557 'ps': {unknown} - 559..561 '{}': () - 568..569 'f': Box)> - 568..573 'f(&s)': () - 570..572 '&s': &Option - 571..572 's': Option - 549..562: expected Box)>, got Box<|{unknown}| -> ()> + 154..158 'self': &Box + 166..193 '{ ... }': &T + 176..187 '&self.inner': &*mut T + 177..181 'self': &Box + 177..187 'self.inner': *mut T + 206..296 '{ ...&s); }': () + 216..217 's': Option + 220..224 'None': Option + 234..235 'f': Box)> + 269..282 'box (|ps| {})': Box<|{unknown}| -> ()> + 274..281 '|ps| {}': |{unknown}| -> () + 275..277 'ps': {unknown} + 279..281 '{}': () + 288..289 'f': Box)> + 288..293 'f(&s)': () + 290..292 '&s': &Option + 291..292 's': Option + 269..282: expected Box)>, got Box<|{unknown}| -> ()> "#]], ); } diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 4705fae08..05a2b1293 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs @@ -3014,8 +3014,8 @@ fn foo() { file_id: FileId( 1, ), - full_range: 250..432, - focus_range: 289..295, + full_range: 251..433, + focus_range: 290..296, name: "Future", kind: Trait, description: "pub trait Future", diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 4b55f7504..9bec03e17 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs @@ -1269,16 +1269,11 @@ fn bar(t: &Foo) {} fn suggest_deref_fn_ret() { check_relevance( r#" -#[lang = "deref"] -trait Deref { - type Target; - fn deref(&self) -> &Self::Target; -} - +//- minicore: deref struct S; struct T(S); -impl Deref for T { +impl core::ops::Deref for T { type Target = S; fn deref(&self) -> &Self::Target { @@ -1292,15 +1287,16 @@ fn bar() -> T {} fn main() { foo($0); } - "#, +"#, expect![[r#" - tt Deref [] - fn bar() [] - fn &bar() [type] - fn foo(…) [] st T [] st S [] fn main() [] + fn bar() [] + fn &bar() [type] + fn foo(…) [] + md core [] + tt Sized [] "#]], ) } -- cgit v1.2.3