]> git.lizzy.rs Git - rust.git/blob - crates/test_utils/src/minicore.rs
fix: Do not complete `Drop::drop`, complete `std::mem::drop` instead
[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 //!     unsize: sized
13 //!     coerce_unsized: unsize
14 //!     slice:
15 //!     range:
16 //!     deref: sized
17 //!     deref_mut: deref
18 //!     index: sized
19 //!     fn:
20 //!     try:
21 //!     pin:
22 //!     future: pin
23 //!     option:
24 //!     result:
25 //!     iterator: option
26 //!     iterators: iterator, fn
27 //!     default: sized
28 //!     hash:
29 //!     clone: sized
30 //!     copy: clone
31 //!     from: sized
32 //!     eq: sized
33 //!     ord: eq, option
34 //!     derive:
35 //!     fmt: result
36 //!     bool_impl: option, fn
37 //!     add:
38 //!     as_ref: sized
39 //!     drop:
40
41 pub mod marker {
42     // region:sized
43     #[lang = "sized"]
44     #[fundamental]
45     #[rustc_specialization_trait]
46     pub trait Sized {}
47     // endregion:sized
48
49     // region:unsize
50     #[lang = "unsize"]
51     pub trait Unsize<T: ?Sized> {}
52     // endregion:unsize
53
54     // region:copy
55     #[lang = "copy"]
56     pub trait Copy: Clone {}
57     // region:derive
58     #[rustc_builtin_macro]
59     pub macro Copy($item:item) {}
60     // endregion:derive
61
62     mod copy_impls {
63         use super::Copy;
64
65         macro_rules! impl_copy {
66             ($($t:ty)*) => {
67                 $(
68                     impl Copy for $t {}
69                 )*
70             }
71         }
72
73         impl_copy! {
74             usize u8 u16 u32 u64 u128
75             isize i8 i16 i32 i64 i128
76             f32 f64
77             bool char
78         }
79
80         impl<T: ?Sized> Copy for *const T {}
81         impl<T: ?Sized> Copy for *mut T {}
82         impl<T: ?Sized> Copy for &T {}
83     }
84     // endregion:copy
85 }
86
87 // region:default
88 pub mod default {
89     pub trait Default: Sized {
90         fn default() -> Self;
91     }
92     // region:derive
93     #[rustc_builtin_macro]
94     pub macro Default($item:item) {}
95     // endregion:derive
96 }
97 // endregion:default
98
99 // region:hash
100 pub mod hash {
101     pub trait Hasher {}
102
103     pub trait Hash {
104         fn hash<H: Hasher>(&self, state: &mut H);
105     }
106 }
107 // endregion:hash
108
109 // region:clone
110 pub mod clone {
111     #[lang = "clone"]
112     pub trait Clone: Sized {
113         fn clone(&self) -> Self;
114     }
115     // region:derive
116     #[rustc_builtin_macro]
117     pub macro Clone($item:item) {}
118     // endregion:derive
119 }
120 // endregion:clone
121
122 pub mod convert {
123     // region:from
124     pub trait From<T>: Sized {
125         fn from(_: T) -> Self;
126     }
127     pub trait Into<T>: Sized {
128         fn into(self) -> T;
129     }
130
131     impl<T, U> Into<U> for T
132     where
133         U: From<T>,
134     {
135         fn into(self) -> U {
136             U::from(self)
137         }
138     }
139
140     impl<T> From<T> for T {
141         fn from(t: T) -> T {
142             t
143         }
144     }
145     // endregion:from
146
147     // region:as_ref
148     pub trait AsRef<T: ?Sized> {
149         fn as_ref(&self) -> &T;
150     }
151     // endregion:as_ref
152 }
153
154 pub mod ops {
155     // region:coerce_unsized
156     mod unsize {
157         use crate::marker::Unsize;
158
159         #[lang = "coerce_unsized"]
160         pub trait CoerceUnsized<T: ?Sized> {}
161
162         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
163         impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
164         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
165         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
166
167         impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
168         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
169
170         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
171         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
172         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
173     }
174     pub use self::unsize::CoerceUnsized;
175     // endregion:coerce_unsized
176
177     // region:deref
178     mod deref {
179         #[lang = "deref"]
180         pub trait Deref {
181             #[lang = "deref_target"]
182             type Target: ?Sized;
183             fn deref(&self) -> &Self::Target;
184         }
185         // region:deref_mut
186         #[lang = "deref_mut"]
187         pub trait DerefMut: Deref {
188             fn deref_mut(&mut self) -> &mut Self::Target;
189         }
190         // endregion:deref_mut
191     }
192     pub use self::deref::{
193         Deref,
194         DerefMut, // :deref_mut
195     };
196     // endregion:deref
197
198     // region:drop
199     #[lang = "drop"]
200     pub trait Drop {
201         fn drop(&mut self);
202     }
203     // endregion:drop
204
205     // region:index
206     mod index {
207         #[lang = "index"]
208         pub trait Index<Idx: ?Sized> {
209             type Output: ?Sized;
210             fn index(&self, index: Idx) -> &Self::Output;
211         }
212         #[lang = "index_mut"]
213         pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
214             fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
215         }
216
217         // region:slice
218         impl<T, I> Index<I> for [T]
219         where
220             I: SliceIndex<[T]>,
221         {
222             type Output = I::Output;
223             fn index(&self, index: I) -> &I::Output {
224                 loop {}
225             }
226         }
227         impl<T, I> IndexMut<I> for [T]
228         where
229             I: SliceIndex<[T]>,
230         {
231             fn index_mut(&mut self, index: I) -> &mut I::Output {
232                 loop {}
233             }
234         }
235
236         pub unsafe trait SliceIndex<T: ?Sized> {
237             type Output: ?Sized;
238         }
239         unsafe impl<T> SliceIndex<[T]> for usize {
240             type Output = T;
241         }
242         // endregion:slice
243     }
244     pub use self::index::{Index, IndexMut};
245     // endregion:index
246
247     // region:drop
248     pub mod mem {
249         pub fn drop<T>(_x: T) {}
250     }
251     // endregion:drop
252
253     // region:range
254     mod range {
255         #[lang = "RangeFull"]
256         pub struct RangeFull;
257
258         #[lang = "Range"]
259         pub struct Range<Idx> {
260             pub start: Idx,
261             pub end: Idx,
262         }
263
264         #[lang = "RangeFrom"]
265         pub struct RangeFrom<Idx> {
266             pub start: Idx,
267         }
268
269         #[lang = "RangeTo"]
270         pub struct RangeTo<Idx> {
271             pub end: Idx,
272         }
273
274         #[lang = "RangeInclusive"]
275         pub struct RangeInclusive<Idx> {
276             pub(crate) start: Idx,
277             pub(crate) end: Idx,
278             pub(crate) exhausted: bool,
279         }
280
281         #[lang = "RangeToInclusive"]
282         pub struct RangeToInclusive<Idx> {
283             pub end: Idx,
284         }
285     }
286     pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
287     pub use self::range::{RangeInclusive, RangeToInclusive};
288     // endregion:range
289
290     // region:fn
291     mod function {
292         #[lang = "fn"]
293         #[fundamental]
294         pub trait Fn<Args>: FnMut<Args> {}
295
296         #[lang = "fn_mut"]
297         #[fundamental]
298         pub trait FnMut<Args>: FnOnce<Args> {}
299
300         #[lang = "fn_once"]
301         #[fundamental]
302         pub trait FnOnce<Args> {
303             #[lang = "fn_once_output"]
304             type Output;
305         }
306     }
307     pub use self::function::{Fn, FnMut, FnOnce};
308     // endregion:fn
309     // region:try
310     mod try_ {
311         pub enum ControlFlow<B, C = ()> {
312             Continue(C),
313             Break(B),
314         }
315         pub trait FromResidual<R = Self::Residual> {
316             #[lang = "from_residual"]
317             fn from_residual(residual: R) -> Self;
318         }
319         #[lang = "try"]
320         pub trait Try: FromResidual<Self::Residual> {
321             type Output;
322             type Residual;
323             #[lang = "from_output"]
324             fn from_output(output: Self::Output) -> Self;
325             #[lang = "branch"]
326             fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
327         }
328
329         impl<B, C> Try for ControlFlow<B, C> {
330             type Output = C;
331             type Residual = ControlFlow<B, convert::Infallible>;
332             fn from_output(output: Self::Output) -> Self {}
333             fn branch(self) -> ControlFlow<Self::Residual, Self::Output> {}
334         }
335
336         impl<B, C> FromResidual for ControlFlow<B, C> {
337             fn from_residual(residual: ControlFlow<B, convert::Infallible>) -> Self {}
338         }
339     }
340     pub use self::try_::{ControlFlow, FromResidual, Try};
341     // endregion:try
342
343     // region:add
344     #[lang = "add"]
345     pub trait Add<Rhs = Self> {
346         type Output;
347         fn add(self, rhs: Rhs) -> Self::Output;
348     }
349     // endregion:add
350 }
351
352 // region:eq
353 pub mod cmp {
354     #[lang = "eq"]
355     pub trait PartialEq<Rhs: ?Sized = Self> {
356         fn eq(&self, other: &Rhs) -> bool;
357     }
358
359     pub trait Eq: PartialEq<Self> {}
360
361     // region:derive
362     #[rustc_builtin_macro]
363     pub macro PartialEq($item:item) {}
364     #[rustc_builtin_macro]
365     pub macro Eq($item:item) {}
366     // endregion:derive
367
368     // region:ord
369     #[lang = "partial_ord"]
370     pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> {
371         fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
372     }
373
374     pub trait Ord: Eq + PartialOrd<Self> {
375         fn cmp(&self, other: &Self) -> Ordering;
376     }
377
378     pub enum Ordering {
379         Less = -1,
380         Equal = 0,
381         Greater = 1,
382     }
383
384     // region:derive
385     #[rustc_builtin_macro]
386     pub macro PartialOrd($item:item) {}
387     #[rustc_builtin_macro]
388     pub macro Ord($item:item) {}
389     // endregion:derive
390
391     // endregion:ord
392 }
393 // endregion:eq
394
395 // region:fmt
396 pub mod fmt {
397     pub struct Error;
398     pub type Result = Result<(), Error>;
399     pub struct Formatter<'a>;
400     pub trait Debug {
401         fn fmt(&self, f: &mut Formatter<'_>) -> Result;
402     }
403 }
404 // endregion:fmt
405
406 // region:slice
407 pub mod slice {
408     #[lang = "slice"]
409     impl<T> [T] {
410         pub fn len(&self) -> usize {
411             loop {}
412         }
413     }
414 }
415 // endregion:slice
416
417 // region:option
418 pub mod option {
419     pub enum Option<T> {
420         #[lang = "None"]
421         None,
422         #[lang = "Some"]
423         Some(T),
424     }
425
426     impl<T> Option<T> {
427         pub const fn unwrap(self) -> T {
428             match self {
429                 Some(val) => val,
430                 None => panic!("called `Option::unwrap()` on a `None` value"),
431             }
432         }
433     }
434 }
435 // endregion:option
436
437 // region:result
438 pub mod result {
439     pub enum Result<T, E> {
440         #[lang = "Ok"]
441         Ok(T),
442         #[lang = "Err"]
443         Err(E),
444     }
445 }
446 // endregion:result
447
448 // region:pin
449 pub mod pin {
450     #[lang = "pin"]
451     #[fundamental]
452     pub struct Pin<P> {
453         pointer: P,
454     }
455 }
456 // endregion:pin
457
458 // region:future
459 pub mod future {
460     use crate::{
461         pin::Pin,
462         task::{Context, Poll},
463     };
464
465     #[lang = "future_trait"]
466     pub trait Future {
467         type Output;
468         #[lang = "poll"]
469         fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
470     }
471 }
472 pub mod task {
473     pub enum Poll<T> {
474         #[lang = "Ready"]
475         Ready(T),
476         #[lang = "Pending"]
477         Pending,
478     }
479
480     pub struct Context<'a> {
481         waker: &'a (),
482     }
483 }
484 // endregion:future
485
486 // region:iterator
487 pub mod iter {
488     // region:iterators
489     mod adapters {
490         pub struct Take<I> {
491             iter: I,
492             n: usize,
493         }
494         impl<I> Iterator for Take<I>
495         where
496             I: Iterator,
497         {
498             type Item = <I as Iterator>::Item;
499
500             fn next(&mut self) -> Option<<I as Iterator>::Item> {
501                 loop {}
502             }
503         }
504
505         pub struct FilterMap<I, F> {
506             iter: I,
507             f: F,
508         }
509         impl<B, I: Iterator, F> Iterator for FilterMap<I, F>
510         where
511             F: FnMut(I::Item) -> Option<B>,
512         {
513             type Item = B;
514
515             #[inline]
516             fn next(&mut self) -> Option<B> {
517                 loop {}
518             }
519         }
520     }
521     pub use self::adapters::Take;
522
523     mod sources {
524         mod repeat {
525             pub fn repeat<T>(elt: T) -> Repeat<T> {
526                 loop {}
527             }
528
529             pub struct Repeat<A> {
530                 element: A,
531             }
532
533             impl<A> Iterator for Repeat<A> {
534                 type Item = A;
535
536                 fn next(&mut self) -> Option<A> {
537                     loop {}
538                 }
539             }
540         }
541         pub use self::repeat::{repeat, Repeat};
542     }
543     pub use self::sources::{repeat, Repeat};
544     // endregion:iterators
545
546     mod traits {
547         mod iterator {
548             use super::super::Take;
549
550             pub trait Iterator {
551                 type Item;
552                 #[lang = "next"]
553                 fn next(&mut self) -> Option<Self::Item>;
554                 fn nth(&mut self, n: usize) -> Option<Self::Item> {
555                     loop {}
556                 }
557                 fn by_ref(&mut self) -> &mut Self
558                 where
559                     Self: Sized,
560                 {
561                     self
562                 }
563                 // region:iterators
564                 fn take(self, n: usize) -> crate::iter::Take<Self> {
565                     loop {}
566                 }
567                 fn filter_map<B, F>(self, f: F) -> crate::iter::FilterMap<Self, F>
568                 where
569                     Self: Sized,
570                     F: FnMut(Self::Item) -> Option<B>,
571                 {
572                     loop {}
573                 }
574                 // endregion:iterators
575             }
576             impl<I: Iterator + ?Sized> Iterator for &mut I {
577                 type Item = I::Item;
578                 fn next(&mut self) -> Option<I::Item> {
579                     (**self).next()
580                 }
581             }
582         }
583         pub use self::iterator::Iterator;
584
585         mod collect {
586             pub trait IntoIterator {
587                 type Item;
588                 type IntoIter: Iterator<Item = Self::Item>;
589                 #[lang = "into_iter"]
590                 fn into_iter(self) -> Self::IntoIter;
591             }
592             impl<I: Iterator> IntoIterator for I {
593                 type Item = I::Item;
594                 type IntoIter = I;
595                 fn into_iter(self) -> I {
596                     self
597                 }
598             }
599         }
600         pub use self::collect::IntoIterator;
601     }
602     pub use self::traits::{IntoIterator, Iterator};
603 }
604 // endregion:iterator
605
606 // region:derive
607 mod macros {
608     pub(crate) mod builtin {
609         #[rustc_builtin_macro]
610         pub macro derive($item:item) {
611             /* compiler built-in */
612         }
613     }
614 }
615 // endregion:derive
616
617 // region:bool_impl
618 #[lang = "bool"]
619 impl bool {
620     pub fn then<T, F: FnOnce() -> T>(self, f: F) -> Option<T> {
621         if self {
622             Some(f())
623         } else {
624             None
625         }
626     }
627 }
628 // endregion:bool_impl
629
630 pub mod prelude {
631     pub mod v1 {
632         pub use crate::{
633             clone::Clone,                       // :clone
634             cmp::{Eq, PartialEq},               // :eq
635             cmp::{Ord, PartialOrd},             // :ord
636             convert::AsRef,                     // :as_ref
637             convert::{From, Into},              // :from
638             default::Default,                   // :default
639             iter::{IntoIterator, Iterator},     // :iterator
640             macros::builtin::derive,            // :derive
641             marker::Copy,                       // :copy
642             marker::Sized,                      // :sized
643             mem::drop,                          // :drop
644             ops::Drop,                          // :drop
645             ops::{Fn, FnMut, FnOnce},           // :fn
646             option::Option::{self, None, Some}, // :option
647             result::Result::{self, Err, Ok},    // :result
648         };
649     }
650
651     pub mod rust_2015 {
652         pub use super::v1::*;
653     }
654
655     pub mod rust_2018 {
656         pub use super::v1::*;
657     }
658
659     pub mod rust_2021 {
660         pub use super::v1::*;
661     }
662 }
663
664 #[prelude_import]
665 #[allow(unused)]
666 use prelude::v1::*;