Skip to main content

rustc_middle/query/
erase.rs

1//! To improve compile times and code size for the compiler itself, query
2//! values are "erased" in some contexts (e.g. inside in-memory cache types),
3//! to reduce the number of generic instantiations created during codegen.
4//!
5//! See <https://github.com/rust-lang/rust/pull/151715> for some bootstrap-time
6//! and performance benchmarks.
7
8use std::ffi::OsStr;
9use std::intrinsics::transmute_unchecked;
10use std::marker::PhantomData;
11use std::mem::MaybeUninit;
12
13use rustc_ast::tokenstream::TokenStream;
14use rustc_data_structures::steal::Steal;
15use rustc_data_structures::sync::{DynSend, DynSync};
16use rustc_span::{ErrorGuaranteed, Spanned};
17
18use crate::mono::{MonoItem, NormalizationErrorInMono};
19use crate::ty::{self, Ty, TyCtxt};
20use crate::{mir, thir, traits};
21
22unsafe extern "C" {
23    type NoAutoTraits;
24}
25
26/// Internal implementation detail of [`Erased`].
27#[derive(#[automatically_derived]
impl<Storage: ::core::marker::Copy + Copy> ::core::marker::Copy for
    ErasedData<Storage> {
}Copy, #[automatically_derived]
impl<Storage: ::core::clone::Clone + Copy> ::core::clone::Clone for
    ErasedData<Storage> {
    #[inline]
    fn clone(&self) -> ErasedData<Storage> {
        ErasedData {
            data: ::core::clone::Clone::clone(&self.data),
            no_auto_traits: ::core::clone::Clone::clone(&self.no_auto_traits),
        }
    }
}Clone)]
28pub struct ErasedData<Storage: Copy> {
29    /// We use `MaybeUninit` here to make sure it's legal to store a transmuted
30    /// value that isn't actually of type `Storage`.
31    data: MaybeUninit<Storage>,
32    /// `Storage` is an erased type, so we use an external type here to opt-out of auto traits
33    /// as those would be incorrect.
34    no_auto_traits: PhantomData<NoAutoTraits>,
35}
36
37// SAFETY: The bounds on `erase_val` ensure the types we erase are `DynSync` and `DynSend`
38unsafe impl<Storage: Copy> DynSync for ErasedData<Storage> {}
39unsafe impl<Storage: Copy> DynSend for ErasedData<Storage> {}
40
41/// Trait for types that can be erased into [`Erased<Self>`].
42///
43/// Erasing and unerasing values is performed by [`erase_val`] and [`restore_val`].
44///
45/// FIXME: This whole trait could potentially be replaced by `T: Copy` and the
46/// storage type `[u8; size_of::<T>()]` when support for that is more mature.
47pub trait Erasable: Copy {
48    /// Storage type to used for erased values of this type.
49    /// Should be `[u8; N]`, where N is equal to `size_of::<Self>`.
50    ///
51    /// [`ErasedData`] wraps this storage type in `MaybeUninit` to ensure that
52    /// transmutes to/from erased storage are well-defined.
53    type Storage: Copy;
54}
55
56/// A value of `T` that has been "erased" into some opaque storage type.
57///
58/// This is helpful for reducing the number of concrete instantiations needed
59/// during codegen when building the compiler.
60///
61/// Using an opaque type alias allows the type checker to enforce that
62/// `Erased<T>` and `Erased<U>` are still distinct types, while allowing
63/// monomorphization to see that they might actually use the same storage type.
64pub type Erased<T: Erasable> = ErasedData<impl Copy>;
65
66/// Erases a value of type `T` into `Erased<T>`.
67///
68/// `Erased<T>` and `Erased<U>` are type-checked as distinct types, but codegen
69/// can see whether they actually have the same storage type.
70#[inline(always)]
71#[define_opaque(Erased)]
72// The `DynSend` and `DynSync` bounds on `T` are used to
73// justify the safety of the implementations of these traits for `ErasedData`.
74pub fn erase_val<T: Erasable + DynSend + DynSync>(value: T) -> Erased<T> {
75    // Ensure the sizes match
76    const {
77        if size_of::<T>() != size_of::<T::Storage>() {
78            {
    ::core::panicking::panic_fmt(format_args!("size of T must match erased type <T as Erasable>::Storage"));
}panic!("size of T must match erased type <T as Erasable>::Storage")
79        }
80    };
81
82    ErasedData::<<T as Erasable>::Storage> {
83        // `transmute_unchecked` is needed here because it does not have `transmute`'s size check
84        // (and thus allows to transmute between `T` and `MaybeUninit<T::Storage>`) (we do the size
85        // check ourselves in the `const` block above).
86        //
87        // `transmute_copy` is also commonly used for this (and it would work here since
88        // `Erasable: Copy`), but `transmute_unchecked` better explains the intent.
89        //
90        // SAFETY: It is safe to transmute to MaybeUninit for types with the same sizes.
91        data: unsafe { transmute_unchecked::<T, MaybeUninit<T::Storage>>(value) },
92        no_auto_traits: PhantomData,
93    }
94}
95
96/// Restores an erased value to its real type.
97///
98/// This relies on the fact that `Erased<T>` and `Erased<U>` are type-checked
99/// as distinct types, even if they use the same storage type.
100#[inline(always)]
101#[define_opaque(Erased)]
102pub fn restore_val<T: Erasable>(erased_value: Erased<T>) -> T {
103    let ErasedData { data, .. }: ErasedData<<T as Erasable>::Storage> = erased_value;
104    // See comment in `erase_val` for why we use `transmute_unchecked`.
105    //
106    // SAFETY: Due to the use of impl Trait in `Erased` the only way to safely create an instance
107    // of `Erased` is to call `erase_val`, so we know that `erased_value.data` is a valid instance
108    // of `T` of the right size.
109    unsafe { transmute_unchecked::<MaybeUninit<T::Storage>, T>(data) }
110}
111
112impl<T> Erasable for &'_ T {
113    type Storage = [u8; size_of::<&'_ ()>()];
114}
115
116impl<T> Erasable for &'_ [T] {
117    type Storage = [u8; size_of::<&'_ [()]>()];
118}
119
120// Note: this impl does not overlap with the impl for `&'_ T` above because `RawList` is unsized
121// and does not satisfy the implicit `T: Sized` bound.
122//
123// Furthermore, even if that implicit bound was removed (by adding `T: ?Sized`) this impl still
124// wouldn't overlap because `?Sized` is equivalent to `MetaSized` and `RawList` does not satisfy
125// `MetaSized` because it contains an extern type.
126impl<H, T> Erasable for &'_ ty::RawList<H, T> {
127    type Storage = [u8; size_of::<&'_ ty::RawList<(), ()>>()];
128}
129
130impl<T> Erasable for Result<&'_ T, traits::query::NoSolution> {
131    type Storage = [u8; size_of::<Result<&'_ (), traits::query::NoSolution>>()];
132}
133
134impl<T> Erasable for Result<&'_ T, ErrorGuaranteed> {
135    type Storage = [u8; size_of::<Result<&'_ (), ErrorGuaranteed>>()];
136}
137
138impl<T> Erasable for Option<&'_ T> {
139    type Storage = [u8; size_of::<Option<&'_ ()>>()];
140}
141
142impl<T: Erasable> Erasable for ty::EarlyBinder<'_, T> {
143    type Storage = T::Storage;
144}
145
146impl<T0, T1> Erasable for (&'_ T0, &'_ T1) {
147    type Storage = [u8; size_of::<(&'_ (), &'_ ())>()];
148}
149
150impl<T0, T1, T2> Erasable for (&'_ T0, &'_ T1, &'_ T2) {
151    type Storage = [u8; size_of::<(&'_ (), &'_ (), &'_ ())>()];
152}
153
154impl<T0, T1> Erasable for (&'_ [T0], &'_ [T1]) {
155    type Storage = [u8; size_of::<(&'_ [()], &'_ [()])>()];
156}
157
158macro_rules! impl_erasable_for_types_with_no_type_params {
159    ($($ty:ty),+ $(,)?) => {
160        $(
161            impl Erasable for $ty {
162                type Storage = [u8; size_of::<$ty>()];
163            }
164        )*
165    }
166}
167
168// For types with no type parameters the erased storage for `Foo` is
169// `[u8; size_of::<Foo>()]`. ('_ lifetimes are allowed.)
170impl Erasable for usize {
    type Storage = [u8; size_of::<usize>()];
}impl_erasable_for_types_with_no_type_params! {
171    // tidy-alphabetical-start
172    (&'_ ty::CrateInherentImpls, Result<(), ErrorGuaranteed>),
173    (),
174    (traits::solve::QueryResult<'_>, &'_ traits::solve::inspect::Probe<TyCtxt<'_>>),
175    Option<&'_ OsStr>,
176    Option<&'_ [rustc_hir::PreciseCapturingArgKind<rustc_span::Symbol, rustc_span::Symbol>]>,
177    Option<(mir::ConstValue, Ty<'_>)>,
178    Option<(rustc_span::def_id::DefId, rustc_session::config::EntryFnType)>,
179    Option<rustc_abi::Align>,
180    Option<rustc_ast::expand::allocator::AllocatorKind>,
181    Option<rustc_data_structures::svh::Svh>,
182    Option<rustc_hir::ConstStability>,
183    Option<rustc_hir::CoroutineKind>,
184    Option<rustc_hir::DefaultBodyStability>,
185    Option<rustc_hir::Stability>,
186    Option<rustc_middle::middle::stability::DeprecationEntry>,
187    Option<rustc_middle::ty::AsyncDestructor>,
188    Option<rustc_middle::ty::Destructor>,
189    Option<rustc_middle::ty::IntrinsicDef>,
190    Option<rustc_middle::ty::ScalarInt>,
191    Option<rustc_span::Span>,
192    Option<rustc_span::def_id::CrateNum>,
193    Option<rustc_span::def_id::DefId>,
194    Option<rustc_span::def_id::LocalDefId>,
195    Option<rustc_target::spec::PanicStrategy>,
196    Option<ty::EarlyBinder<'_, Ty<'_>>>,
197    Option<ty::Value<'_>>,
198    Option<usize>,
199    Result<&'_ TokenStream, ()>,
200    Result<&'_ rustc_target::callconv::FnAbi<'_, Ty<'_>>, &'_ ty::layout::FnAbiError<'_>>,
201    Result<&'_ traits::ImplSource<'_, ()>, traits::CodegenObligationError>,
202    Result<&'_ ty::List<Ty<'_>>, ty::util::AlwaysRequiresDrop>,
203    Result<(&'_ Steal<thir::Thir<'_>>, thir::ExprId), ErrorGuaranteed>,
204    Result<(&'_ [Spanned<MonoItem<'_>>], &'_ [Spanned<MonoItem<'_>>]), NormalizationErrorInMono>,
205    Result<(), ErrorGuaranteed>,
206    Result<Option<ty::EarlyBinder<'_, ty::Const<'_>>>, ErrorGuaranteed>,
207    Result<Option<ty::Instance<'_>>, ErrorGuaranteed>,
208    Result<bool, &ty::layout::LayoutError<'_>>,
209    Result<mir::ConstAlloc<'_>, mir::interpret::ErrorHandled>,
210    Result<mir::ConstValue, mir::interpret::ErrorHandled>,
211    Result<rustc_abi::TyAndLayout<'_, Ty<'_>>, &ty::layout::LayoutError<'_>>,
212    Result<rustc_middle::traits::EvaluationResult, rustc_middle::traits::OverflowError>,
213    Result<rustc_middle::ty::adjustment::CoerceUnsizedInfo, ErrorGuaranteed>,
214    Result<ty::GenericArg<'_>, traits::query::NoSolution>,
215    Ty<'_>,
216    bool,
217    rustc_data_structures::svh::Svh,
218    rustc_hir::Constness,
219    rustc_hir::Defaultness,
220    rustc_hir::HirId,
221    rustc_hir::MaybeOwner<'_>,
222    rustc_hir::OpaqueTyOrigin<rustc_hir::def_id::DefId>,
223    rustc_hir::def::DefKind,
224    rustc_hir::def_id::DefId,
225    rustc_middle::hir::ProjectedMaybeOwner<'_>,
226    rustc_middle::middle::codegen_fn_attrs::SanitizerFnAttrs,
227    rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault,
228    rustc_middle::mir::ConstQualifs,
229    rustc_middle::mir::ConstValue,
230    rustc_middle::mir::interpret::AllocId,
231    rustc_middle::mir::interpret::EvalStaticInitializerRawResult<'_>,
232    rustc_middle::mir::interpret::EvalToValTreeResult<'_>,
233    rustc_middle::mono::MonoItemPartitions<'_>,
234    rustc_middle::traits::query::MethodAutoderefStepsResult<'_>,
235    rustc_middle::ty::AdtDef<'_>,
236    rustc_middle::ty::AnonConstKind,
237    rustc_middle::ty::AssocItem,
238    rustc_middle::ty::Asyncness,
239    rustc_middle::ty::Binder<'_, ty::CoroutineWitnessTypes<TyCtxt<'_>>>,
240    rustc_middle::ty::Binder<'_, ty::FnSig<'_>>,
241    rustc_middle::ty::ClosureTypeInfo<'_>,
242    rustc_middle::ty::Const<'_>,
243    rustc_middle::ty::ConstConditions<'_>,
244    rustc_middle::ty::GenericPredicates<'_>,
245    rustc_middle::ty::ImplTraitHeader<'_>,
246    rustc_middle::ty::ParamEnv<'_>,
247    rustc_middle::ty::SymbolName<'_>,
248    rustc_middle::ty::TypingEnv<'_>,
249    rustc_middle::ty::Visibility<rustc_span::def_id::DefId>,
250    rustc_middle::ty::inhabitedness::InhabitedPredicate<'_>,
251    rustc_session::Limits,
252    rustc_session::config::OptLevel,
253    rustc_session::config::SymbolManglingVersion,
254    rustc_session::cstore::CrateDepKind,
255    rustc_span::ExpnId,
256    rustc_span::Span,
257    rustc_span::Symbol,
258    rustc_target::spec::PanicStrategy,
259    usize,
260    // tidy-alphabetical-end
261}