From fd35413f757218ff666599ef3384eda4b6ae530a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Tue, 17 Oct 2023 13:44:25 +1100 Subject: [PATCH] Rewrite `Box::try_fold_with`. It can be written more simply, without needing `unsafe`. --- compiler/rustc_type_ir/src/structural_impls.rs | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_type_ir/src/structural_impls.rs b/compiler/rustc_type_ir/src/structural_impls.rs index 18ff33f6997f3..08af96ea15f0f 100644 --- a/compiler/rustc_type_ir/src/structural_impls.rs +++ b/compiler/rustc_type_ir/src/structural_impls.rs @@ -151,17 +151,9 @@ impl> TypeVisitable for Lrc { } impl> TypeFoldable for Box { - fn try_fold_with>(self, folder: &mut F) -> Result { - let raw = Box::into_raw(self); - Ok(unsafe { - // SAFETY: The raw pointer points to a valid value of type `T`. - let value = raw.read(); - // SAFETY: Converts `Box` to `Box>` which is the - // inverse of `Box::assume_init()` and should be safe. - let raw: Box> = Box::from_raw(raw.cast()); - // SAFETY: Write the mapped value back into the `Box`. - Box::write(raw, value.try_fold_with(folder)?) - }) + fn try_fold_with>(mut self, folder: &mut F) -> Result { + *self = (*self).try_fold_with(folder)?; + Ok(self) } }