Skip to content

Commit

Permalink
Improve wording of static_mut_ref
Browse files Browse the repository at this point in the history
  • Loading branch information
obeis committed Feb 13, 2024
1 parent bc1b9e0 commit 09b7047
Show file tree
Hide file tree
Showing 43 changed files with 202 additions and 154 deletions.
10 changes: 7 additions & 3 deletions compiler/rustc_error_codes/src/error_codes/E0796.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ unsafe {
fn foo<'a>(_x: &'a i32) {}
```

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

Reference of mutable static is a hard error from 2024 edition.
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.

Reference to mutable static is a hard error in 2024 edition.
12 changes: 7 additions & 5 deletions compiler/rustc_hir_analysis/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,21 @@ hir_analysis_start_not_target_feature = `#[start]` function is not allowed to ha
hir_analysis_start_not_track_caller = `#[start]` function is not allowed to be `#[track_caller]`
.label = `#[start]` function is not allowed to be `#[track_caller]`
hir_analysis_static_mut_ref = reference of mutable static is disallowed
hir_analysis_static_mut_ref = reference to mutable static is disallowed
.label = reference of mutable static
.note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
.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
.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
.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
.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
hir_analysis_static_mut_ref_lint = {$shared}reference of mutable static is discouraged
hir_analysis_static_mut_ref_lint = {$shared}reference to mutable static is discouraged
.label = shared reference of mutable static
.label_mut = mutable reference of mutable static
.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
.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
.note = reference of mutable static is a hard error from 2024 edition
.why_note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
.note = reference of mutable static is a hard error in 2024 edition
.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
.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
hir_analysis_static_specialize = cannot specialize on `'static` lifetime
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/check/errs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn handle_static_mut_ref(
} else {
errors::StaticMutRefSugg::Shared { span, var }
};
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg });
tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, note_mut: (), sugg });
return;
}

Expand All @@ -92,6 +92,6 @@ fn handle_static_mut_ref(
STATIC_MUT_REF,
hir_id,
span,
errors::RefOfMutStatic { shared, why_note: (), label, sugg },
errors::RefOfMutStatic { shared, why_note: (), why_note_mut: (), label, sugg },
);
}
4 changes: 4 additions & 0 deletions compiler/rustc_hir_analysis/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1459,6 +1459,8 @@ pub struct StaticMutRef {
#[primary_span]
#[label]
pub span: Span,
#[note(hir_analysis_note_mut)]
pub note_mut: (),
#[subdiagnostic]
pub sugg: StaticMutRefSugg,
}
Expand Down Expand Up @@ -1497,6 +1499,8 @@ pub struct RefOfMutStatic<'a> {
pub shared: &'a str,
#[note(hir_analysis_why_note)]
pub why_note: (),
#[note(hir_analysis_why_note_mut)]
pub why_note_mut: (),
#[subdiagnostic]
pub label: RefOfMutStaticLabel,
#[subdiagnostic]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ declare_lint! {
/// Shared or mutable references of mutable static are almost always a mistake and
/// can lead to undefined behavior and various other problems in your code.
///
/// This lint is "warn" by default on editions up to 2021, from 2024 there is
/// This lint is "warn" by default on editions up to 2021,in 2024 there is
/// a hard error instead.
pub STATIC_MUT_REF,
Warn,
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/abi/statics/static-mut-foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ unsafe fn run() {
rust_dbg_static_mut = -3;
assert_eq!(rust_dbg_static_mut, -3);
static_bound(&rust_dbg_static_mut);
//~^ WARN shared reference of mutable static is discouraged [static_mut_ref]
//~^ WARN shared reference to mutable static is discouraged [static_mut_ref]
static_bound_set(&mut rust_dbg_static_mut);
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
}

pub fn main() {
Expand Down
14 changes: 8 additions & 6 deletions tests/ui/abi/statics/static-mut-foreign.stderr
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
warning: shared reference of mutable static is discouraged
warning: shared reference to mutable static is discouraged
--> $DIR/static-mut-foreign.rs:35:18
|
LL | static_bound(&rust_dbg_static_mut);
| ^^^^^^^^^^^^^^^^^^^^ shared reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
= note: `#[warn(static_mut_ref)]` on by default
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
|
LL | static_bound(addr_of!(rust_dbg_static_mut));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

warning: mutable reference of mutable static is discouraged
warning: mutable reference to mutable static is discouraged
--> $DIR/static-mut-foreign.rs:37:22
|
LL | static_bound_set(&mut rust_dbg_static_mut);
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
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
|
LL | static_bound_set(addr_of_mut!(rust_dbg_static_mut));
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/borrowck-access-permissions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
let _y1 = &mut static_x; //~ ERROR [E0596]
unsafe {
let _y2 = &mut static_x_mut;
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
}
}

