From dc046572def13a3eb625520c5a8bfd651b86f3a3 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 8 Apr 2024 21:21:03 -0400 Subject: [PATCH] fix(visit): Improve `Map` implementation for `Box` (#8819) **Related issue (if exists):** - Closes #8817 --- crates/swc_visit/src/util/map.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/swc_visit/src/util/map.rs b/crates/swc_visit/src/util/map.rs index 1b3eb7d07b8c..992d2332645f 100644 --- a/crates/swc_visit/src/util/map.rs +++ b/crates/swc_visit/src/util/map.rs @@ -1,4 +1,4 @@ -use std::{mem, ptr}; +use std::ptr; /// Copied from `syntax::ptr::P` of rustc. pub trait Map { @@ -13,17 +13,15 @@ pub trait Map { } impl Map for Box { - fn map(mut self, f: F) -> Self + fn map(self, f: F) -> Self where F: FnOnce(T) -> T, { - let p: *mut T = &mut *self; - // Leak self in case of panic. // FIXME(eddyb) Use some sort of "free guard" that // only deallocates, without dropping the pointee, // in case the call the `f` below ends in a panic. - mem::forget(self); + let p = Box::into_raw(self); unsafe { ptr::write(p, f(ptr::read(p)));