Skip to content

Commit

Permalink
Auto merge of #127560 - oli-obk:safe_clobber, r=nnethercote
Browse files Browse the repository at this point in the history
Make `visit_clobber`'s impl safe

This was originally introduced in #58061 but I didn't see any perf discussion about it, so let's see what perf says.

r? `@nnethercote`
  • Loading branch information
bors committed Jul 10, 2024
2 parents 0c81f94 + 3562ec7 commit 3ad5f45
Showing 1 changed file with 3 additions and 14 deletions.
17 changes: 3 additions & 14 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rustc_span::symbol::Ident;
use rustc_span::Span;
use smallvec::{smallvec, Array, SmallVec};
use std::ops::DerefMut;
use std::{panic, ptr};
use std::panic;
use thin_vec::ThinVec;

pub trait ExpectOne<A: Array> {
Expand Down Expand Up @@ -318,19 +318,8 @@ pub trait MutVisitor: Sized {
//
// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
pub fn visit_clobber<T: DummyAstNode>(t: &mut T, f: impl FnOnce(T) -> T) {
unsafe {
// Safe because `t` is used in a read-only fashion by `read()` before
// being overwritten by `write()`.
let old_t = ptr::read(t);
let new_t =
panic::catch_unwind(panic::AssertUnwindSafe(|| f(old_t))).unwrap_or_else(|err| {
// Set `t` to some valid but possible meaningless value,
// and pass the fatal error further.
ptr::write(t, T::dummy());
panic::resume_unwind(err);
});
ptr::write(t, new_t);
}
let old_t = std::mem::replace(t, T::dummy());
*t = f(old_t);
}

// No `noop_` prefix because there isn't a corresponding method in `MutVisitor`.
Expand Down

0 comments on commit 3ad5f45

Please sign in to comment.