Expand Down
7 changes: 4 additions & 3 deletions tests/ui/borrowck/borrowck-access-permissions.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
warning: mutable reference of mutable static is discouraged
warning: mutable reference to mutable static is discouraged
--> $DIR/borrowck-access-permissions.rs:18:23
|
LL | let _y2 = &mut static_x_mut;
| ^^^^^^^^^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
= note: `#[warn(static_mut_ref)]` on by default
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
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Foo {
fn main() {
unsafe {
let sfoo: *mut Foo = &mut SFOO;
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
let x = (*sfoo).x();
(*sfoo).x[1] += 1;
*x += 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
warning: mutable reference of mutable static is discouraged
warning: mutable reference to mutable static is discouraged
--> $DIR/borrowck-unsafe-static-mutable-borrows.rs:19:30
|
LL | let sfoo: *mut Foo = &mut SFOO;
| ^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
= note: `#[warn(static_mut_ref)]` on by default
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
|
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/borrowck/issue-20801.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn imm_ref() -> &'static T {

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

fn mut_ptr() -> *mut T {
Expand Down
7 changes: 4 additions & 3 deletions tests/ui/borrowck/issue-20801.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
warning: mutable reference of mutable static is discouraged
warning: mutable reference to mutable static is discouraged
--> $DIR/issue-20801.rs:14:14
|
LL | unsafe { &mut GLOBAL_MUT_T }
| ^^^^^^^^^^^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
= note: `#[warn(static_mut_ref)]` on by default
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
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod borrowck_closures_unique {
//~^ ERROR is not declared as mutable
unsafe {
c1(&mut Y);
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
}
}
}
Expand All @@ -25,7 +25,7 @@ mod borrowck_closures_unique_grandparent {
};
unsafe {
c1(&mut Z);
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
}
}
}
Expand Down Expand Up @@ -62,7 +62,7 @@ fn main() {
static mut X: isize = 2;
unsafe {
borrowck_closures_unique::e(&mut X);
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
}

mutability_errors::capture_assign_whole((1000,));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,44 @@
warning: mutable reference of mutable static is discouraged
warning: mutable reference to mutable static is discouraged
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:12:16
|
LL | c1(&mut Y);
| ^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
= note: `#[warn(static_mut_ref)]` on by default
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
|
LL | c1(addr_of_mut!(Y));
| ~~~~~~~~~~~~~~~

warning: mutable reference of mutable static is discouraged
warning: mutable reference to mutable static is discouraged
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:27:16
|
LL | c1(&mut Z);
| ^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
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
|
LL | c1(addr_of_mut!(Z));
| ~~~~~~~~~~~~~~~

warning: mutable reference of mutable static is discouraged
warning: mutable reference to mutable static is discouraged
--> $DIR/issue-55492-borrowck-migrate-scans-parents.rs:64:37
|
LL | borrowck_closures_unique::e(&mut X);
| ^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
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
|
LL | borrowck_closures_unique::e(addr_of_mut!(X));
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const_let_assign2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static mut BB: AA = AA::new();

fn main() {
let ptr = unsafe { &mut BB };
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
//~^ WARN mutable reference to mutable static is discouraged [static_mut_ref]
for a in ptr.data.iter() {
println!("{}", a);
}
Expand Down
7 changes: 4 additions & 3 deletions tests/ui/consts/const_let_assign2.stderr
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
warning: mutable reference of mutable static is discouraged
warning: mutable reference to mutable static is discouraged
--> $DIR/const_let_assign2.rs:18:24
|
LL | let ptr = unsafe { &mut BB };
| ^^^^^^^ mutable reference of mutable static
|
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
= note: reference of mutable static is a hard error from 2024 edition
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
= note: reference of mutable static is a hard error in 2024 edition
= 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
= note: a mutable reference supposedly lives forever, so creating more than one is very dangerous and they can accidentally be used in overlapping ways
= note: `#[warn(static_mut_ref)]` on by default
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
|
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/consts/const_refs_to_static_fail_invalid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ fn mutable() {
// This *must not build*, the constant we are matching against
// could change its value!
match &42 {
C => {}, //~ERROR: could not evaluate constant pattern
_ => {},
C => {} //~ERROR: could not evaluate constant pattern
_ => {}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const_refs_to_static_fail_invalid.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ LL | const C: &i32 = unsafe { &S_MUT };
error: could not evaluate constant pattern
--> $DIR/const_refs_to_static_fail_invalid.rs:46:9
|
LL | C => {},
LL | C => {}
| ^

error: aborting due to 6 previous errors
Expand Down
Loading

0 comments on commit 09b7047

Please sign in to comment.