Skip to content

Commit

Permalink
Eliminate cast from &T to &mut T
Browse files Browse the repository at this point in the history
Summary:
This code is 100% UB and no longer compiles in Rust 1.73.

```
error: casting `&T` to `&mut T` is undefined behavior, even if the reference is unused, consider instead using an `UnsafeCell`
   --> fbcode/hermetic_infra/reverie/experimental/reverie-sabre/src/thread.rs:652:32
    |
652 |             let slot_map_mut = &mut *((&*SLOT_MAP as *const _) as *mut _);
    |                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = note: `#[deny(invalid_reference_casting)]` on by default
```

Reviewed By: JakobDegen

Differential Revision: D50488164

fbshipit-source-id: ea649d97f47ccc329a44650aff5dc467ed12b48c
  • Loading branch information
David Tolnay authored and facebook-github-bot committed Oct 20, 2023
1 parent 890bb93 commit 613bea7
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions experimental/reverie-sabre/src/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ impl<E: EventSink> Thread<E> {
mod tests {
use std::collections::HashSet;
use std::mem;
use std::ptr;
use std::sync::atomic::fence;
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
Expand Down Expand Up @@ -638,7 +639,6 @@ mod tests {
Thread::<TestEventSink>::current()
}

#[allow(clippy::cast_ref_to_mut)]
pub fn run_test_in_new_thread<T>(t: T)
where
T: 'static + Send + FnOnce(),
Expand All @@ -649,7 +649,8 @@ mod tests {
// test run. This is safe because each test is running inside a single
// mutex, so only one test will be accessing the static variable at once
unsafe {
let slot_map_mut = &mut *((&*SLOT_MAP as *const _) as *mut _);
// FIXME(JakobDegen): This is UB
let slot_map_mut = ptr::addr_of!(*SLOT_MAP).cast_mut();

*slot_map_mut = SlotMap::<ThreadRepr>::new();
}
Expand Down

0 comments on commit 613bea7

Please sign in to comment.