@@ -20,6 +20,7 @@ use std::marker::PhantomData;
2020use std:: mem;
2121use std:: num:: NonZeroUsize ;
2222use std:: ops:: { ControlFlow , Deref } ;
23+ use std:: ptr:: NonNull ;
2324
2425/// An entity in the Rust type system, which can be one of
2526/// several kinds (types, lifetimes, and consts).
@@ -31,10 +32,29 @@ use std::ops::{ControlFlow, Deref};
3132/// `Region` and `Const` are all interned.
3233#[ derive( Copy , Clone , PartialEq , Eq , Hash ) ]
3334pub struct GenericArg < ' tcx > {
34- ptr : NonZeroUsize ,
35+ ptr : NonNull < ( ) > ,
3536 marker : PhantomData < ( Ty < ' tcx > , ty:: Region < ' tcx > , ty:: Const < ' tcx > ) > ,
3637}
3738
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+
3858impl < ' tcx > IntoDiagnosticArg for GenericArg < ' tcx > {
3959 fn into_diagnostic_arg ( self ) -> DiagnosticArgValue < ' static > {
4060 self . to_string ( ) . into_diagnostic_arg ( )
@@ -60,21 +80,21 @@ impl<'tcx> GenericArgKind<'tcx> {
6080 GenericArgKind :: Lifetime ( lt) => {
6181 // Ensure we can use the tag bits.
6282 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 ( ) )
6484 }
6585 GenericArgKind :: Type ( ty) => {
6686 // Ensure we can use the tag bits.
6787 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 ( ) )
6989 }
7090 GenericArgKind :: Const ( ct) => {
7191 // Ensure we can use the tag bits.
7292 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 ( ) )
7494 }
7595 } ;
7696
77- GenericArg { ptr : unsafe { NonZeroUsize :: new_unchecked ( ptr | tag) } , marker : PhantomData }
97+ GenericArg { ptr : ptr. map_addr ( |addr| addr | tag) , marker : PhantomData }
7898 }
7999}
80100
@@ -123,20 +143,22 @@ impl<'tcx> From<ty::Term<'tcx>> for GenericArg<'tcx> {
123143impl < ' tcx > GenericArg < ' tcx > {
124144 #[ inline]
125145 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+ } ;
127149 // SAFETY: use of `Interned::new_unchecked` here is ok because these
128150 // pointers were originally created from `Interned` types in `pack()`,
129151 // and this is just going in the other direction.
130152 unsafe {
131- match ptr & TAG_MASK {
153+ match self . ptr . addr ( ) . get ( ) & TAG_MASK {
132154 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 ( ) ,
134156 ) ) ) ,
135157 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 ( ) ,
137159 ) ) ) ,
138160 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 ( ) ,
140162 ) ) ) ,
141163 _ => intrinsics:: unreachable ( ) ,
142164 }
0 commit comments