Skip to content

Commit caf9e2f

Browse files
committed
Auto merge of rust-lang#127560 - oli-obk:safe_clobber, r=<try>
Make `visit_clobber`'s impl safe This was originally introduced in rust-lang#58061 but I didn't see any perf discussion about it, so let's see what perf says. r? `@nnethercote`
2 parents 7caf672 + 3562ec7 commit caf9e2f

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

compiler/rustc_ast/src/mut_visit.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_span::symbol::Ident;
2020
use rustc_span::Span;
2121
use smallvec::{smallvec, Array, SmallVec};
2222
use std::ops::DerefMut;
23-
use std::{panic, ptr};
23+
use std::panic;
2424
use thin_vec::ThinVec;
2525

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

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

0 commit comments

Comments
 (0)