@@ -20,6 +20,7 @@ use std::marker::PhantomData;
20
20
use std:: mem;
21
21
use std:: num:: NonZeroUsize ;
22
22
use std:: ops:: { ControlFlow , Deref } ;
23
+ use std:: ptr:: NonNull ;
23
24
24
25
/// An entity in the Rust type system, which can be one of
25
26
/// several kinds (types, lifetimes, and consts).
@@ -31,10 +32,29 @@ use std::ops::{ControlFlow, Deref};
31
32
/// `Region` and `Const` are all interned.
32
33
#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
33
34
pub struct GenericArg < ' tcx > {
34
- ptr : NonZeroUsize ,
35
+ ptr : NonNull < ( ) > ,
35
36
marker : PhantomData < ( Ty < ' tcx > , ty:: Region < ' tcx > , ty:: Const < ' tcx > ) > ,
36
37
}
37
38
39
+ #[ cfg( parallel_compiler) ]
40
+ unsafe impl < ' tcx > rustc_data_structures:: sync:: DynSend for GenericArg < ' tcx > where
41
+ & ' tcx ( Ty < ' tcx > , ty:: Region < ' tcx > , ty:: Const < ' tcx > ) : rustc_data_structures:: sync:: DynSend
42
+ {
43
+ }
44
+ #[ cfg( parallel_compiler) ]
45
+ unsafe impl < ' tcx > rustc_data_structures:: sync:: DynSync for GenericArg < ' tcx > where
46
+ & ' tcx ( Ty < ' tcx > , ty:: Region < ' tcx > , ty:: Const < ' tcx > ) : rustc_data_structures:: sync:: DynSync
47
+ {
48
+ }
49
+ unsafe impl < ' tcx > Send for GenericArg < ' tcx > where
50
+ & ' tcx ( Ty < ' tcx > , ty:: Region < ' tcx > , ty:: Const < ' tcx > ) : Send
51
+ {
52
+ }
53
+ unsafe impl < ' tcx > Sync for GenericArg < ' tcx > where
54
+ & ' tcx ( Ty < ' tcx > , ty:: Region < ' tcx > , ty:: Const < ' tcx > ) : Sync
55
+ {
56
+ }
57
+
38
58
impl < ' tcx > IntoDiagnosticArg for GenericArg < ' tcx > {
39
59
fn into_diagnostic_arg ( self ) -> DiagnosticArgValue < ' static > {
40
60
self . to_string ( ) . into_diagnostic_arg ( )
@@ -60,21 +80,21 @@ impl<'tcx> GenericArgKind<'tcx> {
60
80
GenericArgKind :: Lifetime ( lt) => {
61
81
// Ensure we can use the tag bits.
62
82
assert_eq ! ( mem:: align_of_val( & * lt. 0.0 ) & TAG_MASK , 0 ) ;
63
- ( REGION_TAG , lt. 0 . 0 as * const ty :: RegionKind < ' tcx > as usize )
83
+ ( REGION_TAG , NonNull :: from ( lt. 0 . 0 ) . cast ( ) )
64
84
}
65
85
GenericArgKind :: Type ( ty) => {
66
86
// Ensure we can use the tag bits.
67
87
assert_eq ! ( mem:: align_of_val( & * ty. 0.0 ) & TAG_MASK , 0 ) ;
68
- ( TYPE_TAG , ty. 0 . 0 as * const WithCachedTypeInfo < ty :: TyKind < ' tcx > > as usize )
88
+ ( TYPE_TAG , NonNull :: from ( ty. 0 . 0 ) . cast ( ) )
69
89
}
70
90
GenericArgKind :: Const ( ct) => {
71
91
// Ensure we can use the tag bits.
72
92
assert_eq ! ( mem:: align_of_val( & * ct. 0.0 ) & TAG_MASK , 0 ) ;
73
- ( CONST_TAG , ct. 0 . 0 as * const WithCachedTypeInfo < ty :: ConstData < ' tcx > > as usize )
93
+ ( CONST_TAG , NonNull :: from ( ct. 0 . 0 ) . cast ( ) )
74
94
}
75
95
} ;
76
96
77
- GenericArg { ptr : unsafe { NonZeroUsize :: new_unchecked ( ptr | tag) } , marker : PhantomData }
97
+ GenericArg { ptr : ptr. map_addr ( |addr| addr | tag) , marker : PhantomData }
78
98
}
79
99
}
80
100
@@ -123,20 +143,22 @@ impl<'tcx> From<ty::Term<'tcx>> for GenericArg<'tcx> {
123
143
impl < ' tcx > GenericArg < ' tcx > {
124
144
#[ inline]
125
145
pub fn unpack ( self ) -> GenericArgKind < ' tcx > {
126
- let ptr = self . ptr . get ( ) ;
146
+ let ptr = unsafe {
147
+ self . ptr . map_addr ( |addr| NonZeroUsize :: new_unchecked ( addr. get ( ) & !TAG_MASK ) )
148
+ } ;
127
149
// SAFETY: use of `Interned::new_unchecked` here is ok because these
128
150
// pointers were originally created from `Interned` types in `pack()`,
129
151
// and this is just going in the other direction.
130
152
unsafe {
131
- match ptr & TAG_MASK {
153
+ match self . ptr . addr ( ) . get ( ) & TAG_MASK {
132
154
REGION_TAG => GenericArgKind :: Lifetime ( ty:: Region ( Interned :: new_unchecked (
133
- & * ( ( ptr & ! TAG_MASK ) as * const ty:: RegionKind < ' tcx > ) ,
155
+ ptr. cast :: < ty:: RegionKind < ' tcx > > ( ) . as_ref ( ) ,
134
156
) ) ) ,
135
157
TYPE_TAG => GenericArgKind :: Type ( Ty ( Interned :: new_unchecked (
136
- & * ( ( ptr & ! TAG_MASK ) as * const WithCachedTypeInfo < ty:: TyKind < ' tcx > > ) ,
158
+ ptr. cast :: < WithCachedTypeInfo < ty:: TyKind < ' tcx > > > ( ) . as_ref ( ) ,
137
159
) ) ) ,
138
160
CONST_TAG => GenericArgKind :: Const ( ty:: Const ( Interned :: new_unchecked (
139
- & * ( ( ptr & ! TAG_MASK ) as * const WithCachedTypeInfo < ty:: ConstData < ' tcx > > ) ,
161
+ ptr. cast :: < WithCachedTypeInfo < ty:: ConstData < ' tcx > > > ( ) . as_ref ( ) ,
140
162
) ) ) ,
141
163
_ => intrinsics:: unreachable ( ) ,
142
164
}
0 commit comments