Skip to content

Commit

Permalink
A raw ref of a deref is always safe
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Oct 7, 2024
1 parent 1b3b8e7 commit f2a80a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
12 changes: 3 additions & 9 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -512,17 +512,11 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
// THIR desugars UNSAFE_STATIC into *UNSAFE_STATIC_REF, where
// UNSAFE_STATIC_REF holds the addr of the UNSAFE_STATIC, so: take two steps
&& let ExprKind::Deref { arg } = self.thir[arg].kind
// FIXME(workingjubiee): we lack a clear reason to reject ThreadLocalRef here,
// but we also have no conclusive reason to allow it either!
&& let ExprKind::StaticRef { .. } = self.thir[arg].kind
{
// A raw ref to a place expr, even an "unsafe static", is okay!
// We short-circuit to not recursively traverse this expression.
// Taking a raw ref to a deref place expr is always safe.
// Make sure the expression we're deref'ing is safe, though.
visit::walk_expr(self, &self.thir[arg]);
return;
// note: const_mut_refs enables this code, and it currently remains unsafe:
// static mut BYTE: u8 = 0;
// static mut BYTE_PTR: *mut u8 = unsafe { addr_of_mut!(BYTE) };
// static mut DEREF_BYTE_PTR: *mut u8 = unsafe { addr_of_mut!(*BYTE_PTR) };
}
}
ExprKind::Deref { arg } => {
Expand Down
1 change: 0 additions & 1 deletion tests/ui/static/raw-ref-deref-without-unsafe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ static mut BYTE_PTR: *mut u8 = ptr::addr_of_mut!(BYTE);
// (it's fine to create raw refs to places!) the following derefs the ptr before creating its ref!
static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR);
//~^ ERROR: use of mutable static
//~| ERROR: dereference of raw pointer

fn main() {
let _ = unsafe { DEREF_BYTE_PTR };
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/static/raw-ref-deref-without-unsafe.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ LL | static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR);
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior

error: aborting due to 2 previous errors
error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0133`.
14 changes: 14 additions & 0 deletions tests/ui/unsafe/place-expr-safe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//@ check-pass

fn main() {
let ptr = std::ptr::null_mut::<i32>();
let addr = &raw const *ptr;

let local = 1;
let ptr = &local as *const i32;
let addr = &raw const *ptr;

let boxed = Box::new(1);
let ptr = &*boxed as *const i32;
let addr = &raw const *ptr;
}

0 comments on commit f2a80a0

Please sign in to comment.