Skip to content

Commit dd05776

Browse files
committed
Improve wording of static_mut_ref
Rename `static_mut_ref` lint to `static_mut_refs`
1 parent bc1b9e0 commit dd05776

File tree

69 files changed

+579
-574
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+579
-574
lines changed

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ fn start<T: Termination + 'static>(
112112

113113
static mut NUM: u8 = 6 * 7;
114114

115-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
116-
#[allow(static_mut_ref)]
115+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
116+
#[allow(static_mut_refs)]
117117
static NUM_REF: &'static u8 = unsafe { &NUM };
118118

119119
unsafe fn zeroed<T>() -> T {

compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ fn start<T: Termination + 'static>(
9999

100100
static mut NUM: u8 = 6 * 7;
101101

102-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
103-
#[allow(static_mut_ref)]
102+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
103+
#[allow(static_mut_refs)]
104104
static NUM_REF: &'static u8 = unsafe { &NUM };
105105

106106
macro_rules! assert {
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
Reference of mutable static.
1+
You have created a reference to a mutable static.
22

33
Erroneous code example:
44

55
```compile_fail,edition2024,E0796
66
static mut X: i32 = 23;
7-
static mut Y: i32 = 24;
87
9-
unsafe {
10-
let y = &X;
11-
let ref x = X;
12-
let (x, y) = (&X, &Y);
13-
foo(&X);
8+
fn work() {
9+
let _val = unsafe { X };
1410
}
1511
16-
fn foo<'a>(_x: &'a i32) {}
12+
let x_ref = unsafe { &mut X };
13+
work();
14+
// The next line has Undefined Behavior!
15+
// `x_ref` is a mutable reference and allows no aliases,
16+
// but `work` has been reading the reference between
17+
// the moment `x_ref` was created and when it was used.
18+
// This violates the uniqueness of `x_ref`.
19+
*x_ref = 42;
1720
```
1821

19-
Mutable statics can be written to by multiple threads: aliasing violations or
20-
data races will cause undefined behavior.
22+
A reference to a mutable static has lifetime `'static`. This is very dangerous
23+
as it is easy to accidentally overlap the lifetime of that reference with
24+
other, conflicting accesses to the same static.
2125

22-
Reference of mutable static is a hard error from 2024 edition.
26+
References to mutable statics are a hard error in the 2024 edition.

compiler/rustc_hir_analysis/messages.ftl

+18-13
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,24 @@ hir_analysis_start_not_target_feature = `#[start]` function is not allowed to ha
373373
hir_analysis_start_not_track_caller = `#[start]` function is not allowed to be `#[track_caller]`
374374
.label = `#[start]` function is not allowed to be `#[track_caller]`
375375
376-
hir_analysis_static_mut_ref = reference of mutable static is disallowed
377-
.label = reference of mutable static
378-
.note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
379-
.suggestion = shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
380-
.suggestion_mut = mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
381-
382-
hir_analysis_static_mut_ref_lint = {$shared}reference of mutable static is discouraged
383-
.label = shared reference of mutable static
384-
.label_mut = mutable reference of mutable static
385-
.suggestion = shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
386-
.suggestion_mut = mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
387-
.note = reference of mutable static is a hard error from 2024 edition
388-
.why_note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
376+
hir_analysis_static_mut_ref = creating a {$shared} reference to a mutable static
377+
.label = {$shared}reference to mutable static
378+
.note = {$shared ->
379+
[shared] this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior
380+
*[mutable] this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
381+
}
382+
.suggestion = use `addr_of!` instead to create a raw pointer
383+
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer
384+
385+
hir_analysis_static_mut_refs_lint = creating a {$shared} reference to mutable static is discouraged
386+
.label = {$shared} reference to mutable static
387+
.suggestion = use `addr_of!` instead to create a raw pointer
388+
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer
389+
.note = this will be a hard error in the 2024 edition
390+
.why_note = {$shared ->
391+
[shared] this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior
392+
*[mutable] this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
393+
}
389394
390395
hir_analysis_static_specialize = cannot specialize on `'static` lifetime
391396

compiler/rustc_hir_analysis/src/check/errs.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_hir as hir;
22
use rustc_hir_pretty::qpath_to_string;
3-
use rustc_lint_defs::builtin::STATIC_MUT_REF;
3+
use rustc_lint_defs::builtin::STATIC_MUT_REFS;
44
use rustc_middle::ty::TyCtxt;
55
use rustc_span::Span;
66
use rustc_type_ir::Mutability;
@@ -66,32 +66,24 @@ fn handle_static_mut_ref(
6666
hir_id: hir::HirId,
6767
) {
6868
if e2024 {
69-
let sugg = if mutable {
70-
errors::StaticMutRefSugg::Mut { span, var }
69+
let (sugg, shared) = if mutable {
70+
(errors::StaticMutRefSugg::Mut { span, var }, "mutable")
7171
} else {
72-
errors::StaticMutRefSugg::Shared { span, var }
72+
(errors::StaticMutRefSugg::Shared { span, var }, "shared")
7373
};
74-
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg });
74+
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg, shared });
7575
return;
7676
}
7777

