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