Skip to content

Fix a wrong error message in 2024 edition #140056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ lint_single_use_lifetime = lifetime parameter `{$ident}` only used once

lint_span_use_eq_ctxt = use `.eq_ctxt()` instead of `.ctxt() == .ctxt()`

lint_static_mut_refs_lint = creating a {$shared_label}reference to mutable static is discouraged
lint_static_mut_refs_lint = creating a {$shared_label}reference to mutable static
.label = {$shared_label}reference to mutable static
.suggestion = use `&raw const` instead to create a raw pointer
.suggestion_mut = use `&raw mut` instead to create a raw pointer
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/static_mut_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ declare_lint! {
/// This lint is "warn" by default on editions up to 2021, in 2024 is "deny".
pub STATIC_MUT_REFS,
Warn,
"shared references or mutable references of mutable static is discouraged",
"creating a shared reference to mutable static",
@future_incompatible = FutureIncompatibleInfo {
reason: FutureIncompatibilityReason::EditionError(Edition::Edition2024),
reference: "<https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ LL | if result.is_ok() {
LL | result.as_mut().unwrap();
| ^^^^^^^^^^^^^^^^^^^^^^^^

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> tests/ui/checked_unwrap/simple_conditionals.rs:183:12
|
LL | if X.is_some() {
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 to mutable static is discouraged [static_mut_refs]
//~^ WARN mutable reference to mutable static [static_mut_refs]
let x = (*sfoo).x();
(*sfoo).x[1] += 1;
*x += 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: creating a mutable reference to mutable static is discouraged
warning: creating a mutable reference to mutable static
--> $DIR/borrowck-unsafe-static-mutable-borrows.rs:19:30
|
LL | let sfoo: *mut Foo = &mut SFOO;
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 to mutable static is discouraged [static_mut_refs]
//~^ WARN mutable reference to mutable static [static_mut_refs]
for a in ptr.data.iter() {
println!("{}", a);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/consts/const_let_assign2.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: creating a mutable reference to mutable static is discouraged
warning: creating a mutable reference to mutable static
--> $DIR/const_let_assign2.rs:18:24
|
LL | let ptr = unsafe { &mut BB };
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-39367.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn arena() -> &'static ArenaSet<Vec<u8>> {

static mut ONCE: Once = Once::new();
ONCE.call_once(|| {
//~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
//~^ WARN creating a shared reference to mutable static [static_mut_refs]
DATA = transmute
::<Box<ArenaSet<Vec<u8>>>, *const ArenaSet<Vec<u8>>>
(Box::new(__static_ref_initialize()));
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/issues/issue-39367.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/issue-39367.rs:22:13
|
LL | / ONCE.call_once(|| {
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/lint/static-mut-refs.e2021.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:38:18
|
LL | let _y = &X;
Expand All @@ -12,7 +12,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let _y = &raw const X;
| +++++++++

warning: creating a mutable reference to mutable static is discouraged
warning: creating a mutable reference to mutable static
--> $DIR/static-mut-refs.rs:42:18
|
LL | let _y = &mut X;
Expand All @@ -25,7 +25,7 @@ help: use `&raw mut` instead to create a raw pointer
LL | let _y = &raw mut X;
| +++

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:50:22
|
LL | let ref _a = X;
Expand All @@ -34,7 +34,7 @@ LL | let ref _a = X;
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:54:25
|
LL | let (_b, _c) = (&X, &Y);
Expand All @@ -47,7 +47,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let (_b, _c) = (&raw const X, &Y);
| +++++++++

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:54:29
|
LL | let (_b, _c) = (&X, &Y);
Expand All @@ -60,7 +60,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let (_b, _c) = (&X, &raw const Y);
| +++++++++

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:60:13
|
LL | foo(&X);
Expand All @@ -73,7 +73,7 @@ help: use `&raw const` instead to create a raw pointer
LL | foo(&raw const X);
| +++++++++

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:66:17
|
LL | let _ = Z.len();
Expand All @@ -82,7 +82,7 @@ LL | let _ = Z.len();
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:72:33
|
LL | let _ = format!("{:?}", Z);
Expand All @@ -91,7 +91,7 @@ LL | let _ = format!("{:?}", Z);
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:76:18
|
LL | let _v = &A.value;
Expand All @@ -104,7 +104,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let _v = &raw const A.value;
| +++++++++

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:80:18
|
LL | let _s = &A.s.value;
Expand All @@ -117,7 +117,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let _s = &raw const A.s.value;
| +++++++++

warning: creating a shared reference to mutable static is discouraged
warning: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:84:22
|
LL | let ref _v = A.value;
Expand All @@ -126,7 +126,7 @@ LL | let ref _v = A.value;
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

warning: creating a mutable reference to mutable static is discouraged
warning: creating a mutable reference to mutable static
--> $DIR/static-mut-refs.rs:14:14
|
LL | &mut ($x.0)
Expand Down
24 changes: 12 additions & 12 deletions tests/ui/lint/static-mut-refs.e2024.stderr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:38:18
|
LL | let _y = &X;
Expand All @@ -12,7 +12,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let _y = &raw const X;
| +++++++++

error: creating a mutable reference to mutable static is discouraged
error: creating a mutable reference to mutable static
--> $DIR/static-mut-refs.rs:42:18
|
LL | let _y = &mut X;
Expand All @@ -25,7 +25,7 @@ help: use `&raw mut` instead to create a raw pointer
LL | let _y = &raw mut X;
| +++

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:50:22
|
LL | let ref _a = X;
Expand All @@ -34,7 +34,7 @@ LL | let ref _a = X;
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:54:25
|
LL | let (_b, _c) = (&X, &Y);
Expand All @@ -47,7 +47,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let (_b, _c) = (&raw const X, &Y);
| +++++++++

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:54:29
|
LL | let (_b, _c) = (&X, &Y);
Expand All @@ -60,7 +60,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let (_b, _c) = (&X, &raw const Y);
| +++++++++

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:60:13
|
LL | foo(&X);
Expand All @@ -73,7 +73,7 @@ help: use `&raw const` instead to create a raw pointer
LL | foo(&raw const X);
| +++++++++

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:66:17
|
LL | let _ = Z.len();
Expand All @@ -82,7 +82,7 @@ LL | let _ = Z.len();
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:72:33
|
LL | let _ = format!("{:?}", Z);
Expand All @@ -91,7 +91,7 @@ LL | let _ = format!("{:?}", Z);
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:76:18
|
LL | let _v = &A.value;
Expand All @@ -104,7 +104,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let _v = &raw const A.value;
| +++++++++

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:80:18
|
LL | let _s = &A.s.value;
Expand All @@ -117,7 +117,7 @@ help: use `&raw const` instead to create a raw pointer
LL | let _s = &raw const A.s.value;
| +++++++++

error: creating a shared reference to mutable static is discouraged
error: creating a shared reference to mutable static
--> $DIR/static-mut-refs.rs:84:22
|
LL | let ref _v = A.value;
Expand All @@ -126,7 +126,7 @@ LL | let ref _v = A.value;
= note: for more information, see <https://doc.rust-lang.org/nightly/edition-guide/rust-2024/static-mut-references.html>
= note: shared references to mutable statics are dangerous; it's undefined behavior if the static is mutated or if a mutable reference is created for it while the shared reference lives

error: creating a mutable reference to mutable static is discouraged
error: creating a mutable reference to mutable static
--> $DIR/static-mut-refs.rs:14:14
|
LL | &mut ($x.0)
Expand Down
48 changes: 24 additions & 24 deletions tests/ui/lint/static-mut-refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ static mut FOO: (u32, u32) = (1, 2);
macro_rules! bar {
($x:expr) => {
&mut ($x.0)
//[e2021]~^ WARN creating a mutable reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR creating a mutable reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN creating a mutable reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR creating a mutable reference to mutable static [static_mut_refs]
};
}

Expand All @@ -36,54 +36,54 @@ fn main() {

unsafe {
let _y = &X;
//[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs]

let _y = &mut X;
//[e2021]~^ WARN mutable reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR mutable reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN mutable reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR mutable reference to mutable static [static_mut_refs]

let _z = &raw mut X;

let _p = &raw const X;

let ref _a = X;
//[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs]

let (_b, _c) = (&X, &Y);
//[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^^^ WARN shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^^^ ERROR shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs]
//[e2021]~^^^ WARN shared reference to mutable static [static_mut_refs]
//[e2024]~^^^^ ERROR shared reference to mutable static [static_mut_refs]

foo(&X);
//[e2021]~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR shared reference to mutable static [static_mut_refs]

static mut Z: &[i32; 3] = &[0, 1, 2];

let _ = Z.len();
//[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

let _ = Z[0];

let _ = format!("{:?}", Z);
//[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

let _v = &A.value;
//[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

let _s = &A.s.value;
//[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

let ref _v = A.value;
//[e2021]~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static is discouraged [static_mut_refs]
//[e2021]~^ WARN creating a shared reference to mutable static [static_mut_refs]
//[e2024]~^^ ERROR creating a shared reference to mutable static [static_mut_refs]

let _x = bar!(FOO);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct S1 {
impl S1 {
fn new(_x: u64) -> S1 {
S1 { a: unsafe { &mut X1 } }
//~^ WARN mutable reference to mutable static is discouraged [static_mut_refs]
//~^ WARN mutable reference to mutable static [static_mut_refs]
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
warning: creating a mutable reference to mutable static is discouraged
warning: creating a mutable reference to mutable static
--> $DIR/borrowck-thread-local-static-mut-borrow-outlives-fn.rs:17:26
|
LL | S1 { a: unsafe { &mut X1 } }
Expand Down
3 changes: 1 addition & 2 deletions tests/ui/statics/issue-15261.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
#![allow(dead_code)]
#![allow(non_upper_case_globals)]


static mut n_mut: usize = 0;

static n: &'static usize = unsafe { &n_mut };
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
//~^ WARN shared reference to mutable static [static_mut_refs]

fn main() {}
Loading
Loading