Skip to content

Commit

Permalink
Use transmute_unchecked and make the types explicit in query type e…
Browse files Browse the repository at this point in the history
…rasure

This doesn't really change anything, but makes the code a bit more explicit/readable.
  • Loading branch information
WaffleLapkin committed Oct 3, 2023
1 parent a6dfd89 commit 61361bb
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions compiler/rustc_middle/src/query/erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use crate::mir;
use crate::query::CyclePlaceholder;
use crate::traits;
use crate::ty::{self, Ty};
use std::mem::{size_of, transmute_copy, MaybeUninit};
use std::mem::{size_of, MaybeUninit};
use std::intrinsics::transmute_unchecked;

#[derive(Copy, Clone)]
pub struct Erased<T: Copy> {
Expand Down Expand Up @@ -30,7 +31,7 @@ pub fn erase<T: EraseType>(src: T) -> Erase<T> {

Erased::<<T as EraseType>::Result> {
// SAFETY: It is safe to transmute to MaybeUninit for types with the same sizes.
data: unsafe { transmute_copy(&src) },
data: unsafe { transmute_unchecked::<T, MaybeUninit<T::Result>>(src) },
}
}

Expand All @@ -41,7 +42,7 @@ pub fn restore<T: EraseType>(value: Erase<T>) -> T {
// SAFETY: Due to the use of impl Trait in `Erase` the only way to safely create an instance
// of `Erase` is to call `erase`, so we know that `value.data` is a valid instance of `T` of
// the right size.
unsafe { transmute_copy(&value.data) }
unsafe { transmute_unchecked::<MaybeUninit<T::Result>, T>(value.data) }
}

impl<T> EraseType for &'_ T {
Expand Down

0 comments on commit 61361bb

Please sign in to comment.