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::mir::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
112// FIXME(#151565): Using `T: ?Sized` here should let us remove the separate
113// impls for fat reference types.
114impl<T> Erasable for &'_ T {
115    type Storage = [u8; size_of::<&'_ ()>()];
116}
117
118impl<T> Erasable for &'_ [T] {
119    type Storage = [u8; size_of::<&'_ [()]>()];
120}
121
122impl<T> Erasable for &'_ ty::List<T> {
123    type Storage = [u8; size_of::<&'_ ty::List<()>>()];
124}
125
126impl<T> Erasable for &'_ ty::ListWithCachedTypeInfo<T> {
127    type Storage = [u8; size_of::<&'_ ty::ListWithCachedTypeInfo<()>>()];
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
150macro_rules! impl_erasable_for_types_with_no_type_params {
151    ($($ty:ty),+ $(,)?) => {
152        $(
153            impl Erasable for $ty {
154                type Storage = [u8; size_of::<$ty>()];
155            }
156        )*
157    }
158}
159
160// For types with no type parameters the erased storage for `Foo` is
161// `[u8; size_of::<Foo>()]`. ('_ lifetimes are allowed.)
162impl Erasable for usize {
    type Storage = [u8; size_of::<usize>()];
}impl_erasable_for_types_with_no_type_params! {
163    // tidy-alphabetical-start
164    (&'_ ty::CrateInherentImpls, Result<(), ErrorGuaranteed>),
165    (),
166    (traits::solve::QueryResult<'_>, &'_ traits::solve::inspect::Probe<TyCtxt<'_>>),
167    Option<&'_ OsStr>,
168    Option<&'_ [rustc_hir::PreciseCapturingArgKind<rustc_span::Symbol, rustc_span::Symbol>]>,
169    Option<(mir::ConstValue, Ty<'_>)>,
170    Option<(rustc_span::def_id::DefId, rustc_session::config::EntryFnType)>,
171    Option<rustc_abi::Align>,
172    Option<rustc_ast::expand::allocator::AllocatorKind>,
173    Option<rustc_data_structures::svh::Svh>,
174    Option<rustc_hir::ConstStability>,
175    Option<rustc_hir::CoroutineKind>,
176    Option<rustc_hir::DefaultBodyStability>,
177    Option<rustc_hir::Stability>,
178    Option<rustc_middle::middle::stability::DeprecationEntry>,
179    Option<rustc_middle::ty::AsyncDestructor>,
180    Option<rustc_middle::ty::Destructor>,
181    Option<rustc_middle::ty::IntrinsicDef>,
182    Option<rustc_middle::ty::ScalarInt>,
183    Option<rustc_span::Span>,
184    Option<rustc_span::def_id::CrateNum>,
185    Option<rustc_span::def_id::DefId>,
186    Option<rustc_span::def_id::LocalDefId>,
187    Option<rustc_target::spec::PanicStrategy>,
188    Option<ty::EarlyBinder<'_, Ty<'_>>>,
189    Option<ty::Value<'_>>,
190    Option<usize>,
191    Result<&'_ TokenStream, ()>,
192    Result<&'_ rustc_target::callconv::FnAbi<'_, Ty<'_>>, &'_ ty::layout::FnAbiError<'_>>,
193    Result<&'_ traits::ImplSource<'_, ()>, traits::CodegenObligationError>,
194    Result<&'_ ty::List<Ty<'_>>, ty::util::AlwaysRequiresDrop>,
195    Result<(&'_ Steal<thir::Thir<'_>>, thir::ExprId), ErrorGuaranteed>,
196    Result<(&'_ [Spanned<MonoItem<'_>>], &'_ [Spanned<MonoItem<'_>>]), NormalizationErrorInMono>,
197    Result<(), ErrorGuaranteed>,
198    Result<Option<ty::EarlyBinder<'_, ty::Const<'_>>>, ErrorGuaranteed>,
199    Result<Option<ty::Instance<'_>>, ErrorGuaranteed>,
200    Result<bool, &ty::layout::LayoutError<'_>>,
201    Result<mir::ConstAlloc<'_>, mir::interpret::ErrorHandled>,
202    Result<mir::ConstValue, mir::interpret::ErrorHandled>,
203    Result<rustc_abi::TyAndLayout<'_, Ty<'_>>, &ty::layout::LayoutError<'_>>,
204    Result<rustc_middle::traits::EvaluationResult, rustc_middle::traits::OverflowError>,
205    Result<rustc_middle::ty::adjustment::CoerceUnsizedInfo, ErrorGuaranteed>,
206    Result<ty::GenericArg<'_>, traits::query::NoSolution>,
207    Ty<'_>,
208    bool,
209    rustc_data_structures::svh::Svh,
210    rustc_hir::Constness,
211    rustc_hir::Defaultness,
212    rustc_hir::HirId,
213    rustc_hir::MaybeOwner<'_>,
214    rustc_hir::OpaqueTyOrigin<rustc_hir::def_id::DefId>,
215    rustc_hir::def::DefKind,
216    rustc_hir::def_id::DefId,
217    rustc_middle::middle::codegen_fn_attrs::SanitizerFnAttrs,
218    rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault,
219    rustc_middle::mir::ConstQualifs,
220    rustc_middle::mir::ConstValue,
221    rustc_middle::mir::interpret::AllocId,
222    rustc_middle::mir::interpret::EvalStaticInitializerRawResult<'_>,
223    rustc_middle::mir::interpret::EvalToValTreeResult<'_>,
224    rustc_middle::mir::mono::MonoItemPartitions<'_>,
225    rustc_middle::traits::query::MethodAutoderefStepsResult<'_>,
226    rustc_middle::ty::AdtDef<'_>,
227    rustc_middle::ty::AnonConstKind,
228    rustc_middle::ty::AssocItem,
229    rustc_middle::ty::Asyncness,
230    rustc_middle::ty::Binder<'_, ty::CoroutineWitnessTypes<TyCtxt<'_>>>,
231    rustc_middle::ty::Binder<'_, ty::FnSig<'_>>,
232    rustc_middle::ty::ClosureTypeInfo<'_>,
233    rustc_middle::ty::Const<'_>,
234    rustc_middle::ty::ConstConditions<'_>,
235    rustc_middle::ty::GenericPredicates<'_>,
236    rustc_middle::ty::ImplTraitHeader<'_>,
237    rustc_middle::ty::ParamEnv<'_>,
238    rustc_middle::ty::SymbolName<'_>,
239    rustc_middle::ty::TypingEnv<'_>,
240    rustc_middle::ty::Visibility<rustc_span::def_id::DefId>,
241    rustc_middle::ty::inhabitedness::InhabitedPredicate<'_>,
242    rustc_session::Limits,
243    rustc_session::config::OptLevel,
244    rustc_session::config::SymbolManglingVersion,
245    rustc_session::cstore::CrateDepKind,
246    rustc_span::ExpnId,
247    rustc_span::Span,
248    rustc_span::Symbol,
249    rustc_target::spec::PanicStrategy,
250    usize,
251    // tidy-alphabetical-end
252}