A raw ref of a deref is always safe
This commit is contained in:
parent
1b3b8e7b02
commit
f2a80a0f89
@ -512,17 +512,11 @@ fn visit_expr(&mut self, expr: &'a Expr<'tcx>) {
|
|||||||
// THIR desugars UNSAFE_STATIC into *UNSAFE_STATIC_REF, where
|
// THIR desugars UNSAFE_STATIC into *UNSAFE_STATIC_REF, where
|
||||||
// UNSAFE_STATIC_REF holds the addr of the UNSAFE_STATIC, so: take two steps
|
// UNSAFE_STATIC_REF holds the addr of the UNSAFE_STATIC, so: take two steps
|
||||||
&& let ExprKind::Deref { arg } = self.thir[arg].kind
|
&& 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!
|
// Taking a raw ref to a deref place expr is always safe.
|
||||||
// We short-circuit to not recursively traverse this expression.
|
// Make sure the expression we're deref'ing is safe, though.
|
||||||
|
visit::walk_expr(self, &self.thir[arg]);
|
||||||
return;
|
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 } => {
|
ExprKind::Deref { arg } => {
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
// (it's fine to create raw refs to places!) the following derefs the ptr before creating its ref!
|
// (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);
|
static mut DEREF_BYTE_PTR: *mut u8 = ptr::addr_of_mut!(*BYTE_PTR);
|
||||||
//~^ ERROR: use of mutable static
|
//~^ ERROR: use of mutable static
|
||||||
//~| ERROR: dereference of raw pointer
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = unsafe { DEREF_BYTE_PTR };
|
let _ = unsafe { DEREF_BYTE_PTR };
|
||||||
|
@ -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
|
= 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`.
|
For more information about this error, try `rustc --explain E0133`.
|
||||||
|
14
tests/ui/unsafe/place-expr-safe.rs
Normal file
14
tests/ui/unsafe/place-expr-safe.rs
Normal file
@ -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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user