From 61361bb2129208c67134432f0bb1264749ed7180 Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 3 Oct 2023 21:12:23 +0000 Subject: [PATCH] Use `transmute_unchecked` and make the types explicit in query type erasure This doesn't really change anything, but makes the code a bit more explicit/readable. --- compiler/rustc_middle/src/query/erase.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_middle/src/query/erase.rs b/compiler/rustc_middle/src/query/erase.rs index 8ba3764bcc317..88af3eac6ae99 100644 --- a/compiler/rustc_middle/src/query/erase.rs +++ b/compiler/rustc_middle/src/query/erase.rs @@ -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 { @@ -30,7 +31,7 @@ pub fn erase(src: T) -> Erase { Erased::<::Result> { // SAFETY: It is safe to transmute to MaybeUninit for types with the same sizes. - data: unsafe { transmute_copy(&src) }, + data: unsafe { transmute_unchecked::>(src) }, } } @@ -41,7 +42,7 @@ pub fn restore(value: Erase) -> 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::, T>(value.data) } } impl EraseType for &'_ T {