]> git.lizzy.rs Git - rust.git/blob - crates/test_utils/src/minicore.rs
internal: sanity-check minicore flags
[rust.git] / crates / test_utils / src / minicore.rs
1 //! This is a fixture we use for tests that need lang items.
2 //!
3 //! We want to include the minimal subset of core for each test, so this file
4 //! supports "conditional compilation". Tests use the following syntax to include minicore:
5 //!
6 //!  //- minicore: flag1, flag2
7 //!
8 //! We then strip all the code marked with other flags.
9 //!
10 //! Available flags:
11 //!     sized:
12 //!     slice:
13 //!     range:
14 //!     unsize: sized
15 //!     deref: sized
16 //!     coerce_unsized: unsize
17
18 pub mod marker {
19     // region:sized
20     #[lang = "sized"]
21     #[fundamental]
22     #[rustc_specialization_trait]
23     pub trait Sized {}
24     // endregion:sized
25
26     // region:unsize
27     #[lang = "unsize"]
28     pub trait Unsize<T: ?Sized> {}
29     // endregion:unsize
30 }
31
32 pub mod ops {
33     // region:coerce_unsized
34     mod unsize {
35         use crate::marker::Unsize;
36
37         #[lang = "coerce_unsized"]
38         pub trait CoerceUnsized<T: ?Sized> {}
39
40         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
41         impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
42         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
43         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
44
45         impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
46         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
47
48         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
49         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
50         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
51     }
52     pub use self::unsize::CoerceUnsized;
53     // endregion:coerce_unsized
54
55     // region:deref
56     mod deref {
57         #[lang = "deref"]
58         pub trait Deref {
59             #[lang = "deref_target"]
60             type Target: ?Sized;
61             fn deref(&self) -> &Self::Target;
62         }
63     }
64     pub use self::deref::Deref;
65     // endregion:deref
66
67     // region:range
68     mod range {
69         #[lang = "RangeFull"]
70         pub struct RangeFull;
71
72         #[lang = "Range"]
73         pub struct Range<Idx> {
74             pub start: Idx,
75             pub end: Idx,
76         }
77
78         #[lang = "RangeFrom"]
79         pub struct RangeFrom<Idx> {
80             pub start: Idx,
81         }
82
83         #[lang = "RangeTo"]
84         pub struct RangeTo<Idx> {
85             pub end: Idx,
86         }
87
88         #[lang = "RangeInclusive"]
89         pub struct RangeInclusive<Idx> {
90             pub(crate) start: Idx,
91             pub(crate) end: Idx,
92             pub(crate) exhausted: bool,
93         }
94
95         #[lang = "RangeToInclusive"]
96         pub struct RangeToInclusive<Idx> {
97             pub end: Idx,
98         }
99     }
100     pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
101     pub use self::range::{RangeInclusive, RangeToInclusive};
102     // endregion:range
103 }
104
105 // region:slice
106 pub mod slice {
107     #[lang = "slice"]
108     impl<T> [T] {
109         pub fn len(&self) -> usize {
110             loop {}
111         }
112     }
113 }
114 // endregion:slice
115
116 pub mod prelude {
117     pub mod v1 {
118         pub use crate::marker::Sized; // :sized
119     }
120
121     pub mod rust_2015 {
122         pub use super::v1::*;
123     }
124
125     pub mod rust_2018 {
126         pub use super::v1::*;
127     }
128
129     pub mod rust_2021 {
130         pub use super::v1::*;
131     }
132 }
133
134 #[prelude_import]
135 #[allow(unused)]
136 use prelude::v1::*;