@@ -18,6 +18,68 @@ use self::TyKind::*;
1818use rustc_data_structures:: stable_hasher:: HashStable ;
1919use rustc_serialize:: { Decodable , Decoder , Encodable } ;
2020
21+ /// The movability of a generator / closure literal:
22+ /// whether a generator contains self-references, causing it to be `!Unpin`.
23+ #[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Encodable , Decodable , Debug , Copy ) ]
24+ #[ derive( HashStable_Generic ) ]
25+ pub enum Movability {
26+ /// May contain self-references, `!Unpin`.
27+ Static ,
28+ /// Must not contain self-references, `Unpin`.
29+ Movable ,
30+ }
31+
32+ #[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Debug , Copy ) ]
33+ #[ derive( HashStable_Generic , Encodable , Decodable ) ]
34+ pub enum Mutability {
35+ // N.B. Order is deliberate, so that Not < Mut
36+ Not ,
37+ Mut ,
38+ }
39+
40+ impl Mutability {
41+ pub fn invert ( self ) -> Self {
42+ match self {
43+ Mutability :: Mut => Mutability :: Not ,
44+ Mutability :: Not => Mutability :: Mut ,
45+ }
46+ }
47+
48+ /// Returns `""` (empty string) or `"mut "` depending on the mutability.
49+ pub fn prefix_str ( self ) -> & ' static str {
50+ match self {
51+ Mutability :: Mut => "mut " ,
52+ Mutability :: Not => "" ,
53+ }
54+ }
55+
56+ /// Returns `"&"` or `"&mut "` depending on the mutability.
57+ pub fn ref_prefix_str ( self ) -> & ' static str {
58+ match self {
59+ Mutability :: Not => "&" ,
60+ Mutability :: Mut => "&mut " ,
61+ }
62+ }
63+
64+ /// Returns `""` (empty string) or `"mutably "` depending on the mutability.
65+ pub fn mutably_str ( self ) -> & ' static str {
66+ match self {
67+ Mutability :: Not => "" ,
68+ Mutability :: Mut => "mutably " ,
69+ }
70+ }
71+
72+ /// Return `true` if self is mutable
73+ pub fn is_mut ( self ) -> bool {
74+ matches ! ( self , Self :: Mut )
75+ }
76+
77+ /// Return `true` if self is **not** mutable
78+ pub fn is_not ( self ) -> bool {
79+ matches ! ( self , Self :: Not )
80+ }
81+ }
82+
2183/// Specifies how a trait object is represented.
2284#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash , Debug ) ]
2385#[ derive( Encodable , Decodable , HashStable_Generic ) ]
@@ -98,7 +160,7 @@ pub enum TyKind<I: Interner> {
98160
99161 /// A reference; a pointer with an associated lifetime. Written as
100162 /// `&'a mut T` or `&'a T`.
101- Ref ( I :: Region , I :: Ty , I :: Mutability ) ,
163+ Ref ( I :: Region , I :: Ty , Mutability ) ,
102164
103165 /// The anonymous type of a function declaration/definition. Each
104166 /// function has a unique type.
@@ -141,7 +203,7 @@ pub enum TyKind<I: Interner> {
141203 ///
142204 /// For more info about generator args, visit the documentation for
143205 /// `GeneratorArgs`.
144- Generator ( I :: DefId , I :: GenericArgs , I :: Movability ) ,
206+ Generator ( I :: DefId , I :: GenericArgs , Movability ) ,
145207
146208 /// A type representing the types stored inside a generator.
147209 /// This should only appear as part of the `GeneratorArgs`.
@@ -506,15 +568,15 @@ impl<I: Interner> DebugWithInfcx<I> for TyKind<I> {
506568 Slice ( t) => write ! ( f, "[{:?}]" , & this. wrap( t) ) ,
507569 RawPtr ( p) => {
508570 let ( ty, mutbl) = I :: ty_and_mut_to_parts ( p. clone ( ) ) ;
509- match I :: mutability_is_mut ( mutbl) {
510- true => write ! ( f, "*mut " ) ,
511- false => write ! ( f, "*const " ) ,
571+ match mutbl {
572+ Mutability :: Mut => write ! ( f, "*mut " ) ,
573+ Mutability :: Not => write ! ( f, "*const " ) ,
512574 } ?;
513575 write ! ( f, "{:?}" , & this. wrap( ty) )
514576 }
515- Ref ( r, t, m) => match I :: mutability_is_mut ( m . clone ( ) ) {
516- true => write ! ( f, "&{:?} mut {:?}" , & this. wrap( r) , & this. wrap( t) ) ,
517- false => write ! ( f, "&{:?} {:?}" , & this. wrap( r) , & this. wrap( t) ) ,
577+ Ref ( r, t, m) => match m {
578+ Mutability :: Mut => write ! ( f, "&{:?} mut {:?}" , & this. wrap( r) , & this. wrap( t) ) ,
579+ Mutability :: Not => write ! ( f, "&{:?} {:?}" , & this. wrap( r) , & this. wrap( t) ) ,
518580 } ,
519581 FnDef ( d, s) => f. debug_tuple_field2_finish ( "FnDef" , d, & this. wrap ( s) ) ,
520582 FnPtr ( s) => write ! ( f, "{:?}" , & this. wrap( s) ) ,
@@ -573,8 +635,6 @@ where
573635 I :: Const : Encodable < E > ,
574636 I :: Region : Encodable < E > ,
575637 I :: TypeAndMut : Encodable < E > ,
576- I :: Mutability : Encodable < E > ,
577- I :: Movability : Encodable < E > ,
578638 I :: PolyFnSig : Encodable < E > ,
579639 I :: BoundExistentialPredicates : Encodable < E > ,
580640 I :: Tys : Encodable < E > ,
@@ -687,8 +747,6 @@ where
687747 I :: Const : Decodable < D > ,
688748 I :: Region : Decodable < D > ,
689749 I :: TypeAndMut : Decodable < D > ,
690- I :: Mutability : Decodable < D > ,
691- I :: Movability : Decodable < D > ,
692750 I :: PolyFnSig : Decodable < D > ,
693751 I :: BoundExistentialPredicates : Decodable < D > ,
694752 I :: Tys : Decodable < D > ,
@@ -753,8 +811,6 @@ where
753811 I :: PolyFnSig : HashStable < CTX > ,
754812 I :: BoundExistentialPredicates : HashStable < CTX > ,
755813 I :: Region : HashStable < CTX > ,
756- I :: Movability : HashStable < CTX > ,
757- I :: Mutability : HashStable < CTX > ,
758814 I :: Tys : HashStable < CTX > ,
759815 I :: AliasTy : HashStable < CTX > ,
760816 I :: BoundTy : HashStable < CTX > ,
0 commit comments