Skip to content

Commit 178ba0e

Browse files
committed
Rewrite Box<T>::try_fold_with.
It can be written more simply, without needing `unsafe`.
1 parent 847c8ba commit 178ba0e

File tree

1 file changed

+3
-11
lines changed

1 file changed

+3
-11
lines changed

compiler/rustc_type_ir/src/structural_impls.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,9 @@ impl<I: Interner, T: TypeVisitable<I>> TypeVisitable<I> for Lrc<T> {
151151
}
152152

153153
impl<I: Interner, T: TypeFoldable<I>> TypeFoldable<I> for Box<T> {
154-
fn try_fold_with<F: FallibleTypeFolder<I>>(self, folder: &mut F) -> Result<Self, F::Error> {
155-
let raw = Box::into_raw(self);
156-
Ok(unsafe {
157-
// SAFETY: The raw pointer points to a valid value of type `T`.
158-
let value = raw.read();
159-
// SAFETY: Converts `Box<T>` to `Box<MaybeUninit<T>>` which is the
160-
// inverse of `Box::assume_init()` and should be safe.
161-
let raw: Box<mem::MaybeUninit<T>> = Box::from_raw(raw.cast());
162-
// SAFETY: Write the mapped value back into the `Box`.
163-
Box::write(raw, value.try_fold_with(folder)?)
164-
})
154+
fn try_fold_with<F: FallibleTypeFolder<I>>(mut self, folder: &mut F) -> Result<Self, F::Error> {
155+
*self = (*self).try_fold_with(folder)?;
156+
Ok(self)
165157
}
166158
}
167159

0 commit comments

Comments
 (0)