Skip to content

Commit c15b018

Browse files
committed
Improve wording of static_mut_ref
1 parent bc1b9e0 commit c15b018

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

+7-3
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

+7-5
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

+2-2
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

+4
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

+1-1
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

+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_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

+8-6
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

+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_ref]
2020
}
2121
}
2222

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

+4-3
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

+1-1
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;

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

+4-3
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-unsafe-static-mutable-borrows.rs:19:30
33
|
44
LL | let sfoo: *mut Foo = &mut SFOO;
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/issue-20801.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn imm_ref() -> &'static T {
1212

1313
fn mut_ref() -> &'static mut T {
1414
unsafe { &mut GLOBAL_MUT_T }
15-
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
15+
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
1616
}
1717

1818
fn mut_ptr() -> *mut T {

tests/ui/borrowck/issue-20801.stderr

+4-3
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/issue-20801.rs:14:14
33
|
44
LL | unsafe { &mut GLOBAL_MUT_T }
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/issue-55492-borrowck-migrate-scans-parents.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod borrowck_closures_unique {
1010
//~^ ERROR is not declared as mutable
1111
unsafe {
1212
c1(&mut Y);
13-
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
13+
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
1414
}
1515
}
1616
}
@@ -25,7 +25,7 @@ mod borrowck_closures_unique_grandparent {
2525
};
2626
unsafe {
2727
c1(&mut Z);
28-
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
28+
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
2929
}
3030
}
3131
}
@@ -62,7 +62,7 @@ fn main() {
6262
static mut X: isize = 2;
6363
unsafe {
6464
borrowck_closures_unique::e(&mut X);
65-
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
65+
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
6666
}
6767

6868
mutability_errors::capture_assign_whole((1000,));

tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr

+12-9
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,44 @@
1-
warning: mutable reference of mutable static is discouraged
1+
warning: mutable reference to mutable static is discouraged
22
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:12:16
33
|
44
LL | c1(&mut Y);
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
|
1314
LL | c1(addr_of_mut!(Y));
1415
| ~~~~~~~~~~~~~~~
1516

16-
warning: mutable reference of mutable static is discouraged
17+
warning: mutable reference to mutable static is discouraged
1718
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:27:16
1819
|
1920
LL | c1(&mut Z);
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 | c1(addr_of_mut!(Z));
2830
| ~~~~~~~~~~~~~~~
2931

30-
warning: mutable reference of mutable static is discouraged
32+
warning: mutable reference to mutable static is discouraged
3133
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:64:37
3234
|
3335
LL | borrowck_closures_unique::e(&mut X);
3436
| ^^^^^^ mutable reference of mutable static
3537
|
3638
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
37-
= note: reference of mutable static is a hard error from 2024 edition
38-
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
39+
= note: reference of mutable static is a hard error in 2024 edition
40+
= 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
41+
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
3942
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
4043
|
4144
LL | borrowck_closures_unique::e(addr_of_mut!(X));

tests/ui/consts/const_let_assign2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static mut BB: AA = AA::new();
1616

1717
fn main() {
1818
let ptr = unsafe { &mut BB };
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
for a in ptr.data.iter() {
2121
println!("{}", a);
2222
}

tests/ui/consts/const_let_assign2.stderr

+4-3
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/const_let_assign2.rs:18:24
33
|
44
LL | let ptr = unsafe { &mut BB };
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/consts/const_refs_to_static_fail_invalid.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ fn mutable() {
4343
// This *must not build*, the constant we are matching against
4444
// could change its value!
4545
match &42 {
46-
C => {}, //~ERROR: could not evaluate constant pattern
47-
_ => {},
46+
C => {} //~ERROR: could not evaluate constant pattern
47+
_ => {}
4848
}
4949
}
5050

tests/ui/consts/const_refs_to_static_fail_invalid.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ LL | const C: &i32 = unsafe { &S_MUT };
4646
error: could not evaluate constant pattern
4747
--> $DIR/const_refs_to_static_fail_invalid.rs:46:9
4848
|
49-
LL | C => {},
49+
LL | C => {}
5050
| ^
5151

5252
error: aborting due to 6 previous errors

0 commit comments

Comments
 (0)