Skip to content

Commit

Permalink
fix(visit): Improve Map implementation for Box (#8819)
Browse files Browse the repository at this point in the history
**Related issue (if exists):**

 - Closes #8817
  • Loading branch information
dsherret committed Apr 9, 2024
1 parent aa4ed06 commit dc04657
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions 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<T> {
Expand All @@ -13,17 +13,15 @@ pub trait Map<T> {
}

impl<T> Map<T> for Box<T> {
fn map<F>(mut self, f: F) -> Self
fn map<F>(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)));
Expand Down

0 comments on commit dc04657

Please sign in to comment.