Skip to content

Commit 7d7a522

Browse files
committed
ZJIT: Prefer raw pointer over references in with_ruby_vm()
When references show up on in the type declaration, it's an invitation to think about how long the implicit lifetime is. This code doesn't do anything tricky lifetime-wise, so it looks better declaring only raw pointers.
1 parent 3d6c043 commit 7d7a522

1 file changed

Lines changed: 10 additions & 10 deletions

File tree

zjit/src/cruby.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,24 +1232,24 @@ pub mod test_utils {
12321232
pub fn with_rubyvm<T>(mut func: impl FnMut() -> T) -> T {
12331233
RUBY_VM_INIT.call_once(boot_rubyvm);
12341234

1235-
// Set up a callback wrapper to store a return value
1236-
let mut result: Option<T> = None;
1237-
let mut data: &mut dyn FnMut() = &mut || {
1238-
// Store the result externally
1239-
result.replace(func());
1240-
};
1241-
12421235
// Invoke callback through rb_protect() so exceptions don't crash the process.
12431236
// "Fun" double pointer dance to get a thin function pointer to pass through C
12441237
unsafe extern "C" fn callback_wrapper(data: VALUE) -> VALUE {
12451238
// SAFETY: shorter lifetime than the data local in the caller frame
1246-
let callback: *mut &mut dyn FnMut() = std::ptr::with_exposed_provenance_mut(data.0);
1247-
unsafe { (*callback)() };
1239+
let callback: *const *mut dyn FnMut() = std::ptr::with_exposed_provenance_mut(data.0);
1240+
unsafe { (**callback)() };
12481241
Qnil
12491242
}
12501243

1244+
// Set up a callback wrapper to store the return value
1245+
let mut result: Option<T> = None;
1246+
let mut func_wrapper = || {
1247+
result.replace(func());
1248+
};
1249+
let data: *mut dyn FnMut() = &raw mut func_wrapper;
1250+
let data: *const *mut dyn FnMut() = &raw const data;
12511251
let mut state: c_int = 0;
1252-
unsafe { super::rb_protect(Some(callback_wrapper), VALUE((&raw mut data).expose_provenance()), &mut state) };
1252+
unsafe { super::rb_protect(Some(callback_wrapper), VALUE(data.expose_provenance()), &mut state) };
12531253
if state != 0 {
12541254
unsafe { rb_zjit_print_exception(); }
12551255
assert_eq!(0, state, "Exceptional unwind in callback. Ruby exception?");

0 commit comments

Comments
 (0)