Skip to content
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/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2728,7 +2728,7 @@ pub(crate) enum MutRefSugg {

#[derive(Subdiagnostic)]
#[suggestion(
"this type already provides \"interior mutability\", so its binding doesn't need to be declared as mutable",
"this type already provides \"interior mutability\", so its binding doesn't need to be declared as mutable when borrowed with a shared reference",
style = "verbose",
applicability = "maybe-incorrect",
code = ""
Expand Down
10 changes: 8 additions & 2 deletions compiler/rustc_lint/src/static_mut_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ fn emit_static_mut_refs(
};

let (interior_mutability_help, interior_mutability_sugg) =
interior_mutability_suggestion(cx, def_id);
interior_mutability_suggestion(cx, def_id, mut_note, suggest_addr_of);

cx.emit_span_lint(
STATIC_MUT_REFS,
Expand All @@ -208,17 +208,23 @@ fn emit_static_mut_refs(
fn interior_mutability_suggestion(
cx: &LateContext<'_>,
def_id: DefId,
mut_ref: bool,
suggest_addr_of: bool,
) -> (bool, Option<StaticMutRefsInteriorMutabilitySugg>) {
let static_ty = cx.tcx.type_of(def_id).skip_binder();
let has_interior_mutability = !static_ty.is_freeze(cx.tcx, cx.typing_env());

if !has_interior_mutability {
return (!suggest_addr_of, None);
}

if mut_ref {
return (false, None);
}

let sugg =
static_mutability_span(cx, def_id).map(|span| StaticMutRefsInteriorMutabilitySugg { span });
(sugg.is_none(), sugg)
(false, sugg)
}

fn static_mutability_span(cx: &LateContext<'_>, def_id: DefId) -> Option<Span> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ LL | let _lock = unsafe { MACRO_MUTEX.lock().unwrap() };
| ^^^^^^^^^^^^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
= note: `#[deny(static_mut_refs)]` (part of `#[deny(rust_2024_compatibility)]`) on by default

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/lint/static-mut-refs-interior-mutability.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | let _lock = unsafe { STDINOUT_MUTEX.lock().unwrap() };
= 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
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
= note: `#[deny(static_mut_refs)]` (part of `#[deny(rust_2024_compatibility)]`) on by default
help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable
help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable when borrowed with a shared reference
|
LL - static mut STDINOUT_MUTEX: Mutex<bool> = Mutex::new(false);
LL + static STDINOUT_MUTEX: Mutex<bool> = Mutex::new(false);
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/lint/static-mut-refs.e2021.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ LL | let ref _a = X;
| ^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: creating a shared reference to mutable static
Expand Down Expand Up @@ -80,6 +81,7 @@ LL | let _ = Z.len();
| ^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: creating a shared reference to mutable static
Expand All @@ -89,6 +91,7 @@ LL | let _ = format!("{:?}", Z);
| ^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: creating a shared reference to mutable static
Expand Down Expand Up @@ -124,6 +127,7 @@ LL | let ref _v = A.value;
| ^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: creating a mutable reference to mutable static
Expand All @@ -136,6 +140,7 @@ LL | let _x = bar!(FOO);
| --------- in this macro invocation
|
= note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
= note: this warning originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
5 changes: 5 additions & 0 deletions tests/ui/lint/static-mut-refs.e2024.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ LL | let ref _a = X;
| ^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

error: creating a shared reference to mutable static
Expand Down Expand Up @@ -80,6 +81,7 @@ LL | let _ = Z.len();
| ^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

error: creating a shared reference to mutable static
Expand All @@ -89,6 +91,7 @@ LL | let _ = format!("{:?}", Z);
| ^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

error: creating a shared reference to mutable static
Expand Down Expand Up @@ -124,6 +127,7 @@ LL | let ref _v = A.value;
| ^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

error: creating a mutable reference to mutable static
Expand All @@ -136,6 +140,7 @@ LL | let _x = bar!(FOO);
| --------- in this macro invocation
|
= note: mutable references to mutable statics are dangerous; it's undefined behavior if any other pointer to the static is used or if any other reference is created for the static while the mutable reference lives
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
= note: this error originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/statics/static-lazy-init-with-arena-set.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | | });
= 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
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
= note: `#[warn(static_mut_refs)]` (part of `#[warn(rust_2024_compatibility)]`) on by default
help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable
help: this type already provides "interior mutability", so its binding doesn't need to be declared as mutable when borrowed with a shared reference
|
LL - static mut ONCE: Once = Once::new();
LL + static ONCE: Once = Once::new();
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/statics/static-mut-xc.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LL | assert_eq!(static_mut_xc::a, 3);
| ^^^^^^^^^^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>
= note: `#[warn(static_mut_refs)]` (part of `#[warn(rust_2024_compatibility)]`) on by default

Expand All @@ -15,6 +16,7 @@ LL | assert_eq!(static_mut_xc::a, 4);
| ^^^^^^^^^^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: creating a shared reference to mutable static
Expand All @@ -24,6 +26,7 @@ LL | assert_eq!(static_mut_xc::a, 5);
| ^^^^^^^^^^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: creating a shared reference to mutable static
Expand All @@ -33,6 +36,7 @@ LL | assert_eq!(static_mut_xc::a, 15);
| ^^^^^^^^^^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: creating a shared reference to mutable static
Expand All @@ -42,6 +46,7 @@ LL | assert_eq!(static_mut_xc::a, -3);
| ^^^^^^^^^^^^^^^^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: creating a shared reference to mutable static
Expand Down
1 change: 1 addition & 0 deletions tests/ui/statics/static-recursive.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ LL | assert_eq!(S, *(S as *const *const u8));
| ^ shared reference to mutable static
|
= 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
= help: use a type that relies on "interior mutability" instead; to read more on this, visit <https://doc.rust-lang.org/reference/interior-mutability.html>
= note: for more information, see <https://doc.rust-lang.org/edition-guide/rust-2024/static-mut-references.html>

warning: 2 warnings emitted
Expand Down
Loading