mirror of
https://github.com/rust-lang/rust.git
synced 2025-11-17 10:05:53 +00:00
The implied deref to statics introduced by HIR->THIR lowering is only
used to create place expressions, it lacks unsafe semantics.
It is also confusing, as there is no visible `*ident` in the source.
For both classes of "unsafe static" (extern static and static mut)
allow this operation.
We lack a clear story around `thread_local! { static mut }`, which
is actually its own category of item that reuses the static syntax but
has its own rules. It's possible they should be similarly included, but
in the absence of a good reason one way or another, we do not bless it.
20 lines
415 B
Rust
20 lines
415 B
Rust
//@ run-pass
|
|
#![feature(const_refs_to_static)]
|
|
|
|
static S: i32 = 0;
|
|
static mut S_MUT: i32 = 0;
|
|
|
|
const C1: &i32 = &S;
|
|
#[allow(unused)]
|
|
const C1_READ: () = {
|
|
assert!(*C1 == 0);
|
|
};
|
|
const C2: *const i32 = std::ptr::addr_of!(S_MUT);
|
|
|
|
fn main() {
|
|
assert_eq!(*C1, 0);
|
|
assert_eq!(unsafe { *C2 }, 0);
|
|
// Computing this pattern will read from an immutable static. That's fine.
|
|
assert!(matches!(&0, C1));
|
|
}
|