Skip to content

Commit

Permalink
Rollup merge of #123967 - RalfJung:static_mut_refs, r=Nilstrieb
Browse files Browse the repository at this point in the history
static_mut_refs: use raw pointers to remove the remaining FIXME

Using `SyncUnsafeCell` would not make a lot of sense IMO.
  • Loading branch information
jieyouxu authored Apr 20, 2024
2 parents e9e936c + b4a4645 commit ccd9880
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
15 changes: 11 additions & 4 deletions compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
#![feature(no_core, lang_items, never_type, linkage, extern_types, thread_local, repr_simd)]
#![feature(
no_core,
lang_items,
never_type,
linkage,
extern_types,
thread_local,
repr_simd,
raw_ref_op
)]
#![no_core]
#![allow(dead_code, non_camel_case_types, internal_features)]

Expand Down Expand Up @@ -112,9 +121,7 @@ fn start<T: Termination + 'static>(

static mut NUM: u8 = 6 * 7;

// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[allow(static_mut_refs)]
static NUM_REF: &'static u8 = unsafe { &NUM };
static NUM_REF: &'static u8 = unsafe { &*&raw const NUM };

unsafe fn zeroed<T>() -> T {
let mut uninit = MaybeUninit { uninit: () };
Expand Down
6 changes: 2 additions & 4 deletions compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#![feature(
no_core, unboxed_closures, start, lang_items, never_type, linkage,
extern_types, thread_local
extern_types, thread_local, raw_ref_op
)]
#![no_core]
#![allow(dead_code, internal_features, non_camel_case_types)]
Expand Down Expand Up @@ -99,9 +99,7 @@ fn start<T: Termination + 'static>(

static mut NUM: u8 = 6 * 7;

// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[allow(static_mut_refs)]
static NUM_REF: &'static u8 = unsafe { &NUM };
static NUM_REF: &'static u8 = unsafe { &* &raw const NUM };

macro_rules! assert {
($e:expr) => {
Expand Down
5 changes: 2 additions & 3 deletions library/std/src/sys/thread_local/static_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ pub macro thread_local_inner {
(@key $t:ty, const $init:expr) => {{
#[inline] // see comments below
#[deny(unsafe_op_in_unsafe_fn)]
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
#[allow(static_mut_refs)]
unsafe fn __getit(
_init: $crate::option::Option<&mut $crate::option::Option<$t>>,
) -> $crate::option::Option<&'static $t> {
Expand All @@ -25,7 +23,8 @@ pub macro thread_local_inner {
// FIXME(#84224) this should come after the `target_thread_local`
// block.
static mut VAL: $t = INIT_EXPR;
unsafe { $crate::option::Option::Some(&VAL) }
// SAFETY: we only ever create shared references, so there's no mutable aliasing.
unsafe { $crate::option::Option::Some(&*$crate::ptr::addr_of!(VAL)) }
}

unsafe {
Expand Down

0 comments on commit ccd9880

Please sign in to comment.