mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 21:05:02 +00:00
internal: add index to minicore
This commit is contained in:
parent
73b3ee664e
commit
991919e71f
2 changed files with 50 additions and 38 deletions
|
@ -567,11 +567,11 @@ fn indexing_arrays() {
|
||||||
fn infer_ops_index() {
|
fn infer_ops_index() {
|
||||||
check_types(
|
check_types(
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs crate:main deps:std
|
//- minicore: index
|
||||||
struct Bar;
|
struct Bar;
|
||||||
struct Foo;
|
struct Foo;
|
||||||
|
|
||||||
impl std::ops::Index<u32> for Bar {
|
impl core::ops::Index<u32> for Bar {
|
||||||
type Output = Foo;
|
type Output = Foo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -580,15 +580,6 @@ fn test() {
|
||||||
let b = a[1u32];
|
let b = a[1u32];
|
||||||
b;
|
b;
|
||||||
} //^ Foo
|
} //^ Foo
|
||||||
|
|
||||||
//- /std.rs crate:std
|
|
||||||
#[prelude_import] use ops::*;
|
|
||||||
mod ops {
|
|
||||||
#[lang = "index"]
|
|
||||||
pub trait Index<Idx> {
|
|
||||||
type Output;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -597,16 +588,16 @@ mod ops {
|
||||||
fn infer_ops_index_int() {
|
fn infer_ops_index_int() {
|
||||||
check_types(
|
check_types(
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs crate:main deps:std
|
//- minicore: index
|
||||||
struct Bar;
|
struct Bar;
|
||||||
struct Foo;
|
struct Foo;
|
||||||
|
|
||||||
impl std::ops::Index<u32> for Bar {
|
impl core::ops::Index<u32> for Bar {
|
||||||
type Output = Foo;
|
type Output = Foo;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Range;
|
struct Range;
|
||||||
impl std::ops::Index<Range> for Bar {
|
impl core::ops::Index<Range> for Bar {
|
||||||
type Output = Bar;
|
type Output = Bar;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -616,15 +607,6 @@ fn test() {
|
||||||
b;
|
b;
|
||||||
//^ Foo
|
//^ Foo
|
||||||
}
|
}
|
||||||
|
|
||||||
//- /std.rs crate:std
|
|
||||||
#[prelude_import] use ops::*;
|
|
||||||
mod ops {
|
|
||||||
#[lang = "index"]
|
|
||||||
pub trait Index<Idx> {
|
|
||||||
type Output;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -633,25 +615,12 @@ mod ops {
|
||||||
fn infer_ops_index_autoderef() {
|
fn infer_ops_index_autoderef() {
|
||||||
check_types(
|
check_types(
|
||||||
r#"
|
r#"
|
||||||
//- /main.rs crate:main deps:std
|
//- minicore: index, slice
|
||||||
fn test() {
|
fn test() {
|
||||||
let a = &[1u32, 2, 3];
|
let a = &[1u32, 2, 3];
|
||||||
let b = a[1u32];
|
let b = a[1];
|
||||||
b;
|
b;
|
||||||
} //^ u32
|
} //^ u32
|
||||||
|
|
||||||
//- /std.rs crate:std
|
|
||||||
impl<T> ops::Index<u32> for [T] {
|
|
||||||
type Output = T;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[prelude_import] use ops::*;
|
|
||||||
mod ops {
|
|
||||||
#[lang = "index"]
|
|
||||||
pub trait Index<Idx> {
|
|
||||||
type Output;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
//! range:
|
//! range:
|
||||||
//! deref: sized
|
//! deref: sized
|
||||||
//! deref_mut: deref
|
//! deref_mut: deref
|
||||||
|
//! index: sized
|
||||||
//! fn:
|
//! fn:
|
||||||
//! pin:
|
//! pin:
|
||||||
//! future: pin
|
//! future: pin
|
||||||
|
@ -167,6 +168,48 @@ pub mod ops {
|
||||||
};
|
};
|
||||||
// endregion:deref
|
// endregion:deref
|
||||||
|
|
||||||
|
// region:index
|
||||||
|
mod index {
|
||||||
|
#[lang = "index"]
|
||||||
|
pub trait Index<Idx: ?Sized> {
|
||||||
|
type Output: ?Sized;
|
||||||
|
fn index(&self, index: Idx) -> &Self::Output;
|
||||||
|
}
|
||||||
|
#[lang = "index_mut"]
|
||||||
|
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
|
||||||
|
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
|
||||||
|
}
|
||||||
|
|
||||||
|
// region:slice
|
||||||
|
impl<T, I> Index<I> for [T]
|
||||||
|
where
|
||||||
|
I: SliceIndex<[T]>,
|
||||||
|
{
|
||||||
|
type Output = I::Output;
|
||||||
|
fn index(&self, index: I) -> &I::Output {
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<T, I> IndexMut<I> for [T]
|
||||||
|
where
|
||||||
|
I: SliceIndex<[T]>,
|
||||||
|
{
|
||||||
|
fn index_mut(&mut self, index: I) -> &mut I::Output {
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub unsafe trait SliceIndex<T: ?Sized> {
|
||||||
|
type Output: ?Sized;
|
||||||
|
}
|
||||||
|
unsafe impl<T> SliceIndex<[T]> for usize {
|
||||||
|
type Output = T;
|
||||||
|
}
|
||||||
|
// endregion:slice
|
||||||
|
}
|
||||||
|
pub use self::index::{Index, IndexMut};
|
||||||
|
// endregion:index
|
||||||
|
|
||||||
// region:range
|
// region:range
|
||||||
mod range {
|
mod range {
|
||||||
#[lang = "RangeFull"]
|
#[lang = "RangeFull"]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue