Rollup merge of #129248 - compiler-errors:raw-ref-deref, r=nnethercote
Taking a raw ref (`&raw (const|mut)`) of a deref of pointer (`*ptr`) is always safe T-opsem decided in https://github.com/rust-lang/reference/pull/1387 that `*ptr` is only unsafe if the place is accessed. This means that taking a raw ref of a deref expr is always safe, since it doesn't constitute a read. This also relaxes the `DEREF_NULLPTR` lint to stop warning in the case of raw ref of a deref'd nullptr, and updates its docs to reflect that change in the UB specification. This does not change the behavior of `addr_of!((*ptr).field)`, since field projections still require the projection is in-bounds. I'm on the fence whether this requires an FCP, since it's something that is guaranteed by the reference you could ostensibly call this a bugfix since we were counting truly safe operations as unsafe. Perhaps someone on opsem has a strong opinion? cc `@rust-lang/opsem`
This commit is contained in:
commit
93bf791e8b
8 changed files with 46 additions and 52 deletions
|
@ -509,20 +509,12 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
|
|||
}
|
||||
ExprKind::RawBorrow { arg, .. } => {
|
||||
if let ExprKind::Scope { value: arg, .. } = self.thir[arg].kind
|
||||
// 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 } => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue