diff options
| author | bors <bors@rust-lang.org> | 2024-07-10 22:01:02 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2024-07-10 22:01:02 +0000 |
| commit | 3ad5f459b35769b8ad9f0b364d3dabb3721b9117 (patch) | |
| tree | 0b0a91d42f5f37e711c51ee381a8d4f373008907 | |
| parent | 0c81f94b9a6207fb1fc080caa83584dea2d71fc6 (diff) | |
| parent | 3562ec74cae80cfccda7d388c186609254437426 (diff) | |
| download | rust-3ad5f459b35769b8ad9f0b364d3dabb3721b9117.tar.gz rust-3ad5f459b35769b8ad9f0b364d3dabb3721b9117.zip | |
Auto merge of #127560 - oli-obk:safe_clobber, r=nnethercote
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`
| -rw-r--r-- | compiler/rustc_ast/src/mut_visit.rs | 17 |
1 files changed, 3 insertions, 14 deletions
diff --git a/compiler/rustc_ast/src/mut_visit.rs b/compiler/rustc_ast/src/mut_visit.rs index cbf21317f1a..1c1163551db 100644 --- a/compiler/rustc_ast/src/mut_visit.rs +++ b/compiler/rustc_ast/src/mut_visit.rs @@ -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> { @@ -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`. |
