Skip to content

Commit 0656ed6

Browse files
obeiscuviper
authored andcommitted
Improve wording of static_mut_ref
Rename `static_mut_ref` lint to `static_mut_refs`. (cherry picked from commit 408eeae)
1 parent 2c9c764 commit 0656ed6

File tree

74 files changed

+810
-487
lines changed

Some content is hidden

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

74 files changed

+810
-487
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
@@ -350,19 +350,24 @@ hir_analysis_start_not_target_feature = `#[start]` function is not allowed to ha
350350
hir_analysis_start_not_track_caller = `#[start]` function is not allowed to be `#[track_caller]`
351351
.label = `#[start]` function is not allowed to be `#[track_caller]`
352352
353-
hir_analysis_static_mut_ref = reference of mutable static is disallowed
354-
.label = reference of mutable static
355-
.note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
356-
.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
357-
.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
358-
359-
hir_analysis_static_mut_ref_lint = {$shared}reference of mutable static is discouraged
360-
.label = shared reference of mutable static
361-
.label_mut = mutable reference of mutable static
362-
.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
363-
.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
364-
.note = reference of mutable static is a hard error from 2024 edition
365-
.why_note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
353+
hir_analysis_static_mut_ref = creating a {$shared} reference to a mutable static
354+
.label = {$shared} reference to mutable static
355+
.note = {$shared ->
356+
[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
357+
*[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
358+
}
359+
.suggestion = use `addr_of!` instead to create a raw pointer
360+
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer
361+
362+
hir_analysis_static_mut_refs_lint = creating a {$shared} reference to mutable static is discouraged
363+
.label = {$shared} reference to mutable static
364+
.suggestion = use `addr_of!` instead to create a raw pointer
365+
.suggestion_mut = use `addr_of_mut!` instead to create a raw pointer
366+
.note = this will be a hard error in the 2024 edition
367+
.why_note = {$shared ->
368+
[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
369+
*[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
370+
}
366371
367372
hir_analysis_static_specialize = cannot specialize on `'static` lifetime
368373

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
@@ -1414,12 +1414,13 @@ pub struct OnlyCurrentTraitsPointerSugg<'a> {
14141414
#[derive(Diagnostic)]
14151415
#[diag(hir_analysis_static_mut_ref, code = E0796)]
14161416
#[note]
1417-
pub struct StaticMutRef {
1417+
pub struct StaticMutRef<'a> {
14181418
#[primary_span]
14191419
#[label]
14201420
pub span: Span,
14211421
#[subdiagnostic]
14221422
pub sugg: StaticMutRefSugg,
1423+
pub shared: &'a str,
14231424
}
14241425

14251426
#[derive(Subdiagnostic)]
@@ -1450,30 +1451,15 @@ pub enum StaticMutRefSugg {
14501451

14511452
// STATIC_MUT_REF lint
14521453
#[derive(LintDiagnostic)]
1453-
#[diag(hir_analysis_static_mut_ref_lint)]
1454+
#[diag(hir_analysis_static_mut_refs_lint)]
14541455
#[note]
1456+
#[note(hir_analysis_why_note)]
14551457
pub struct RefOfMutStatic<'a> {
1456-
pub shared: &'a str,
1457-
#[note(hir_analysis_why_note)]
1458-
pub why_note: (),
1459-
#[subdiagnostic]
1460-
pub label: RefOfMutStaticLabel,
1458+
#[label]
1459+
pub span: Span,
14611460
#[subdiagnostic]
14621461
pub sugg: RefOfMutStaticSugg,
1463-
}
1464-
1465-
#[derive(Subdiagnostic)]
1466-
pub enum RefOfMutStaticLabel {
1467-
#[label(hir_analysis_label)]
1468-
Shared {
1469-
#[primary_span]
1470-
span: Span,
1471-
},
1472-
#[label(hir_analysis_label_mut)]
1473-
Mut {
1474-
#[primary_span]
1475-
span: Span,
1476-
},
1462+
pub shared: &'a str,
14771463
}
14781464

14791465
#[derive(Subdiagnostic)]

compiler/rustc_lint/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ fn register_builtins(store: &mut LintStore) {
327327
store.register_renamed("or_patterns_back_compat", "rust_2021_incompatible_or_patterns");
328328
store.register_renamed("non_fmt_panic", "non_fmt_panics");
329329
store.register_renamed("unused_tuple_struct_fields", "dead_code");
330+
store.register_renamed("static_mut_ref", "static_mut_refs");
330331

331332
// These were moved to tool lints, but rustc still sees them when compiling normally, before
332333
// 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
@@ -88,7 +88,7 @@ declare_lint_pass! {
8888
SINGLE_USE_LIFETIMES,
8989
SOFT_UNSTABLE,
9090
STABLE_FEATURES,
91-
STATIC_MUT_REF,
91+
STATIC_MUT_REFS,
9292
SUSPICIOUS_AUTO_TRAIT_IMPLS,
9393
TEST_UNSTABLE_LINT,
9494
TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
@@ -1768,7 +1768,7 @@ declare_lint! {
17681768
}
17691769

17701770
declare_lint! {
1771-
/// The `static_mut_ref` lint checks for shared or mutable references
1771+
/// The `static_mut_refs` lint checks for shared or mutable references
17721772
/// of mutable static inside `unsafe` blocks and `unsafe` functions.
17731773
///
17741774
/// ### Example
@@ -1806,9 +1806,9 @@ declare_lint! {
18061806
/// Shared or mutable references of mutable static are almost always a mistake and
18071807
/// can lead to undefined behavior and various other problems in your code.
18081808
///
1809-
/// This lint is "warn" by default on editions up to 2021, from 2024 there is
1809+
/// This lint is "warn" by default on editions up to 2021, in 2024 there is
18101810
/// a hard error instead.
1811-
pub STATIC_MUT_REF,
1811+
pub STATIC_MUT_REFS,
18121812
Warn,
18131813
"shared references or mutable references of mutable static is discouraged",
18141814
@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-
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
264+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
265+
#[cfg_attr(not(bootstrap), 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-
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
327+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
328+
#[cfg_attr(not(bootstrap), 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

+2-2
Original file line numberDiff line numberDiff line change
@@ -337,8 +337,8 @@ 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-
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
340+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
341+
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
342342
pub mod panic_count {
343343
use crate::cell::Cell;
344344
use crate::sync::atomic::{AtomicUsize, Ordering};

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ 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-
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
16+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
17+
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
1818
unsafe fn __getit(
1919
_init: $crate::option::Option<&mut $crate::option::Option<$t>>,
2020
) -> $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-
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
14+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint
15+
#[cfg_attr(not(bootstrap), 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-
#[cfg_attr(not(bootstrap), 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)