Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(visit): Improve Map implementation for Box #8819

Merged
merged 6 commits into from
Apr 9, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions crates/swc_visit/src/util/map.rs
Original file line number Diff line number Diff line change
@@ -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