78-
let (label, sugg, shared) = if mutable {
79-
(
80-
errors::RefOfMutStaticLabel::Mut { span },
81-
errors::RefOfMutStaticSugg::Mut { span, var },
82-
"mutable ",
83-
)
78+
let (sugg, shared) = if mutable {
79+
(errors::RefOfMutStaticSugg::Mut { span, var }, "mutable")
8480
} else {
85-
(
86-
errors::RefOfMutStaticLabel::Shared { span },
87-
errors::RefOfMutStaticSugg::Shared { span, var },
88-
"shared ",
89-
)
81+
(errors::RefOfMutStaticSugg::Shared { span, var }, "shared")
9082
};
9183
tcx.emit_node_span_lint(
92-
STATIC_MUT_REF,
84+
STATIC_MUT_REFS,
9385
hir_id,
9486
span,
95-
errors::RefOfMutStatic { shared, why_note: (), label, sugg },
87+
errors::RefOfMutStatic { span, sugg, shared },
9688
);
9789
}

compiler/rustc_hir_analysis/src/errors.rs

+7-21
Original file line numberDiff line numberDiff line change
@@ -1455,12 +1455,13 @@ pub struct OnlyCurrentTraitsPointerSugg<'a> {
14551455
#[derive(Diagnostic)]
14561456
#[diag(hir_analysis_static_mut_ref, code = E0796)]
14571457
#[note]
1458-
pub struct StaticMutRef {
1458+
pub struct StaticMutRef<'a> {
14591459
#[primary_span]
14601460
#[label]
14611461
pub span: Span,
14621462
#[subdiagnostic]
14631463
pub sugg: StaticMutRefSugg,
1464+
pub shared: &'a str,
14641465
}
14651466

14661467
#[derive(Subdiagnostic)]
@@ -1491,30 +1492,15 @@ pub enum StaticMutRefSugg {
14911492

14921493
// STATIC_MUT_REF lint
14931494
#[derive(LintDiagnostic)]
1494-
#[diag(hir_analysis_static_mut_ref_lint)]
1495+
#[diag(hir_analysis_static_mut_refs_lint)]
14951496
#[note]
1497+
#[note(hir_analysis_why_note)]
14961498
pub struct RefOfMutStatic<'a> {
1497-
pub shared: &'a str,
1498-
#[note(hir_analysis_why_note)]
1499-
pub why_note: (),
1500-
#[subdiagnostic]
1501-
pub label: RefOfMutStaticLabel,
1499+
#[label]
1500+
pub span: Span,
15021501
#[subdiagnostic]
15031502
pub sugg: RefOfMutStaticSugg,
1504-
}
1505-
1506-
#[derive(Subdiagnostic)]
1507-
pub enum RefOfMutStaticLabel {
1508-
#[label(hir_analysis_label)]
1509-
Shared {
1510-
#[primary_span]
1511-
span: Span,
1512-
},
1513-
#[label(hir_analysis_label_mut)]
1514-
Mut {
1515-
#[primary_span]
1516-
span: Span,
1517-
},
1503+
pub shared: &'a str,
15181504
}
15191505

