File tree Expand file tree Collapse file tree 1 file changed +9
-0
lines changed
compiler/rustc_middle/src/query Expand file tree Collapse file tree 1 file changed +9
-0
lines changed Original file line number Diff line number Diff 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) ]
4047pub 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.
You can’t perform that action at this time.
0 commit comments