Skip to content

Commit 76fcdb3

Browse files
committed
Modify GenericArg and Term structs to use strict provenance rules
1 parent 8847bda commit 76fcdb3

File tree

2 files changed

+60
-18
lines changed

2 files changed

+60
-18
lines changed

compiler/rustc_middle/src/ty/generic_args.rs

+31-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::ty::visit::{TypeVisitable, TypeVisitableExt, TypeVisitor};
77
use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt};
88

99
use rustc_data_structures::intern::Interned;
10+
use rustc_data_structures::sync::{DynSend, DynSync};
1011
use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg};
1112
use rustc_hir::def_id::DefId;
1213
use rustc_macros::HashStable;
@@ -20,6 +21,7 @@ use std::marker::PhantomData;
2021
use std::mem;
2122
use std::num::NonZeroUsize;
2223
use std::ops::{ControlFlow, Deref};
24+
use std::ptr::NonNull;
2325

2426
/// An entity in the Rust type system, which can be one of
2527
/// several kinds (types, lifetimes, and consts).
@@ -31,10 +33,27 @@ use std::ops::{ControlFlow, Deref};
3133
/// `Region` and `Const` are all interned.
3234
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
3335
pub struct GenericArg<'tcx> {
34-
ptr: NonZeroUsize,
36+
ptr: NonNull<()>,
3537
marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>)>,
3638
}
3739

40+
unsafe impl<'tcx> DynSend for GenericArg<'tcx> where
41+
&'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): DynSend
42+
{
43+
}
44+
unsafe impl<'tcx> DynSync for GenericArg<'tcx> where
45+
&'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): DynSync
46+
{
47+
}
48+
unsafe impl<'tcx> Send for GenericArg<'tcx> where
49+
&'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): Send
50+
{
51+
}
52+
unsafe impl<'tcx> Sync for GenericArg<'tcx> where
53+
&'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): Sync
54+
{
55+
}
56+
3857
impl<'tcx> IntoDiagnosticArg for GenericArg<'tcx> {
3958
fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> {
4059
self.to_string().into_diagnostic_arg()
@@ -60,21 +79,21 @@ impl<'tcx> GenericArgKind<'tcx> {
6079
GenericArgKind::Lifetime(lt) => {
6180
// Ensure we can use the tag bits.
6281
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)
82+
(REGION_TAG, NonNull::from(lt.0.0).cast())
6483
}
6584
GenericArgKind::Type(ty) => {
6685
// Ensure we can use the tag bits.
6786
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)
87+
(TYPE_TAG, NonNull::from(ty.0.0).cast())
6988
}
7089
GenericArgKind::Const(ct) => {
7190
// Ensure we can use the tag bits.
7291
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)
92+
(CONST_TAG, NonNull::from(ct.0.0).cast())
7493
}
7594
};
7695

77-
GenericArg { ptr: unsafe { NonZeroUsize::new_unchecked(ptr | tag) }, marker: PhantomData }
96+
GenericArg { ptr: unsafe { ptr.map_addr(|addr| NonZeroUsize::new_unchecked(addr.get() | tag)) }, marker: PhantomData }
7897
}
7998
}
8099

