1
- #![ feature( associated_type_defaults) ]
2
- #![ feature( fmt_helpers_for_derive) ]
3
- #![ feature( min_specialization) ]
4
- #![ feature( never_type) ]
5
- #![ feature( rustc_attrs) ]
6
- #![ feature( unwrap_infallible) ]
1
+ #![ cfg_attr(
2
+ feature = "nightly" ,
3
+ feature(
4
+ associated_type_defaults,
5
+ fmt_helpers_for_derive,
6
+ min_specialization,
7
+ never_type,
8
+ rustc_attrs,
9
+ unwrap_infallible
10
+ )
11
+ ) ]
7
12
#![ deny( rustc:: untranslatable_diagnostic) ]
8
13
#![ deny( rustc:: diagnostic_outside_of_impl) ]
9
- #![ allow( internal_features) ]
14
+ #![ cfg_attr ( feature = "nightly" , allow( internal_features) ) ]
10
15
11
16
#[ macro_use]
12
17
extern crate bitflags;
18
+ #[ cfg( feature = "nightly" ) ]
13
19
#[ macro_use]
14
20
extern crate rustc_macros;
15
21
22
+ #[ cfg( not( feature = "nightly" ) ) ]
23
+ pub use index:: * ;
24
+ #[ cfg( feature = "nightly" ) ]
25
+ pub use index_nightly:: * ;
26
+ #[ cfg( feature = "nightly" ) ]
16
27
use rustc_data_structures:: stable_hasher:: { HashStable , StableHasher } ;
28
+ #[ cfg( feature = "nightly" ) ]
17
29
use rustc_data_structures:: unify:: { EqUnifyValue , UnifyKey } ;
18
30
use smallvec:: SmallVec ;
19
31
use std:: fmt;
20
32
use std:: fmt:: Debug ;
21
33
use std:: hash:: Hash ;
34
+ #[ cfg( feature = "nightly" ) ]
22
35
use std:: mem:: discriminant;
23
36
37
+ #[ cfg( feature = "nightly" ) ]
24
38
pub mod codec;
25
39
pub mod fold;
40
+ #[ cfg( not( feature = "nightly" ) ) ]
41
+ mod index;
42
+ #[ cfg( feature = "nightly" ) ]
43
+ mod index_nightly;
26
44
pub mod sty;
27
45
pub mod ty_info;
28
46
pub mod visit;
@@ -31,6 +49,7 @@ pub mod visit;
31
49
mod macros;
32
50
mod structural_impls;
33
51
52
+ #[ cfg( feature = "nightly" ) ]
34
53
pub use codec:: * ;
35
54
pub use structural_impls:: { DebugWithInfcx , InferCtxtLike , OptWithInfcx } ;
36
55
pub use sty:: * ;
@@ -302,53 +321,6 @@ bitflags! {
302
321
}
303
322
}
304
323
305
- rustc_index:: newtype_index! {
306
- /// A [De Bruijn index][dbi] is a standard means of representing
307
- /// regions (and perhaps later types) in a higher-ranked setting. In
308
- /// particular, imagine a type like this:
309
- /// ```ignore (illustrative)
310
- /// for<'a> fn(for<'b> fn(&'b isize, &'a isize), &'a char)
311
- /// // ^ ^ | | |
312
- /// // | | | | |
313
- /// // | +------------+ 0 | |
314
- /// // | | |
315
- /// // +----------------------------------+ 1 |
316
- /// // | |
317
- /// // +----------------------------------------------+ 0
318
- /// ```
319
- /// In this type, there are two binders (the outer fn and the inner
320
- /// fn). We need to be able to determine, for any given region, which
321
- /// fn type it is bound by, the inner or the outer one. There are
322
- /// various ways you can do this, but a De Bruijn index is one of the
323
- /// more convenient and has some nice properties. The basic idea is to
324
- /// count the number of binders, inside out. Some examples should help
325
- /// clarify what I mean.
326
- ///
327
- /// Let's start with the reference type `&'b isize` that is the first
328
- /// argument to the inner function. This region `'b` is assigned a De
329
- /// Bruijn index of 0, meaning "the innermost binder" (in this case, a
330
- /// fn). The region `'a` that appears in the second argument type (`&'a
331
- /// isize`) would then be assigned a De Bruijn index of 1, meaning "the
332
- /// second-innermost binder". (These indices are written on the arrows
333
- /// in the diagram).
334
- ///
335
- /// What is interesting is that De Bruijn index attached to a particular
336
- /// variable will vary depending on where it appears. For example,
337
- /// the final type `&'a char` also refers to the region `'a` declared on
338
- /// the outermost fn. But this time, this reference is not nested within
339
- /// any other binders (i.e., it is not an argument to the inner fn, but
340
- /// rather the outer one). Therefore, in this case, it is assigned a
341
- /// De Bruijn index of 0, because the innermost binder in that location
342
- /// is the outer fn.
343
- ///
344
- /// [dbi]: https://en.wikipedia.org/wiki/De_Bruijn_index
345
- #[ derive( HashStable_Generic ) ]
346
- #[ debug_format = "DebruijnIndex({})" ]
347
- pub struct DebruijnIndex {
348
- const INNERMOST = 0 ;
349
- }
350
- }
351
-
352
324
impl DebruijnIndex {
353
325
/// Returns the resulting index when this value is moved into
354
326
/// `amount` number of new binders. So, e.g., if you had
@@ -421,12 +393,12 @@ pub fn debug_bound_var<T: std::fmt::Write>(
421
393
if debruijn == INNERMOST {
422
394
write ! ( fmt, "^{var:?}" )
423
395
} else {
424
- write ! ( fmt, "^{}_{:?}" , debruijn. index ( ) , var)
396
+ write ! ( fmt, "^{}_{:?}" , debruijn. as_u32 ( ) , var)
425
397
}
426
398
}
427
399
428
400
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
429
- #[ derive( Encodable , Decodable , HashStable_Generic ) ]
401
+ #[ cfg_attr ( feature = "nightly" , derive( Encodable , Decodable , HashStable_Generic ) ) ]
430
402
pub enum IntTy {
431
403
Isize ,
432
404
I8 ,
@@ -484,7 +456,7 @@ impl IntTy {
484
456
}
485
457
486
458
#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Copy ) ]
487
- #[ derive( Encodable , Decodable , HashStable_Generic ) ]
459
+ #[ cfg_attr ( feature = "nightly" , derive( Encodable , Decodable , HashStable_Generic ) ) ]
488
460
pub enum UintTy {
489
461
Usize ,
490
462
U8 ,
@@ -542,7 +514,7 @@ impl UintTy {
542
514
}
543
515
544
516
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
545
- #[ derive( Encodable , Decodable , HashStable_Generic ) ]
517
+ #[ cfg_attr ( feature = "nightly" , derive( Encodable , Decodable , HashStable_Generic ) ) ]
546
518
pub enum FloatTy {
547
519
F32 ,
548
520
F64 ,
@@ -573,30 +545,13 @@ pub enum IntVarValue {
573
545
#[ derive( Clone , Copy , PartialEq , Eq ) ]
574
546
pub struct FloatVarValue ( pub FloatTy ) ;
575
547
576
- rustc_index:: newtype_index! {
577
- /// A **ty**pe **v**ariable **ID**.
578
- #[ debug_format = "?{}t" ]
579
- pub struct TyVid { }
580
- }
581
-
582
- rustc_index:: newtype_index! {
583
- /// An **int**egral (`u32`, `i32`, `usize`, etc.) type **v**ariable **ID**.
584
- #[ debug_format = "?{}i" ]
585
- pub struct IntVid { }
586
- }
587
-
588
- rustc_index:: newtype_index! {
589
- /// A **float**ing-point (`f32` or `f64`) type **v**ariable **ID**.
590
- #[ debug_format = "?{}f" ]
591
- pub struct FloatVid { }
592
- }
593
-
594
548
/// A placeholder for a type that hasn't been inferred yet.
595
549
///
596
550
/// E.g., if we have an empty array (`[]`), then we create a fresh
597
551
/// type variable for the element type since we won't know until it's
598
552
/// used what the element type is supposed to be.
599
- #[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash , Encodable , Decodable ) ]
553
+ #[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
554
+ #[ cfg_attr( feature = "nightly" , derive( Encodable , Decodable ) ) ]
600
555
pub enum InferTy {
601
556
/// A type variable.
602
557
TyVar ( TyVid ) ,
@@ -629,6 +584,7 @@ pub enum InferTy {
629
584
630
585
/// Raw `TyVid` are used as the unification key for `sub_relations`;
631
586
/// they carry no values.
587
+ #[ cfg( feature = "nightly" ) ]
632
588
impl UnifyKey for TyVid {
633
589
type Value = ( ) ;
634
590
#[ inline]
@@ -644,8 +600,10 @@ impl UnifyKey for TyVid {
644
600
}
645
601
}
646
602
603
+ #[ cfg( feature = "nightly" ) ]
647
604
impl EqUnifyValue for IntVarValue { }
648
605
606
+ #[ cfg( feature = "nightly" ) ]
649
607
impl UnifyKey for IntVid {
650
608
type Value = Option < IntVarValue > ;
651
609
#[ inline] // make this function eligible for inlining - it is quite hot.
@@ -661,8 +619,10 @@ impl UnifyKey for IntVid {
661
619
}
662
620
}
663
621
622
+ #[ cfg( feature = "nightly" ) ]
664
623
impl EqUnifyValue for FloatVarValue { }
665
624
625
+ #[ cfg( feature = "nightly" ) ]
666
626
impl UnifyKey for FloatVid {
667
627
type Value = Option < FloatVarValue > ;
668
628
#[ inline]
@@ -678,8 +638,9 @@ impl UnifyKey for FloatVid {
678
638
}
679
639
}
680
640
681
- #[ derive( Copy , Clone , PartialEq , Eq , Decodable , Encodable , Hash , HashStable_Generic ) ]
682
- #[ rustc_pass_by_value]
641
+ #[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
642
+ #[ cfg_attr( feature = "nightly" , derive( Encodable , Decodable , HashStable_Generic ) ) ]
643
+ #[ cfg_attr( feature = "nightly" , rustc_pass_by_value) ]
683
644
pub enum Variance {
684
645
Covariant , // T<A> <: T<B> iff A <: B -- e.g., function return type
685
646
Invariant , // T<A> <: T<B> iff B == A -- e.g., type of mutable cell
@@ -747,6 +708,7 @@ impl Variance {
747
708
}
748
709
}
749
710
711
+ #[ cfg( feature = "nightly" ) ]
750
712
impl < CTX > HashStable < CTX > for InferTy {
751
713
fn hash_stable ( & self , ctx : & mut CTX , hasher : & mut StableHasher ) {
752
714
use InferTy :: * ;
@@ -800,47 +762,6 @@ impl fmt::Display for InferTy {
800
762
}
801
763
}
802
764
803
- rustc_index:: newtype_index! {
804
- /// "Universes" are used during type- and trait-checking in the
805
- /// presence of `for<..>` binders to control what sets of names are
806
- /// visible. Universes are arranged into a tree: the root universe
807
- /// contains names that are always visible. Each child then adds a new
808
- /// set of names that are visible, in addition to those of its parent.
809
- /// We say that the child universe "extends" the parent universe with
810
- /// new names.
811
- ///
812
- /// To make this more concrete, consider this program:
813
- ///
814
- /// ```ignore (illustrative)
815
- /// struct Foo { }
816
- /// fn bar<T>(x: T) {
817
- /// let y: for<'a> fn(&'a u8, Foo) = ...;
818
- /// }
819
- /// ```
820
- ///
821
- /// The struct name `Foo` is in the root universe U0. But the type
822
- /// parameter `T`, introduced on `bar`, is in an extended universe U1
823
- /// -- i.e., within `bar`, we can name both `T` and `Foo`, but outside
824
- /// of `bar`, we cannot name `T`. Then, within the type of `y`, the
825
- /// region `'a` is in a universe U2 that extends U1, because we can
826
- /// name it inside the fn type but not outside.
827
- ///
828
- /// Universes are used to do type- and trait-checking around these
829
- /// "forall" binders (also called **universal quantification**). The
830
- /// idea is that when, in the body of `bar`, we refer to `T` as a
831
- /// type, we aren't referring to any type in particular, but rather a
832
- /// kind of "fresh" type that is distinct from all other types we have
833
- /// actually declared. This is called a **placeholder** type, and we
834
- /// use universes to talk about this. In other words, a type name in
835
- /// universe 0 always corresponds to some "ground" type that the user
836
- /// declared, but a type name in a non-zero universe is a placeholder
837
- /// type -- an idealized representative of "types in general" that we
838
- /// use for checking generic functions.
839
- #[ derive( HashStable_Generic ) ]
840
- #[ debug_format = "U{}" ]
841
- pub struct UniverseIndex { }
842
- }
843
-
844
765
impl UniverseIndex {
845
766
pub const ROOT : UniverseIndex = UniverseIndex :: from_u32 ( 0 ) ;
846
767
@@ -858,20 +779,20 @@ impl UniverseIndex {
858
779
/// name the region `'a`, but that region was not nameable from
859
780
/// `U` because it was not in scope there.
860
781
pub fn next_universe ( self ) -> UniverseIndex {
861
- UniverseIndex :: from_u32 ( self . private . checked_add ( 1 ) . unwrap ( ) )
782
+ UniverseIndex :: from_u32 ( self . as_u32 ( ) . checked_add ( 1 ) . unwrap ( ) )
862
783
}
863
784
864
785
/// Returns `true` if `self` can name a name from `other` -- in other words,
865
786
/// if the set of names in `self` is a superset of those in
866
787
/// `other` (`self >= other`).
867
788
pub fn can_name ( self , other : UniverseIndex ) -> bool {
868
- self . private >= other. private
789
+ self . as_u32 ( ) >= other. as_u32 ( )
869
790
}
870
791
871
792
/// Returns `true` if `self` cannot name some names from `other` -- in other
872
793
/// words, if the set of names in `self` is a strict subset of
873
794
/// those in `other` (`self < other`).
874
795
pub fn cannot_name ( self , other : UniverseIndex ) -> bool {
875
- self . private < other. private
796
+ self . as_u32 ( ) < other. as_u32 ( )
876
797
}
877
798
}
0 commit comments