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