Skip to content

Commit 55f8efe

Browse files
committed
Add an explanation for transmute_unchecked
1 parent b3d5025 commit 55f8efe

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

Diff for: compiler/rustc_middle/src/query/erase.rs

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ pub fn erase<T: EraseType>(src: T) -> Erase<T> {
3030
};
3131

3232
Erased::<<T as EraseType>::Result> {
33+
// `transmute_unchecked` is needed here because it does not have `transmute`'s size check
34+
// (and thus allows to transmute between `T`` and `MaybeUninit<T::Result>`) (we do the size
35+
// check ourselves in the `const` block above).
36+
//
37+
// `transmute_copy` is also commonly used for this (and it would work here since
38+
// `EraseType: Copy`), but `transmute_unchecked` better explains the intent.
39+
//
3340
// SAFETY: It is safe to transmute to MaybeUninit for types with the same sizes.
3441
data: unsafe { transmute_unchecked::<T, MaybeUninit<T::Result>>(src) },
3542
}
@@ -39,6 +46,8 @@ pub fn erase<T: EraseType>(src: T) -> Erase<T> {
3946
#[inline(always)]
4047
pub fn restore<T: EraseType>(value: Erase<T>) -> T {
4148
let value: Erased<<T as EraseType>::Result> = value;
49+
// See comment in `erase` for why we use `transmute_unchecked`.
50+
//
4251
// SAFETY: Due to the use of impl Trait in `Erase` the only way to safely create an instance
4352
// of `Erase` is to call `erase`, so we know that `value.data` is a valid instance of `T` of
4453
// the right size.

0 commit comments

Comments
 (0)