Skip to content

Commit 09b7047

Browse files
committed
Improve wording of static_mut_ref
1 parent bc1b9e0 commit 09b7047

Some content is hidden

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

43 files changed

+202
-154
lines changed

compiler/rustc_error_codes/src/error_codes/E0796.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ unsafe {
1616
fn foo<'a>(_x: &'a i32) {}
1717
```
1818

19-
Mutable statics can be written to by multiple threads: aliasing violations or
20-
data races will cause undefined behavior.
19+
a mutable reference supposedly lives forever, so creating more than one
20+
is very dangerous and they can accidentally be used in overlapping ways.
2121

22-
Reference of mutable static is a hard error from 2024 edition.
22+
a shared reference supposedly lives forever, so if there is ever also a
23+
mutable reference created that is very dangerous as they can accidentally
24+
be used in overlapping ways.
25+
26+
Reference to mutable static is a hard error in 2024 edition.

compiler/rustc_hir_analysis/messages.ftl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,19 +373,21 @@ 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
376+
hir_analysis_static_mut_ref = reference to mutable static is disallowed
377377
.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
378+
.note = a shared reference supposedly lives forever, so if there is ever also a mutable reference created that is very dangerous as they can accidentally be used in overlapping ways
379+
.note_mut = a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
379380
.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
380381
.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
381382
382-
hir_analysis_static_mut_ref_lint = {$shared}reference of mutable static is discouraged
383+
hir_analysis_static_mut_ref_lint = {$shared}reference to mutable static is discouraged
383384
.label = shared reference of mutable static
384385
.label_mut = mutable reference of mutable static
385386
.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
386387
.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
388+
.note = reference of mutable static is a hard error in 2024 edition
389+
.why_note = a shared reference supposedly lives forever, so if there is ever also a mutable reference created that is very dangerous as they can accidentally be used in overlapping ways
390+
.why_note_mut = a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
389391
390392
hir_analysis_static_specialize = cannot specialize on `'static` lifetime
391393

compiler/rustc_hir_analysis/src/check/errs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn handle_static_mut_ref(
7171
} else {
7272
errors::StaticMutRefSugg::Shared { span, var }
7373
};
74-
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg });
74+
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, note_mut: (), sugg });
7575
return;
7676
}
7777

@@ -92,6 +92,6 @@ fn handle_static_mut_ref(
9292
STATIC_MUT_REF,
9393
hir_id,
9494
span,
95-
errors::RefOfMutStatic { shared, why_note: (), label, sugg },
95+
errors::RefOfMutStatic { shared, why_note: (), why_note_mut: (), label, sugg },
9696
);
9797
}

compiler/rustc_hir_analysis/src/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,6 +1459,8 @@ pub struct StaticMutRef {
14591459
#[primary_span]
14601460
#[label]
14611461
pub span: Span,
1462+
#[note(hir_analysis_note_mut)]
1463+
pub note_mut: (),
14621464
#[subdiagnostic]
14631465
pub sugg: StaticMutRefSugg,
14641466
}
@@ -1497,6 +1499,8 @@ pub struct RefOfMutStatic<'a> {
14971499
pub shared: &'a str,
14981500
#[note(hir_analysis_why_note)]
14991501
pub why_note: (),
1502+
#[note(hir_analysis_why_note_mut)]
1503+
pub why_note_mut: (),
15001504
#[subdiagnostic]
15011505
pub label: RefOfMutStaticLabel,
15021506
#[subdiagnostic]

compiler/rustc_lint_defs/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,7 @@ 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.
18121812
pub STATIC_MUT_REF,
18131813
Warn,

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

Lines changed: 2 additions & 2 deletions
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_ref]
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_ref]
3939
}
4040

4141
pub fn main() {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
1-
warning: shared reference of mutable static is discouraged
1+
warning: shared reference to mutable static is discouraged
22
--> $DIR/static-mut-foreign.rs:35:18
33
|
44
LL | static_bound(&rust_dbg_static_mut);
55
| ^^^^^^^^^^^^^^^^^^^^ shared reference of 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
8+
= note: reference of mutable static is a hard error in 2024 edition
9+
= note: a shared reference supposedly lives forever, so if there is ever also a mutable reference created that is very dangerous as they can accidentally be used in overlapping ways
10+
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
1011
= note: `#[warn(static_mut_ref)]` on by default
1112
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
1213
|
1314
LL | static_bound(addr_of!(rust_dbg_static_mut));
1415
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1516

16-
warning: mutable reference of mutable static is discouraged
17+
warning: mutable reference to mutable static is discouraged
1718
--> $DIR/static-mut-foreign.rs:37:22
1819
|
1920
LL | static_bound_set(&mut rust_dbg_static_mut);
2021
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference of mutable static
2122
|
2223
= 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
24+
= note: reference of mutable static is a hard error in 2024 edition
25+
= note: a shared reference supposedly lives forever, so if there is ever also a mutable reference created that is very dangerous as they can accidentally be used in overlapping ways
26+
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
2527
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
2628
|
2729
LL | static_bound_set(addr_of_mut!(rust_dbg_static_mut));

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

Lines changed: 1 addition & 1 deletion
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_ref]
2020
}
2121
}
2222

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
warning: mutable reference of mutable static is discouraged
1+
warning: mutable reference to mutable static is discouraged
22
--> $DIR/borrowck-access-permissions.rs:18:23
33
|
44
LL | let _y2 = &mut static_x_mut;
55
| ^^^^^^^^^^^^^^^^^ mutable reference of 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
8+
= note: reference of mutable static is a hard error in 2024 edition
9+
= note: a shared reference supposedly lives forever, so if there is ever also a mutable reference created that is very dangerous as they can accidentally be used in overlapping ways
10+
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
1011
= note: `#[warn(static_mut_ref)]` on by default
1112
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
1213
|

tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Foo {
1717
fn main() {
1818
unsafe {
1919
let sfoo: *mut Foo = &mut SFOO;
20-
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
20+
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
2121
let x = (*sfoo).x();
2222
(*sfoo).x[1] += 1;
2323
*x += 1;

0 commit comments

Comments
 (0)