1
Fork 0

Get rid of some #[allow(static_mut_refs)]

This commit is contained in:
Pavel Grigorenko 2024-02-23 14:09:37 +03:00
parent 52cea084bd
commit 58c8c0853f
No known key found for this signature in database
GPG key ID: 52BFCC6ED389F777
6 changed files with 31 additions and 42 deletions

View file

@ -49,7 +49,7 @@
use alloc::boxed::Box; use alloc::boxed::Box;
use core::any::Any; use core::any::Any;
use core::mem::{self, ManuallyDrop}; use core::mem::{self, ManuallyDrop};
use core::ptr; use core::ptr::{addr_of, addr_of_mut};
use libc::{c_int, c_uint, c_void}; use libc::{c_int, c_uint, c_void};
// NOTE(nbdd0121): The `canary` field is part of stable ABI. // NOTE(nbdd0121): The `canary` field is part of stable ABI.
@ -135,7 +135,7 @@ mod imp {
macro_rules! ptr { macro_rules! ptr {
(0) => (0); (0) => (0);
($e:expr) => { ($e:expr) => {
(($e as usize) - (&imp::__ImageBase as *const _ as usize)) as u32 (($e as usize) - (addr_of!(imp::__ImageBase) as usize)) as u32
} }
} }
} }
@ -220,7 +220,7 @@ extern "C" {
// This is fine since the MSVC runtime uses string comparison on the type name // This is fine since the MSVC runtime uses string comparison on the type name
// to match TypeDescriptors rather than pointer equality. // to match TypeDescriptors rather than pointer equality.
static mut TYPE_DESCRIPTOR: _TypeDescriptor = _TypeDescriptor { static mut TYPE_DESCRIPTOR: _TypeDescriptor = _TypeDescriptor {
pVFTable: unsafe { &TYPE_INFO_VTABLE } as *const _ as *const _, pVFTable: unsafe { addr_of!(TYPE_INFO_VTABLE) } as *const _,
spare: core::ptr::null_mut(), spare: core::ptr::null_mut(),
name: TYPE_NAME, name: TYPE_NAME,
}; };
@ -261,9 +261,6 @@ cfg_if::cfg_if! {
} }
} }
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[cfg_attr(bootstrap, allow(static_mut_ref))]
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 { pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
use core::intrinsics::atomic_store_seqcst; use core::intrinsics::atomic_store_seqcst;
@ -274,8 +271,9 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
// The ManuallyDrop is needed here since we don't want Exception to be // The ManuallyDrop is needed here since we don't want Exception to be
// dropped when unwinding. Instead it will be dropped by exception_cleanup // dropped when unwinding. Instead it will be dropped by exception_cleanup
// which is invoked by the C++ runtime. // which is invoked by the C++ runtime.
let mut exception = ManuallyDrop::new(Exception { canary: &TYPE_DESCRIPTOR, data: Some(data) }); let mut exception =
let throw_ptr = &mut exception as *mut _ as *mut _; ManuallyDrop::new(Exception { canary: addr_of!(TYPE_DESCRIPTOR), data: Some(data) });
let throw_ptr = addr_of_mut!(exception) as *mut _;
// This... may seems surprising, and justifiably so. On 32-bit MSVC the // This... may seems surprising, and justifiably so. On 32-bit MSVC the
// pointers between these structure are just that, pointers. On 64-bit MSVC, // pointers between these structure are just that, pointers. On 64-bit MSVC,
@ -298,23 +296,23 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
// In any case, we basically need to do something like this until we can // In any case, we basically need to do something like this until we can
// express more operations in statics (and we may never be able to). // express more operations in statics (and we may never be able to).
atomic_store_seqcst( atomic_store_seqcst(
&mut THROW_INFO.pmfnUnwind as *mut _ as *mut u32, addr_of_mut!(THROW_INFO.pmfnUnwind) as *mut u32,
ptr!(exception_cleanup) as u32, ptr!(exception_cleanup) as u32,
); );
atomic_store_seqcst( atomic_store_seqcst(
&mut THROW_INFO.pCatchableTypeArray as *mut _ as *mut u32, addr_of_mut!(THROW_INFO.pCatchableTypeArray) as *mut u32,
ptr!(&CATCHABLE_TYPE_ARRAY as *const _) as u32, ptr!(addr_of!(CATCHABLE_TYPE_ARRAY)) as u32,
); );
atomic_store_seqcst( atomic_store_seqcst(
&mut CATCHABLE_TYPE_ARRAY.arrayOfCatchableTypes[0] as *mut _ as *mut u32, addr_of_mut!(CATCHABLE_TYPE_ARRAY.arrayOfCatchableTypes[0]) as *mut u32,
ptr!(&CATCHABLE_TYPE as *const _) as u32, ptr!(addr_of!(CATCHABLE_TYPE)) as u32,
); );
atomic_store_seqcst( atomic_store_seqcst(
&mut CATCHABLE_TYPE.pType as *mut _ as *mut u32, addr_of_mut!(CATCHABLE_TYPE.pType) as *mut u32,
ptr!(&TYPE_DESCRIPTOR as *const _) as u32, ptr!(addr_of!(TYPE_DESCRIPTOR)) as u32,
); );
atomic_store_seqcst( atomic_store_seqcst(
&mut CATCHABLE_TYPE.copyFunction as *mut _ as *mut u32, addr_of_mut!(CATCHABLE_TYPE.copyFunction) as *mut u32,
ptr!(exception_copy) as u32, ptr!(exception_copy) as u32,
); );
@ -322,12 +320,9 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
fn _CxxThrowException(pExceptionObject: *mut c_void, pThrowInfo: *mut u8) -> !; fn _CxxThrowException(pExceptionObject: *mut c_void, pThrowInfo: *mut u8) -> !;
} }
_CxxThrowException(throw_ptr, &mut THROW_INFO as *mut _ as *mut _); _CxxThrowException(throw_ptr, addr_of_mut!(THROW_INFO) as *mut _);
} }
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[cfg_attr(bootstrap, allow(static_mut_ref))]
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
pub unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send> { pub unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send> {
// A null payload here means that we got here from the catch (...) of // A null payload here means that we got here from the catch (...) of
// __rust_try. This happens when a non-Rust foreign exception is caught. // __rust_try. This happens when a non-Rust foreign exception is caught.
@ -335,8 +330,8 @@ pub unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send> {
super::__rust_foreign_exception(); super::__rust_foreign_exception();
} }
let exception = payload as *mut Exception; let exception = payload as *mut Exception;
let canary = ptr::addr_of!((*exception).canary).read(); let canary = addr_of!((*exception).canary).read();
if !ptr::eq(canary, &TYPE_DESCRIPTOR) { if !core::ptr::eq(canary, addr_of!(TYPE_DESCRIPTOR)) {
// A foreign Rust exception. // A foreign Rust exception.
super::__rust_foreign_exception(); super::__rust_foreign_exception();
} }

View file

@ -337,9 +337,6 @@ pub mod panic_count {
#[doc(hidden)] #[doc(hidden)]
#[cfg(not(feature = "panic_immediate_abort"))] #[cfg(not(feature = "panic_immediate_abort"))]
#[unstable(feature = "update_panic_count", issue = "none")] #[unstable(feature = "update_panic_count", issue = "none")]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[cfg_attr(bootstrap, allow(static_mut_ref))]
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
pub mod panic_count { pub mod panic_count {
use crate::cell::Cell; use crate::cell::Cell;
use crate::sync::atomic::{AtomicUsize, Ordering}; use crate::sync::atomic::{AtomicUsize, Ordering};

View file

@ -180,8 +180,6 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[cfg_attr(not(test), rustc_diagnostic_item = "thread_local_macro")] #[cfg_attr(not(test), rustc_diagnostic_item = "thread_local_macro")]
#[allow_internal_unstable(thread_local_internals)] #[allow_internal_unstable(thread_local_internals)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
macro_rules! thread_local { macro_rules! thread_local {
// empty (base case for the recursion) // empty (base case for the recursion)
() => {}; () => {};

View file

@ -1,8 +1,8 @@
//! Ensure that thread-local statics get deallocated when the thread dies. //! Ensure that thread-local statics get deallocated when the thread dies.
#![feature(thread_local)] #![feature(thread_local)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#![allow(static_mut_refs)] use std::ptr::addr_of;
#[thread_local] #[thread_local]
static mut TLS: u8 = 0; static mut TLS: u8 = 0;
@ -12,7 +12,7 @@ unsafe impl Send for SendRaw {}
fn main() { fn main() {
unsafe { unsafe {
let dangling_ptr = std::thread::spawn(|| SendRaw(&TLS as *const u8)).join().unwrap(); let dangling_ptr = std::thread::spawn(|| SendRaw(addr_of!(TLS))).join().unwrap();
let _val = *dangling_ptr.0; //~ ERROR: has been freed let _val = *dangling_ptr.0; //~ ERROR: has been freed
} }
} }

View file

@ -1,8 +1,8 @@
use std::ptr::addr_of;
static mut FOO: i32 = 42; static mut FOO: i32 = 42;
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint static BAR: Foo = Foo(unsafe { addr_of!(FOO) });
#[allow(static_mut_refs)]
static BAR: Foo = Foo(unsafe { &FOO as *const _ });
#[allow(dead_code)] #[allow(dead_code)]
struct Foo(*const i32); struct Foo(*const i32);

View file

@ -8,9 +8,8 @@
//! test, we also check that thread-locals act as per-thread statics. //! test, we also check that thread-locals act as per-thread statics.
#![feature(thread_local)] #![feature(thread_local)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#![allow(static_mut_refs)]
use std::ptr::addr_of_mut;
use std::thread; use std::thread;
#[thread_local] #[thread_local]
@ -23,8 +22,8 @@ static mut C: u8 = 0;
#[thread_local] #[thread_local]
static READ_ONLY: u8 = 42; static READ_ONLY: u8 = 42;
unsafe fn get_a_ref() -> *mut u8 { unsafe fn get_a_ptr() -> *mut u8 {
&mut A addr_of_mut!(A)
} }
struct Sender(*mut u8); struct Sender(*mut u8);
@ -35,12 +34,12 @@ fn main() {
let _val = READ_ONLY; let _val = READ_ONLY;
let ptr = unsafe { let ptr = unsafe {
let x = get_a_ref(); let x = get_a_ptr();
*x = 5; *x = 5;
assert_eq!(A, 5); assert_eq!(A, 5);
B = 15; B = 15;
C = 25; C = 25;
Sender(&mut A) Sender(addr_of_mut!(A))
}; };
thread::spawn(move || unsafe { thread::spawn(move || unsafe {
@ -51,18 +50,18 @@ fn main() {
assert_eq!(C, 25); assert_eq!(C, 25);
B = 14; B = 14;
C = 24; C = 24;
let y = get_a_ref(); let y = get_a_ptr();
assert_eq!(*y, 0); assert_eq!(*y, 0);
*y = 4; *y = 4;
assert_eq!(*ptr.0, 5); assert_eq!(*ptr.0, 5);
assert_eq!(A, 4); assert_eq!(A, 4);
assert_eq!(*get_a_ref(), 4); assert_eq!(*get_a_ptr(), 4);
}) })
.join() .join()
.unwrap(); .unwrap();
unsafe { unsafe {
assert_eq!(*get_a_ref(), 5); assert_eq!(*get_a_ptr(), 5);
assert_eq!(A, 5); assert_eq!(A, 5);
assert_eq!(B, 15); assert_eq!(B, 15);
assert_eq!(C, 24); assert_eq!(C, 24);