15201506
#[derive(Subdiagnostic)]

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ fn register_builtins(store: &mut LintStore) {
324324
store.register_renamed("or_patterns_back_compat", "rust_2021_incompatible_or_patterns");
325325
store.register_renamed("non_fmt_panic", "non_fmt_panics");
326326
store.register_renamed("unused_tuple_struct_fields", "dead_code");
327+
store.register_renamed("static_mut_ref", "static_mut_refs");
327328

328329
// These were moved to tool lints, but rustc still sees them when compiling normally, before
329330
// tool lints are registered, so `check_tool_name_for_backwards_compat` doesn't work. Use

compiler/rustc_lint_defs/src/builtin.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ declare_lint_pass! {
8989
SINGLE_USE_LIFETIMES,
9090
SOFT_UNSTABLE,
9191
STABLE_FEATURES,
92-
STATIC_MUT_REF,
92+
STATIC_MUT_REFS,
9393
SUSPICIOUS_AUTO_TRAIT_IMPLS,
9494
TEST_UNSTABLE_LINT,
9595
TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
@@ -1769,7 +1769,7 @@ declare_lint! {
17691769
}
17701770

17711771
declare_lint! {
1772-
/// The `static_mut_ref` lint checks for shared or mutable references
1772+
/// The `static_mut_refs` lint checks for shared or mutable references
17731773
/// of mutable static inside `unsafe` blocks and `unsafe` functions.
17741774
///
17751775
/// ### Example
@@ -1807,9 +1807,9 @@ declare_lint! {
18071807
/// Shared or mutable references of mutable static are almost always a mistake and
18081808
/// can lead to undefined behavior and various other problems in your code.
18091809
///
1810-
/// This lint is "warn" by default on editions up to 2021, from 2024 there is
1810+
/// This lint is "warn" by default on editions up to 2021, in 2024 there is
18111811
/// a hard error instead.
1812-
pub STATIC_MUT_REF,
1812+
pub STATIC_MUT_REFS,
18131813
Warn,
18141814
"shared references or mutable references of mutable static is discouraged",
18151815
@future_incompatible = FutureIncompatibleInfo {

library/panic_unwind/src/seh.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ cfg_if::cfg_if! {
261261
}
262262
}
263263

264-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
265-
#[allow(static_mut_ref)]
264+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
265+
#[allow(static_mut_refs)]
266266
pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
267267
use core::intrinsics::atomic_store_seqcst;
268268

@@ -324,8 +324,8 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
324324
_CxxThrowException(throw_ptr, &mut THROW_INFO as *mut _ as *mut _);
325325
}
326326

327-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
328-
#[allow(static_mut_ref)]
327+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
328+
#[allow(static_mut_refs)]
329329
pub unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send> {
330330
// A null payload here means that we got here from the catch (...) of
331331
// __rust_try. This happens when a non-Rust foreign exception is caught.

library/std/src/panicking.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,9 @@ pub mod panic_count {
337337
#[doc(hidden)]
338338
#[cfg(not(feature = "panic_immediate_abort"))]
339339
#[unstable(feature = "update_panic_count", issue = "none")]
340-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
341-
#[allow(static_mut_ref)]
340+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
341+
#[cfg_attr(bootstrap, allow(static_mut_ref))]
342+
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
342343
pub mod panic_count {
343344
use crate::cell::Cell;
344345
use crate::sync::atomic::{AtomicUsize, Ordering};

library/std/src/sys/pal/common/thread_local/fast_local.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ pub macro thread_local_inner {
1313
(@key $t:ty, const $init:expr) => {{
1414
#[inline]
1515
#[deny(unsafe_op_in_unsafe_fn)]
16-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
17-
#[allow(static_mut_ref)]
16+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
17+
#[cfg_attr(bootstrap, allow(static_mut_ref))]
18+
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
1819
unsafe fn __getit(
1920
_init: $crate::option::Option<&mut $crate::option::Option<$t>>,
2021
) -> $crate::option::Option<&'static $t> {

library/std/src/sys/pal/common/thread_local/static_local.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ pub macro thread_local_inner {
1111
(@key $t:ty, const $init:expr) => {{
1212
#[inline] // see comments below
1313
#[deny(unsafe_op_in_unsafe_fn)]
14-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
15-
#[allow(static_mut_ref)]
14+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
15+
#[allow(static_mut_refs)]
1616
unsafe fn __getit(
1717
_init: $crate::option::Option<&mut $crate::option::Option<$t>>,
1818
) -> $crate::option::Option<&'static $t> {

library/std/src/thread/local.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
180180
#[stable(feature = "rust1", since = "1.0.0")]
181181
#[cfg_attr(not(test), rustc_diagnostic_item = "thread_local_macro")]
182182
#[allow_internal_unstable(thread_local_internals)]
183-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
184-
#[allow(static_mut_ref)]
183+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
184+
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
185185
macro_rules! thread_local {
186186
// empty (base case for the recursion)
187187
() => {};

src/tools/miri/tests/fail/tls/tls_static_dealloc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! Ensure that thread-local statics get deallocated when the thread dies.
22
33
#![feature(thread_local)]
4-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
5-
#![allow(static_mut_ref)]
4+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
5+
#![allow(static_mut_refs)]
66

77
#[thread_local]
88
static mut TLS: u8 = 0;

src/tools/miri/tests/pass/static_mut.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
static mut FOO: i32 = 42;
22

3-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
4-
#[allow(static_mut_ref)]
3+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
4+
#[allow(static_mut_refs)]
55
static BAR: Foo = Foo(unsafe { &FOO as *const _ });
66

77
#[allow(dead_code)]

src/tools/miri/tests/pass/tls/tls_static.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
//! test, we also check that thread-locals act as per-thread statics.
99
1010
#![feature(thread_local)]
11-
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
12-
#![allow(static_mut_ref)]
11+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
12+
#![allow(static_mut_refs)]
1313

1414
use std::thread;
1515

tests/ui/abi/statics/static-mut-foreign.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ unsafe fn run() {
3333
rust_dbg_static_mut = -3;
3434
assert_eq!(rust_dbg_static_mut, -3);
3535
static_bound(&rust_dbg_static_mut);
36-
//~^ WARN shared reference of mutable static is discouraged [static_mut_ref]
36+
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
3737
static_bound_set(&mut rust_dbg_static_mut);
38-
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
38+
//~^ WARN mutable reference to mutable static is discouraged [static_mut_refs]
3939
}
4040

4141
pub fn main() {

tests/ui/abi/statics/static-mut-foreign.stderr

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
warning: shared reference of mutable static is discouraged
1+
warning: creating a shared reference to mutable static is discouraged
22
--> $DIR/static-mut-foreign.rs:35:18
33
|
44
LL | static_bound(&rust_dbg_static_mut);
5-
| ^^^^^^^^^^^^^^^^^^^^ shared reference of mutable static
5+
| ^^^^^^^^^^^^^^^^^^^^ shared reference to mutable static
66
|
77
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
8-
= note: reference of mutable static is a hard error from 2024 edition
9-
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
10-
= note: `#[warn(static_mut_ref)]` on by default
11-
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
8+
= note: this will be a hard error in the 2024 edition
9+
= note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior
10+
= note: `#[warn(static_mut_refs)]` on by default
11+
help: use `addr_of!` instead to create a raw pointer
1212
|
1313
LL | static_bound(addr_of!(rust_dbg_static_mut));
1414
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1515

16-
warning: mutable reference of mutable static is discouraged
16+
warning: creating a mutable reference to mutable static is discouraged
1717
--> $DIR/static-mut-foreign.rs:37:22
1818
|
1919
LL | static_bound_set(&mut rust_dbg_static_mut);
20-
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference of mutable static
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static
2121
|
2222
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
23-
= note: reference of mutable static is a hard error from 2024 edition
24-
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
25-
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
23+
= note: this will be a hard error in the 2024 edition
24+
= note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior
25+
help: use `addr_of_mut!` instead to create a raw pointer
2626
|
2727
LL | static_bound_set(addr_of_mut!(rust_dbg_static_mut));
2828
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

tests/ui/borrowck/borrowck-access-permissions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616
let _y1 = &mut static_x; //~ ERROR [E0596]
1717
unsafe {
1818
let _y2 = &mut static_x_mut;
19-
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
19+
//~^ WARN mutable reference to mutable static is discouraged [static_mut_refs]
2020
}
2121
}
2222

0 commit comments

Comments
 (0)