@@ -123,20 +142,22 @@ impl<'tcx> From<ty::Term<'tcx>> for GenericArg<'tcx> {
123142
impl<'tcx> GenericArg<'tcx> {
124143
#[inline]
125144
pub fn unpack(self) -> GenericArgKind<'tcx> {
126-
let ptr = self.ptr.get();
145+
let ptr = unsafe {
146+
self.ptr.map_addr(|addr| NonZeroUsize::new_unchecked(addr.get() & !TAG_MASK))
147+
};
127148
// SAFETY: use of `Interned::new_unchecked` here is ok because these
128149
// pointers were originally created from `Interned` types in `pack()`,
129150
// and this is just going in the other direction.
130151
unsafe {
131-
match ptr & TAG_MASK {
152+
match self.ptr.addr().get() & TAG_MASK {
132153
REGION_TAG => GenericArgKind::Lifetime(ty::Region(Interned::new_unchecked(
133-
&*((ptr & !TAG_MASK) as *const ty::RegionKind<'tcx>),
154+
&*(ptr.cast::<ty::RegionKind<'tcx>>().as_ref()),
134155
))),
135156
TYPE_TAG => GenericArgKind::Type(Ty(Interned::new_unchecked(
136-
&*((ptr & !TAG_MASK) as *const WithCachedTypeInfo<ty::TyKind<'tcx>>),
157+
&*(ptr.cast::<WithCachedTypeInfo<ty::TyKind<'tcx>>>().as_ref()),
137158
))),
138159
CONST_TAG => GenericArgKind::Const(ty::Const(Interned::new_unchecked(
139-
&*((ptr & !TAG_MASK) as *const WithCachedTypeInfo<ty::ConstData<'tcx>>),
160+
&*(ptr.cast::<WithCachedTypeInfo<ty::ConstData<'tcx>>>().as_ref()),
140161
))),
141162
_ => intrinsics::unreachable(),
142163
}

compiler/rustc_middle/src/ty/mod.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
3737
use rustc_data_structures::intern::Interned;
3838
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3939
use rustc_data_structures::steal::Steal;
40+
use rustc_data_structures::sync::{DynSend, DynSync};
4041
use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
4142
use rustc_data_structures::unord::UnordMap;
4243
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed, StashKey};
@@ -63,6 +64,7 @@ use std::marker::PhantomData;
6364
use std::mem;
6465
use std::num::NonZeroUsize;
6566
use std::ops::ControlFlow;
67+
use std::ptr::NonNull;
6668
use std::{fmt, str};
6769

6870
pub use crate::ty::diagnostics::*;
@@ -848,10 +850,27 @@ pub type PolyCoercePredicate<'tcx> = ty::Binder<'tcx, CoercePredicate<'tcx>>;
848850

849851
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
850852
pub struct Term<'tcx> {
851-
ptr: NonZeroUsize,
853+
ptr: NonNull<()>,
852854
marker: PhantomData<(Ty<'tcx>, Const<'tcx>)>,
853855
}
854856

857+
unsafe impl<'tcx> DynSend for Term<'tcx> where
858+
&'tcx (Ty<'tcx>, Const<'tcx>): DynSend
859+
{
860+
}
861+
unsafe impl<'tcx> DynSync for Term<'tcx> where
862+
&'tcx (Ty<'tcx>, Const<'tcx>): DynSync
863+
{
864+
}
865+
unsafe impl<'tcx> Send for Term<'tcx> where
866+
&'tcx (Ty<'tcx>, Const<'tcx>): Send
867+
{
868+
}
869+
unsafe impl<'tcx> Sync for Term<'tcx> where
870+
&'tcx (Ty<'tcx>, Const<'tcx>): Sync
871+
{
872+
}
873+
855874
impl Debug for Term<'_> {
856875
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
857876
let data = if let Some(ty) = self.ty() {
@@ -914,17 +933,19 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for Term<'tcx> {
914933
impl<'tcx> Term<'tcx> {
915934
#[inline]
916935
pub fn unpack(self) -> TermKind<'tcx> {
917-
let ptr = self.ptr.get();
936+
let ptr = unsafe {
937+
self.ptr.map_addr(|addr| NonZeroUsize::new_unchecked(addr.get() & !TAG_MASK))
938+
};
918939
// SAFETY: use of `Interned::new_unchecked` here is ok because these
919940
// pointers were originally created from `Interned` types in `pack()`,
920941
// and this is just going in the other direction.
921942
unsafe {
922-
match ptr & TAG_MASK {
943+
match self.ptr.addr().get() & TAG_MASK {
923944
TYPE_TAG => TermKind::Ty(Ty(Interned::new_unchecked(
924-
&*((ptr & !TAG_MASK) as *const WithCachedTypeInfo<ty::TyKind<'tcx>>),
945+
&*(ptr.cast::<WithCachedTypeInfo<ty::TyKind<'tcx>>>().as_ref()),
925946
))),
926947
CONST_TAG => TermKind::Const(ty::Const(Interned::new_unchecked(
927-
&*((ptr & !TAG_MASK) as *const WithCachedTypeInfo<ty::ConstData<'tcx>>),
948+
&*(ptr.cast::<WithCachedTypeInfo<ty::ConstData<'tcx>>>().as_ref()),
928949
))),
929950
_ => core::intrinsics::unreachable(),
930951
}
@@ -986,16 +1007,16 @@ impl<'tcx> TermKind<'tcx> {
9861007
TermKind::Ty(ty) => {
9871008
// Ensure we can use the tag bits.
9881009
assert_eq!(mem::align_of_val(&*ty.0.0) & TAG_MASK, 0);
989-
(TYPE_TAG, ty.0.0 as *const WithCachedTypeInfo<ty::TyKind<'tcx>> as usize)
1010+
(TYPE_TAG, NonNull::from(ty.0.0).cast())
9901011
}
9911012
TermKind::Const(ct) => {
9921013
// Ensure we can use the tag bits.
9931014
assert_eq!(mem::align_of_val(&*ct.0.0) & TAG_MASK, 0);
994-
(CONST_TAG, ct.0.0 as *const WithCachedTypeInfo<ty::ConstData<'tcx>> as usize)
1015+
(CONST_TAG, NonNull::from(ct.0.0).cast())
9951016
}
9961017
};
9971018

998-
Term { ptr: unsafe { NonZeroUsize::new_unchecked(ptr | tag) }, marker: PhantomData }
1019+
Term { ptr: unsafe { ptr.map_addr(|addr| NonZeroUsize::new_unchecked(addr.get() | tag)) }, marker: PhantomData }
9991020
}
10001021
}
10011022

0 commit comments

Comments
 (0)