diff --git a/compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs b/compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs index 2a7b1107ffcaa..8b0b9123ac7df 100644 --- a/compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs +++ b/compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs @@ -112,8 +112,8 @@ fn start( static mut NUM: u8 = 6 * 7; -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#[allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#[allow(static_mut_refs)] static NUM_REF: &'static u8 = unsafe { &NUM }; unsafe fn zeroed() -> T { diff --git a/compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs b/compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs index 9827e299f2a31..add77880716c8 100644 --- a/compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs +++ b/compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs @@ -99,8 +99,8 @@ fn start( static mut NUM: u8 = 6 * 7; -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#[allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#[allow(static_mut_refs)] static NUM_REF: &'static u8 = unsafe { &NUM }; macro_rules! assert { diff --git a/compiler/rustc_error_codes/src/error_codes/E0796.md b/compiler/rustc_error_codes/src/error_codes/E0796.md index cea18f8db851f..7ac429e521581 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0796.md +++ b/compiler/rustc_error_codes/src/error_codes/E0796.md @@ -1,22 +1,26 @@ -Reference of mutable static. +You have created a reference to a mutable static. Erroneous code example: ```compile_fail,edition2024,E0796 static mut X: i32 = 23; -static mut Y: i32 = 24; -unsafe { - let y = &X; - let ref x = X; - let (x, y) = (&X, &Y); - foo(&X); +fn work() { + let _val = unsafe { X }; } -fn foo<'a>(_x: &'a i32) {} +let x_ref = unsafe { &mut X }; +work(); +// The next line has Undefined Behavior! +// `x_ref` is a mutable reference and allows no aliases, +// but `work` has been reading the reference between +// the moment `x_ref` was created and when it was used. +// This violates the uniqueness of `x_ref`. +*x_ref = 42; ``` -Mutable statics can be written to by multiple threads: aliasing violations or -data races will cause undefined behavior. +A reference to a mutable static has lifetime `'static`. This is very dangerous +as it is easy to accidentally overlap the lifetime of that reference with +other, conflicting accesses to the same static. -Reference of mutable static is a hard error from 2024 edition. +References to mutable statics are a hard error in the 2024 edition. diff --git a/compiler/rustc_hir_analysis/messages.ftl b/compiler/rustc_hir_analysis/messages.ftl index a61cfd0e4ce9a..99f7ab2fa4d9c 100644 --- a/compiler/rustc_hir_analysis/messages.ftl +++ b/compiler/rustc_hir_analysis/messages.ftl @@ -373,19 +373,24 @@ 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 - .label = reference of mutable static - .note = mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior - .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 - .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 +hir_analysis_static_mut_ref = creating a {$shared} reference to a mutable static + .label = {$shared}reference to mutable static + .note = {$shared -> + [shared] this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + *[mutable] this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + } + .suggestion = use `addr_of!` instead to create a raw pointer + .suggestion_mut = use `addr_of_mut!` instead to create a raw pointer + +hir_analysis_static_mut_refs_lint = creating a {$shared} reference to mutable static is discouraged + .label = {$shared} reference to mutable static + .suggestion = use `addr_of!` instead to create a raw pointer + .suggestion_mut = use `addr_of_mut!` instead to create a raw pointer + .note = this will be a hard error in the 2024 edition + .why_note = {$shared -> + [shared] this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + *[mutable] this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + } hir_analysis_static_specialize = cannot specialize on `'static` lifetime diff --git a/compiler/rustc_hir_analysis/src/check/errs.rs b/compiler/rustc_hir_analysis/src/check/errs.rs index 87a1f3d342580..4a7ace274c5bb 100644 --- a/compiler/rustc_hir_analysis/src/check/errs.rs +++ b/compiler/rustc_hir_analysis/src/check/errs.rs @@ -1,6 +1,6 @@ use rustc_hir as hir; use rustc_hir_pretty::qpath_to_string; -use rustc_lint_defs::builtin::STATIC_MUT_REF; +use rustc_lint_defs::builtin::STATIC_MUT_REFS; use rustc_middle::ty::TyCtxt; use rustc_span::Span; use rustc_type_ir::Mutability; @@ -66,32 +66,24 @@ fn handle_static_mut_ref( hir_id: hir::HirId, ) { if e2024 { - let sugg = if mutable { - errors::StaticMutRefSugg::Mut { span, var } + let (sugg, shared) = if mutable { + (errors::StaticMutRefSugg::Mut { span, var }, "mutable") } else { - errors::StaticMutRefSugg::Shared { span, var } + (errors::StaticMutRefSugg::Shared { span, var }, "shared") }; - tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg }); + tcx.sess.parse_sess.dcx.emit_err(errors::StaticMutRef { span, sugg, shared }); return; } - let (label, sugg, shared) = if mutable { - ( - errors::RefOfMutStaticLabel::Mut { span }, - errors::RefOfMutStaticSugg::Mut { span, var }, - "mutable ", - ) + let (sugg, shared) = if mutable { + (errors::RefOfMutStaticSugg::Mut { span, var }, "mutable") } else { - ( - errors::RefOfMutStaticLabel::Shared { span }, - errors::RefOfMutStaticSugg::Shared { span, var }, - "shared ", - ) + (errors::RefOfMutStaticSugg::Shared { span, var }, "shared") }; tcx.emit_node_span_lint( - STATIC_MUT_REF, + STATIC_MUT_REFS, hir_id, span, - errors::RefOfMutStatic { shared, why_note: (), label, sugg }, + errors::RefOfMutStatic { span, sugg, shared }, ); } diff --git a/compiler/rustc_hir_analysis/src/errors.rs b/compiler/rustc_hir_analysis/src/errors.rs index 6a505b961974f..a8fd311df4321 100644 --- a/compiler/rustc_hir_analysis/src/errors.rs +++ b/compiler/rustc_hir_analysis/src/errors.rs @@ -1455,12 +1455,13 @@ pub struct OnlyCurrentTraitsPointerSugg<'a> { #[derive(Diagnostic)] #[diag(hir_analysis_static_mut_ref, code = E0796)] #[note] -pub struct StaticMutRef { +pub struct StaticMutRef<'a> { #[primary_span] #[label] pub span: Span, #[subdiagnostic] pub sugg: StaticMutRefSugg, + pub shared: &'a str, } #[derive(Subdiagnostic)] @@ -1491,30 +1492,15 @@ pub enum StaticMutRefSugg { // STATIC_MUT_REF lint #[derive(LintDiagnostic)] -#[diag(hir_analysis_static_mut_ref_lint)] +#[diag(hir_analysis_static_mut_refs_lint)] #[note] +#[note(hir_analysis_why_note)] pub struct RefOfMutStatic<'a> { - pub shared: &'a str, - #[note(hir_analysis_why_note)] - pub why_note: (), - #[subdiagnostic] - pub label: RefOfMutStaticLabel, + #[label] + pub span: Span, #[subdiagnostic] pub sugg: RefOfMutStaticSugg, -} - -#[derive(Subdiagnostic)] -pub enum RefOfMutStaticLabel { - #[label(hir_analysis_label)] - Shared { - #[primary_span] - span: Span, - }, - #[label(hir_analysis_label_mut)] - Mut { - #[primary_span] - span: Span, - }, + pub shared: &'a str, } #[derive(Subdiagnostic)] diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs index 5f769e9ad8a5b..0598a5de06342 100644 --- a/compiler/rustc_lint/src/lib.rs +++ b/compiler/rustc_lint/src/lib.rs @@ -324,6 +324,7 @@ fn register_builtins(store: &mut LintStore) { store.register_renamed("or_patterns_back_compat", "rust_2021_incompatible_or_patterns"); store.register_renamed("non_fmt_panic", "non_fmt_panics"); store.register_renamed("unused_tuple_struct_fields", "dead_code"); + store.register_renamed("static_mut_ref", "static_mut_refs"); // These were moved to tool lints, but rustc still sees them when compiling normally, before // tool lints are registered, so `check_tool_name_for_backwards_compat` doesn't work. Use diff --git a/compiler/rustc_lint_defs/src/builtin.rs b/compiler/rustc_lint_defs/src/builtin.rs index 6a2a2c1e48e2a..3f5d3c2597151 100644 --- a/compiler/rustc_lint_defs/src/builtin.rs +++ b/compiler/rustc_lint_defs/src/builtin.rs @@ -89,7 +89,7 @@ declare_lint_pass! { SINGLE_USE_LIFETIMES, SOFT_UNSTABLE, STABLE_FEATURES, - STATIC_MUT_REF, + STATIC_MUT_REFS, SUSPICIOUS_AUTO_TRAIT_IMPLS, TEST_UNSTABLE_LINT, TEXT_DIRECTION_CODEPOINT_IN_COMMENT, @@ -1769,7 +1769,7 @@ declare_lint! { } declare_lint! { - /// The `static_mut_ref` lint checks for shared or mutable references + /// The `static_mut_refs` lint checks for shared or mutable references /// of mutable static inside `unsafe` blocks and `unsafe` functions. /// /// ### Example @@ -1807,9 +1807,9 @@ 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, + pub STATIC_MUT_REFS, Warn, "shared references or mutable references of mutable static is discouraged", @future_incompatible = FutureIncompatibleInfo { diff --git a/library/panic_unwind/src/seh.rs b/library/panic_unwind/src/seh.rs index d3ba546d730d4..876954ab87e85 100644 --- a/library/panic_unwind/src/seh.rs +++ b/library/panic_unwind/src/seh.rs @@ -261,8 +261,8 @@ cfg_if::cfg_if! { } } -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#[allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#[allow(static_mut_refs)] pub unsafe fn panic(data: Box) -> u32 { use core::intrinsics::atomic_store_seqcst; @@ -324,8 +324,8 @@ pub unsafe fn panic(data: Box) -> u32 { _CxxThrowException(throw_ptr, &mut THROW_INFO as *mut _ as *mut _); } -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#[allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#[allow(static_mut_refs)] pub unsafe fn cleanup(payload: *mut u8) -> Box { // A null payload here means that we got here from the catch (...) of // __rust_try. This happens when a non-Rust foreign exception is caught. diff --git a/library/std/src/panicking.rs b/library/std/src/panicking.rs index 8294160b72ca7..c8306c1b597a3 100644 --- a/library/std/src/panicking.rs +++ b/library/std/src/panicking.rs @@ -337,8 +337,9 @@ pub mod panic_count { #[doc(hidden)] #[cfg(not(feature = "panic_immediate_abort"))] #[unstable(feature = "update_panic_count", issue = "none")] -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#[allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#[cfg_attr(bootstrap, allow(static_mut_ref))] +#[cfg_attr(not(bootstrap), allow(static_mut_refs))] pub mod panic_count { use crate::cell::Cell; use crate::sync::atomic::{AtomicUsize, Ordering}; diff --git a/library/std/src/sys/pal/common/thread_local/fast_local.rs b/library/std/src/sys/pal/common/thread_local/fast_local.rs index 0fdca27852ca6..48f5369272b2f 100644 --- a/library/std/src/sys/pal/common/thread_local/fast_local.rs +++ b/library/std/src/sys/pal/common/thread_local/fast_local.rs @@ -13,8 +13,9 @@ pub macro thread_local_inner { (@key $t:ty, const $init:expr) => {{ #[inline] #[deny(unsafe_op_in_unsafe_fn)] - // FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint - #[allow(static_mut_ref)] + // FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint + #[cfg_attr(bootstrap, allow(static_mut_ref))] + #[cfg_attr(not(bootstrap), allow(static_mut_refs))] unsafe fn __getit( _init: $crate::option::Option<&mut $crate::option::Option<$t>>, ) -> $crate::option::Option<&'static $t> { diff --git a/library/std/src/sys/pal/common/thread_local/static_local.rs b/library/std/src/sys/pal/common/thread_local/static_local.rs index 0dde78b14dbb8..206e62bb5e2c8 100644 --- a/library/std/src/sys/pal/common/thread_local/static_local.rs +++ b/library/std/src/sys/pal/common/thread_local/static_local.rs @@ -11,8 +11,8 @@ pub macro thread_local_inner { (@key $t:ty, const $init:expr) => {{ #[inline] // see comments below #[deny(unsafe_op_in_unsafe_fn)] - // FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint - #[allow(static_mut_ref)] + // FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint + #[allow(static_mut_refs)] unsafe fn __getit( _init: $crate::option::Option<&mut $crate::option::Option<$t>>, ) -> $crate::option::Option<&'static $t> { diff --git a/library/std/src/thread/local.rs b/library/std/src/thread/local.rs index 83d5d63556fc3..d1213e2f16686 100644 --- a/library/std/src/thread/local.rs +++ b/library/std/src/thread/local.rs @@ -180,8 +180,8 @@ impl fmt::Debug for LocalKey { #[stable(feature = "rust1", since = "1.0.0")] #[cfg_attr(not(test), rustc_diagnostic_item = "thread_local_macro")] #[allow_internal_unstable(thread_local_internals)] -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#[allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#[cfg_attr(not(bootstrap), allow(static_mut_refs))] macro_rules! thread_local { // empty (base case for the recursion) () => {}; diff --git a/src/tools/miri/tests/fail/tls/tls_static_dealloc.rs b/src/tools/miri/tests/fail/tls/tls_static_dealloc.rs index 762a8d85314f2..d47a05d8475cf 100644 --- a/src/tools/miri/tests/fail/tls/tls_static_dealloc.rs +++ b/src/tools/miri/tests/fail/tls/tls_static_dealloc.rs @@ -1,8 +1,8 @@ //! Ensure that thread-local statics get deallocated when the thread dies. #![feature(thread_local)] -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#![allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#![allow(static_mut_refs)] #[thread_local] static mut TLS: u8 = 0; diff --git a/src/tools/miri/tests/pass/static_mut.rs b/src/tools/miri/tests/pass/static_mut.rs index c1e58b70adb0b..6b0c0297726f3 100644 --- a/src/tools/miri/tests/pass/static_mut.rs +++ b/src/tools/miri/tests/pass/static_mut.rs @@ -1,7 +1,7 @@ static mut FOO: i32 = 42; -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#[allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#[allow(static_mut_refs)] static BAR: Foo = Foo(unsafe { &FOO as *const _ }); #[allow(dead_code)] diff --git a/src/tools/miri/tests/pass/tls/tls_static.rs b/src/tools/miri/tests/pass/tls/tls_static.rs index 9be00af47aa35..fea5bb1db5e6b 100644 --- a/src/tools/miri/tests/pass/tls/tls_static.rs +++ b/src/tools/miri/tests/pass/tls/tls_static.rs @@ -8,8 +8,8 @@ //! test, we also check that thread-locals act as per-thread statics. #![feature(thread_local)] -// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint -#![allow(static_mut_ref)] +// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_refs` lint +#![allow(static_mut_refs)] use std::thread; diff --git a/tests/ui/abi/statics/static-mut-foreign.rs b/tests/ui/abi/statics/static-mut-foreign.rs index eb732e7c2c31f..5d7cf1477c1c1 100644 --- a/tests/ui/abi/statics/static-mut-foreign.rs +++ b/tests/ui/abi/statics/static-mut-foreign.rs @@ -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_refs] 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_refs] } pub fn main() { diff --git a/tests/ui/abi/statics/static-mut-foreign.stderr b/tests/ui/abi/statics/static-mut-foreign.stderr index 144ac056f87e4..f393088ff9f33 100644 --- a/tests/ui/abi/statics/static-mut-foreign.stderr +++ b/tests/ui/abi/statics/static-mut-foreign.stderr @@ -1,28 +1,28 @@ -warning: shared reference of mutable static is discouraged +warning: creating a 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 + | ^^^^^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: 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: creating a 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 + | ^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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 -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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior +help: use `addr_of_mut!` instead to create a raw pointer | LL | static_bound_set(addr_of_mut!(rust_dbg_static_mut)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/ui/borrowck/borrowck-access-permissions.rs b/tests/ui/borrowck/borrowck-access-permissions.rs index 1638644103ba4..be11286a523df 100644 --- a/tests/ui/borrowck/borrowck-access-permissions.rs +++ b/tests/ui/borrowck/borrowck-access-permissions.rs @@ -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_refs] } } diff --git a/tests/ui/borrowck/borrowck-access-permissions.stderr b/tests/ui/borrowck/borrowck-access-permissions.stderr index 93d92295dd9b6..11e2b63bd6cd7 100644 --- a/tests/ui/borrowck/borrowck-access-permissions.stderr +++ b/tests/ui/borrowck/borrowck-access-permissions.stderr @@ -1,14 +1,14 @@ -warning: mutable reference of mutable static is discouraged +warning: creating a 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 + | ^^^^^^^^^^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of_mut!` instead to create a raw pointer | LL | let _y2 = addr_of_mut!(static_x_mut); | ~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs index 1bf079e24cae4..76a28f928e3c0 100644 --- a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs +++ b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs @@ -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_refs] let x = (*sfoo).x(); (*sfoo).x[1] += 1; *x += 1; diff --git a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr index 7a3824f79a4c2..354d70eb1ad49 100644 --- a/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr +++ b/tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr @@ -1,14 +1,14 @@ -warning: mutable reference of mutable static is discouraged +warning: creating a 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 + | ^^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of_mut!` instead to create a raw pointer | LL | let sfoo: *mut Foo = addr_of_mut!(SFOO); | ~~~~~~~~~~~~~~~~~~ diff --git a/tests/ui/borrowck/issue-20801.rs b/tests/ui/borrowck/issue-20801.rs index ec83af5d5dfc6..7e3d3703dc765 100644 --- a/tests/ui/borrowck/issue-20801.rs +++ b/tests/ui/borrowck/issue-20801.rs @@ -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_refs] } fn mut_ptr() -> *mut T { diff --git a/tests/ui/borrowck/issue-20801.stderr b/tests/ui/borrowck/issue-20801.stderr index b2bee2d880394..97294afd3df9e 100644 --- a/tests/ui/borrowck/issue-20801.stderr +++ b/tests/ui/borrowck/issue-20801.stderr @@ -1,14 +1,14 @@ -warning: mutable reference of mutable static is discouraged +warning: creating a mutable reference to mutable static is discouraged --> $DIR/issue-20801.rs:14:14 | LL | unsafe { &mut GLOBAL_MUT_T } - | ^^^^^^^^^^^^^^^^^ mutable reference of mutable static + | ^^^^^^^^^^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of_mut!` instead to create a raw pointer | LL | unsafe { addr_of_mut!(GLOBAL_MUT_T) } | ~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs b/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs index 9b172b4131911..c3909d0596339 100644 --- a/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs +++ b/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.rs @@ -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_refs] } } } @@ -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_refs] } } } @@ -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_refs] } mutability_errors::capture_assign_whole((1000,)); diff --git a/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr b/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr index e4e4947fce1c1..098a2964e9fc7 100644 --- a/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr +++ b/tests/ui/borrowck/issue-55492-borrowck-migrate-scans-parents.stderr @@ -1,42 +1,42 @@ -warning: mutable reference of mutable static is discouraged +warning: creating a 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 + | ^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: 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: creating a 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 + | ^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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 -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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior +help: 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: creating a 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 + | ^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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 -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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior +help: use `addr_of_mut!` instead to create a raw pointer | LL | borrowck_closures_unique::e(addr_of_mut!(X)); | ~~~~~~~~~~~~~~~ diff --git a/tests/ui/consts/const_let_assign2.rs b/tests/ui/consts/const_let_assign2.rs index 1c7afe0e3d6cb..f375f0f603df5 100644 --- a/tests/ui/consts/const_let_assign2.rs +++ b/tests/ui/consts/const_let_assign2.rs @@ -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_refs] for a in ptr.data.iter() { println!("{}", a); } diff --git a/tests/ui/consts/const_let_assign2.stderr b/tests/ui/consts/const_let_assign2.stderr index 2764153a8a590..5ae8fd353dd78 100644 --- a/tests/ui/consts/const_let_assign2.stderr +++ b/tests/ui/consts/const_let_assign2.stderr @@ -1,14 +1,14 @@ -warning: mutable reference of mutable static is discouraged +warning: creating a 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 + | ^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of_mut!` instead to create a raw pointer | LL | let ptr = unsafe { addr_of_mut!(BB) }; | ~~~~~~~~~~~~~~~~ diff --git a/tests/ui/consts/const_refs_to_static_fail_invalid.rs b/tests/ui/consts/const_refs_to_static_fail_invalid.rs index 665b876c43e2c..bd841c175bf81 100644 --- a/tests/ui/consts/const_refs_to_static_fail_invalid.rs +++ b/tests/ui/consts/const_refs_to_static_fail_invalid.rs @@ -1,7 +1,7 @@ // normalize-stderr-test "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)" // normalize-stderr-test "([0-9a-f][0-9a-f] |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?()?─*╼ )+ *│.*" -> "HEX_DUMP" #![feature(const_refs_to_static)] -#![allow(static_mut_ref)] +#![allow(static_mut_refs)] fn invalid() { static S: i8 = 10; @@ -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 + _ => {} } } diff --git a/tests/ui/consts/const_refs_to_static_fail_invalid.stderr b/tests/ui/consts/const_refs_to_static_fail_invalid.stderr index 082f8532444ad..4ff15f0c28b8f 100644 --- a/tests/ui/consts/const_refs_to_static_fail_invalid.stderr +++ b/tests/ui/consts/const_refs_to_static_fail_invalid.stderr @@ -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 diff --git a/tests/ui/consts/issue-17718-const-bad-values.rs b/tests/ui/consts/issue-17718-const-bad-values.rs index 0299bfef1b49a..52f8c9bf149a6 100644 --- a/tests/ui/consts/issue-17718-const-bad-values.rs +++ b/tests/ui/consts/issue-17718-const-bad-values.rs @@ -1,4 +1,4 @@ -#![allow(static_mut_ref)] +#![allow(static_mut_refs)] const C1: &'static mut [usize] = &mut []; //~^ ERROR: mutable references are not allowed diff --git a/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs b/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs index bcd29f8b03441..bcece4ccb34b5 100644 --- a/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs +++ b/tests/ui/consts/miri_unleashed/const_refers_to_static_cross_crate.rs @@ -2,7 +2,7 @@ // aux-build:static_cross_crate.rs // stderr-per-bitwidth #![feature(exclusive_range_pattern, half_open_range_patterns_in_slices)] -#![allow(static_mut_ref)] +#![allow(static_mut_refs)] extern crate static_cross_crate; diff --git a/tests/ui/consts/miri_unleashed/extern-static.rs b/tests/ui/consts/miri_unleashed/extern-static.rs index 81176b3d4e997..686aa90f55e81 100644 --- a/tests/ui/consts/miri_unleashed/extern-static.rs +++ b/tests/ui/consts/miri_unleashed/extern-static.rs @@ -1,6 +1,6 @@ // compile-flags: -Zunleash-the-miri-inside-of-you #![feature(thread_local)] -#![allow(static_mut_ref)] +#![allow(static_mut_refs)] extern "C" { static mut DATA: u8; diff --git a/tests/ui/consts/miri_unleashed/mutable_references_err.rs b/tests/ui/consts/miri_unleashed/mutable_references_err.rs index 43b65f459a1eb..aabcf6d7bee3e 100644 --- a/tests/ui/consts/miri_unleashed/mutable_references_err.rs +++ b/tests/ui/consts/miri_unleashed/mutable_references_err.rs @@ -1,6 +1,6 @@ // stderr-per-bitwidth // compile-flags: -Zunleash-the-miri-inside-of-you -#![allow(invalid_reference_casting, static_mut_ref)] +#![allow(invalid_reference_casting, static_mut_refs)] use std::sync::atomic::*; use std::cell::UnsafeCell; diff --git a/tests/ui/consts/static-promoted-to-mutable-static.rs b/tests/ui/consts/static-promoted-to-mutable-static.rs index d49ba478dbc82..9150867d42330 100644 --- a/tests/ui/consts/static-promoted-to-mutable-static.rs +++ b/tests/ui/consts/static-promoted-to-mutable-static.rs @@ -1,5 +1,5 @@ // check-pass -#![allow(non_camel_case_types, non_upper_case_globals, static_mut_ref)] +#![allow(non_camel_case_types, non_upper_case_globals, static_mut_refs)] pub struct wl_interface { pub version: i32 diff --git a/tests/ui/consts/static_mut_containing_mut_ref.rs b/tests/ui/consts/static_mut_containing_mut_ref.rs index 495804649b14b..3f5bd8007adca 100644 --- a/tests/ui/consts/static_mut_containing_mut_ref.rs +++ b/tests/ui/consts/static_mut_containing_mut_ref.rs @@ -1,5 +1,5 @@ // build-pass (FIXME(62277): could be check-pass?) -#![allow(static_mut_ref)] +#![allow(static_mut_refs)] static mut STDERR_BUFFER_SPACE: [u8; 42] = [0u8; 42]; diff --git a/tests/ui/consts/static_mut_containing_mut_ref2.rs b/tests/ui/consts/static_mut_containing_mut_ref2.rs index e60a17922fd0c..c2e75c1ba9f0e 100644 --- a/tests/ui/consts/static_mut_containing_mut_ref2.rs +++ b/tests/ui/consts/static_mut_containing_mut_ref2.rs @@ -1,5 +1,5 @@ // revisions: stock mut_refs -#![allow(static_mut_ref)] +#![allow(static_mut_refs)] #![cfg_attr(mut_refs, feature(const_mut_refs))] static mut STDERR_BUFFER_SPACE: u8 = 0; diff --git a/tests/ui/drop/issue-23338-ensure-param-drop-order.rs b/tests/ui/drop/issue-23338-ensure-param-drop-order.rs index 52603744c45fc..a6796bd17df17 100644 --- a/tests/ui/drop/issue-23338-ensure-param-drop-order.rs +++ b/tests/ui/drop/issue-23338-ensure-param-drop-order.rs @@ -91,7 +91,7 @@ pub mod d { pub fn max_width() -> u32 { unsafe { (mem::size_of_val(&trails) * 8) as u32 - //~^ WARN shared reference of mutable static is discouraged [static_mut_ref] + //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] } } diff --git a/tests/ui/drop/issue-23338-ensure-param-drop-order.stderr b/tests/ui/drop/issue-23338-ensure-param-drop-order.stderr index fd36ccbcbee47..de1194e74b40d 100644 --- a/tests/ui/drop/issue-23338-ensure-param-drop-order.stderr +++ b/tests/ui/drop/issue-23338-ensure-param-drop-order.stderr @@ -1,14 +1,14 @@ -warning: shared reference of mutable static is discouraged +warning: creating a shared reference to mutable static is discouraged --> $DIR/issue-23338-ensure-param-drop-order.rs:93:31 | LL | (mem::size_of_val(&trails) * 8) as u32 - | ^^^^^^^ shared reference of mutable static + | ^^^^^^^ shared reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of!` instead to create a raw pointer | LL | (mem::size_of_val(addr_of!(trails)) * 8) as u32 | ~~~~~~~~~~~~~~~~ diff --git a/tests/ui/error-codes/E0017.rs b/tests/ui/error-codes/E0017.rs index 9d3433fa543fd..4637fc75b48c7 100644 --- a/tests/ui/error-codes/E0017.rs +++ b/tests/ui/error-codes/E0017.rs @@ -13,6 +13,6 @@ static CONST_REF: &'static mut i32 = &mut C; //~ ERROR mutable references are no //~| WARN taking a mutable static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; //~ ERROR mutable references are not -//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref] +//~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] fn main() {} diff --git a/tests/ui/error-codes/E0017.stderr b/tests/ui/error-codes/E0017.stderr index 2a70f2ee0ae82..f4a51dca207d9 100644 --- a/tests/ui/error-codes/E0017.stderr +++ b/tests/ui/error-codes/E0017.stderr @@ -1,14 +1,14 @@ -warning: mutable reference of mutable static is discouraged +warning: creating a mutable reference to mutable static is discouraged --> $DIR/E0017.rs:15:52 | LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { &mut M }; - | ^^^^^^ mutable reference of mutable static + | ^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of_mut!` instead to create a raw pointer | LL | static STATIC_MUT_REF: &'static mut i32 = unsafe { addr_of_mut!(M) }; | ~~~~~~~~~~~~~~~ diff --git a/tests/ui/issues/issue-23611-enum-swap-in-drop.rs b/tests/ui/issues/issue-23611-enum-swap-in-drop.rs index b967e6aecdd43..0582ff5e80ad9 100644 --- a/tests/ui/issues/issue-23611-enum-swap-in-drop.rs +++ b/tests/ui/issues/issue-23611-enum-swap-in-drop.rs @@ -187,7 +187,7 @@ pub mod d { pub fn max_width() -> u32 { unsafe { (mem::size_of_val(&trails) * 8) as u32 - //~^ WARN shared reference of mutable static is discouraged [static_mut_ref] + //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] } } diff --git a/tests/ui/issues/issue-23611-enum-swap-in-drop.stderr b/tests/ui/issues/issue-23611-enum-swap-in-drop.stderr index 14a986a333264..bdf46abea8a1f 100644 --- a/tests/ui/issues/issue-23611-enum-swap-in-drop.stderr +++ b/tests/ui/issues/issue-23611-enum-swap-in-drop.stderr @@ -1,14 +1,14 @@ -warning: shared reference of mutable static is discouraged +warning: creating a shared reference to mutable static is discouraged --> $DIR/issue-23611-enum-swap-in-drop.rs:189:31 | LL | (mem::size_of_val(&trails) * 8) as u32 - | ^^^^^^^ shared reference of mutable static + | ^^^^^^^ shared reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of!` instead to create a raw pointer | LL | (mem::size_of_val(addr_of!(trails)) * 8) as u32 | ~~~~~~~~~~~~~~~~ diff --git a/tests/ui/issues/issue-54410.rs b/tests/ui/issues/issue-54410.rs index e3e8ca985b970..8f48a33615373 100644 --- a/tests/ui/issues/issue-54410.rs +++ b/tests/ui/issues/issue-54410.rs @@ -1,3 +1,5 @@ +#![allow(static_mut_refs)] + extern "C" { pub static mut symbol: [i8]; //~^ ERROR the size for values of type `[i8]` cannot be known at compilation time diff --git a/tests/ui/issues/issue-54410.stderr b/tests/ui/issues/issue-54410.stderr index 97e5990750e3c..6152f0073b70f 100644 --- a/tests/ui/issues/issue-54410.stderr +++ b/tests/ui/issues/issue-54410.stderr @@ -1,5 +1,5 @@ error[E0277]: the size for values of type `[i8]` cannot be known at compilation time - --> $DIR/issue-54410.rs:2:28 + --> $DIR/issue-54410.rs:4:28 | LL | pub static mut symbol: [i8]; | ^^^^ doesn't have a size known at compile-time diff --git a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.rs b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.rs index 8eb544e8ab9d2..39d594b0e3227 100644 --- a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.rs +++ b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.rs @@ -15,7 +15,7 @@ struct S1 { impl S1 { fn new(_x: u64) -> S1 { S1 { a: unsafe { &mut X1 } } - //~^ WARN mutable reference of mutable static is discouraged [static_mut_ref] + //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] } } diff --git a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr index 17217cd5859d0..82065cc06ea1d 100644 --- a/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr +++ b/tests/ui/nll/borrowck-thread-local-static-mut-borrow-outlives-fn.stderr @@ -1,14 +1,14 @@ -warning: mutable reference of mutable static is discouraged +warning: creating a mutable reference to mutable static is discouraged --> $DIR/borrowck-thread-local-static-mut-borrow-outlives-fn.rs:17:26 | LL | S1 { a: unsafe { &mut X1 } } - | ^^^^^^^ mutable reference of mutable static + | ^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of_mut!` instead to create a raw pointer | LL | S1 { a: unsafe { addr_of_mut!(X1) } } | ~~~~~~~~~~~~~~~~ diff --git a/tests/ui/static/reference-of-mut-static-safe.e2021.stderr b/tests/ui/static/reference-of-mut-static-safe.e2021.stderr deleted file mode 100644 index 16f47ace3a930..0000000000000 --- a/tests/ui/static/reference-of-mut-static-safe.e2021.stderr +++ /dev/null @@ -1,26 +0,0 @@ -warning: shared reference of mutable static is discouraged - --> $DIR/reference-of-mut-static-safe.rs:9:14 - | -LL | let _x = &X; - | ^^ shared reference of mutable static - | - = note: for more information, see issue #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: `#[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 | let _x = addr_of!(X); - | ~~~~~~~~~~~ - -error[E0133]: use of mutable static is unsafe and requires unsafe function or block - --> $DIR/reference-of-mut-static-safe.rs:9:15 - | -LL | let _x = &X; - | ^ use of mutable static - | - = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior - -error: aborting due to 1 previous error; 1 warning emitted - -For more information about this error, try `rustc --explain E0133`. diff --git a/tests/ui/static/reference-of-mut-static-safe.e2024.stderr b/tests/ui/static/reference-of-mut-static-safe.e2024.stderr deleted file mode 100644 index 53f81179de55a..0000000000000 --- a/tests/ui/static/reference-of-mut-static-safe.e2024.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static-safe.rs:9:14 - | -LL | let _x = &X; - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let _x = addr_of!(X); - | ~~~~~~~~~~~ - -error: aborting due to 1 previous error - -For more information about this error, try `rustc --explain E0796`. diff --git a/tests/ui/static/reference-of-mut-static-unsafe-fn.rs b/tests/ui/static/reference-of-mut-static-unsafe-fn.rs deleted file mode 100644 index 6b1e77850e50d..0000000000000 --- a/tests/ui/static/reference-of-mut-static-unsafe-fn.rs +++ /dev/null @@ -1,23 +0,0 @@ -// compile-flags: --edition 2024 -Z unstable-options - -fn main() {} - -unsafe fn _foo() { - static mut X: i32 = 1; - static mut Y: i32 = 1; - - let _y = &X; - //~^ ERROR reference of mutable static is disallowed - - let ref _a = X; - //~^ ERROR reference of mutable static is disallowed - - let (_b, _c) = (&X, &Y); - //~^ ERROR reference of mutable static is disallowed - //~^^ ERROR reference of mutable static is disallowed - - foo(&X); - //~^ ERROR reference of mutable static is disallowed -} - -fn foo<'a>(_x: &'a i32) {} diff --git a/tests/ui/static/reference-of-mut-static-unsafe-fn.stderr b/tests/ui/static/reference-of-mut-static-unsafe-fn.stderr deleted file mode 100644 index 5c6fdedfa96f7..0000000000000 --- a/tests/ui/static/reference-of-mut-static-unsafe-fn.stderr +++ /dev/null @@ -1,63 +0,0 @@ -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static-unsafe-fn.rs:9:14 - | -LL | let _y = &X; - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let _y = addr_of!(X); - | ~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static-unsafe-fn.rs:12:18 - | -LL | let ref _a = X; - | ^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let ref _a = addr_of!(X); - | ~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static-unsafe-fn.rs:15:21 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let (_b, _c) = (addr_of!(X), &Y); - | ~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static-unsafe-fn.rs:15:25 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let (_b, _c) = (&X, addr_of!(Y)); - | ~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static-unsafe-fn.rs:19:9 - | -LL | foo(&X); - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | foo(addr_of!(X)); - | ~~~~~~~~~~~ - -error: aborting due to 5 previous errors - -For more information about this error, try `rustc --explain E0796`. diff --git a/tests/ui/static/reference-of-mut-static.e2021.stderr b/tests/ui/static/reference-of-mut-static.e2021.stderr deleted file mode 100644 index 77a6b3d304bdb..0000000000000 --- a/tests/ui/static/reference-of-mut-static.e2021.stderr +++ /dev/null @@ -1,91 +0,0 @@ -error: shared reference of mutable static is discouraged - --> $DIR/reference-of-mut-static.rs:16:18 - | -LL | let _y = &X; - | ^^ shared reference of mutable static - | - = note: for more information, see issue #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: the lint level is defined here - --> $DIR/reference-of-mut-static.rs:6:9 - | -LL | #![deny(static_mut_ref)] - | ^^^^^^^^^^^^^^ -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 | let _y = addr_of!(X); - | ~~~~~~~~~~~ - -error: mutable reference of mutable static is discouraged - --> $DIR/reference-of-mut-static.rs:20:18 - | -LL | let _y = &mut X; - | ^^^^^^ mutable reference of mutable static - | - = note: for more information, see issue #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 -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 | let _y = addr_of_mut!(X); - | ~~~~~~~~~~~~~~~ - -error: shared reference of mutable static is discouraged - --> $DIR/reference-of-mut-static.rs:28:22 - | -LL | let ref _a = X; - | ^ shared reference of mutable static - | - = note: for more information, see issue #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 -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 | let ref _a = addr_of!(X); - | ~~~~~~~~~~~ - -error: shared reference of mutable static is discouraged - --> $DIR/reference-of-mut-static.rs:32:25 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ shared reference of mutable static - | - = note: for more information, see issue #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 -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 | let (_b, _c) = (addr_of!(X), &Y); - | ~~~~~~~~~~~ - -error: shared reference of mutable static is discouraged - --> $DIR/reference-of-mut-static.rs:32:29 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ shared reference of mutable static - | - = note: for more information, see issue #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 -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 | let (_b, _c) = (&X, addr_of!(Y)); - | ~~~~~~~~~~~ - -error: shared reference of mutable static is discouraged - --> $DIR/reference-of-mut-static.rs:38:13 - | -LL | foo(&X); - | ^^ shared reference of mutable static - | - = note: for more information, see issue #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 -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 | foo(addr_of!(X)); - | ~~~~~~~~~~~ - -error: aborting due to 6 previous errors - diff --git a/tests/ui/static/reference-of-mut-static.e2024.stderr b/tests/ui/static/reference-of-mut-static.e2024.stderr deleted file mode 100644 index f445ec65a5d24..0000000000000 --- a/tests/ui/static/reference-of-mut-static.e2024.stderr +++ /dev/null @@ -1,75 +0,0 @@ -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static.rs:16:18 - | -LL | let _y = &X; - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let _y = addr_of!(X); - | ~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static.rs:20:18 - | -LL | let _y = &mut X; - | ^^^^^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let _y = addr_of_mut!(X); - | ~~~~~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static.rs:28:22 - | -LL | let ref _a = X; - | ^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let ref _a = addr_of!(X); - | ~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static.rs:32:25 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let (_b, _c) = (addr_of!(X), &Y); - | ~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static.rs:32:29 - | -LL | let (_b, _c) = (&X, &Y); - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | let (_b, _c) = (&X, addr_of!(Y)); - | ~~~~~~~~~~~ - -error[E0796]: reference of mutable static is disallowed - --> $DIR/reference-of-mut-static.rs:38:13 - | -LL | foo(&X); - | ^^ reference of mutable static - | - = note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior -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 | foo(addr_of!(X)); - | ~~~~~~~~~~~ - -error: aborting due to 6 previous errors - -For more information about this error, try `rustc --explain E0796`. diff --git a/tests/ui/static/reference-of-mut-static.rs b/tests/ui/static/reference-of-mut-static.rs deleted file mode 100644 index 01a3b1fbd9b51..0000000000000 --- a/tests/ui/static/reference-of-mut-static.rs +++ /dev/null @@ -1,50 +0,0 @@ -// revisions: e2021 e2024 - -// [e2021] edition:2021 -// [e2024] compile-flags: --edition 2024 -Z unstable-options - -#![deny(static_mut_ref)] - -use std::ptr::{addr_of, addr_of_mut}; - -fn main() { - static mut X: i32 = 1; - - static mut Y: i32 = 1; - - unsafe { - let _y = &X; - //[e2024]~^ ERROR reference of mutable static is disallowed - //[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref] - - let _y = &mut X; - //[e2024]~^ ERROR reference of mutable static is disallowed - //[e2021]~^^ ERROR mutable reference of mutable static is discouraged [static_mut_ref] - - let _z = addr_of_mut!(X); - - let _p = addr_of!(X); - - let ref _a = X; - //[e2024]~^ ERROR reference of mutable static is disallowed - //[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref] - - let (_b, _c) = (&X, &Y); - //[e2024]~^ ERROR reference of mutable static is disallowed - //[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref] - //[e2024]~^^^ ERROR reference of mutable static is disallowed - //[e2021]~^^^^ ERROR shared reference of mutable static is discouraged [static_mut_ref] - - foo(&X); - //[e2024]~^ ERROR reference of mutable static is disallowed - //[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref] - - static mut Z: &[i32; 3] = &[0, 1, 2]; - - let _ = Z.len(); - let _ = Z[0]; - let _ = format!("{:?}", Z); - } -} - -fn foo<'a>(_x: &'a i32) {} diff --git a/tests/ui/static/reference-to-mut-static-safe.e2021.stderr b/tests/ui/static/reference-to-mut-static-safe.e2021.stderr new file mode 100644 index 0000000000000..9ea34290e36d6 --- /dev/null +++ b/tests/ui/static/reference-to-mut-static-safe.e2021.stderr @@ -0,0 +1,26 @@ +warning: creating a shared reference to mutable static is discouraged + --> $DIR/reference-to-mut-static-safe.rs:9:14 + | +LL | let _x = &X; + | ^^ shared reference to mutable static + | + = note: for more information, see issue #114447 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of!` instead to create a raw pointer + | +LL | let _x = addr_of!(X); + | ~~~~~~~~~~~ + +error[E0133]: use of mutable static is unsafe and requires unsafe function or block + --> $DIR/reference-to-mut-static-safe.rs:9:15 + | +LL | let _x = &X; + | ^ use of mutable static + | + = note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior + +error: aborting due to 1 previous error; 1 warning emitted + +For more information about this error, try `rustc --explain E0133`. diff --git a/tests/ui/static/reference-to-mut-static-safe.e2024.stderr b/tests/ui/static/reference-to-mut-static-safe.e2024.stderr new file mode 100644 index 0000000000000..39575441422d4 --- /dev/null +++ b/tests/ui/static/reference-to-mut-static-safe.e2024.stderr @@ -0,0 +1,15 @@ +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static-safe.rs:9:14 + | +LL | let _x = &X; + | ^^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let _x = addr_of!(X); + | ~~~~~~~~~~~ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0796`. diff --git a/tests/ui/static/reference-of-mut-static-safe.rs b/tests/ui/static/reference-to-mut-static-safe.rs similarity index 62% rename from tests/ui/static/reference-of-mut-static-safe.rs rename to tests/ui/static/reference-to-mut-static-safe.rs index 5cb1a03bef512..cc3ef63a2ace3 100644 --- a/tests/ui/static/reference-of-mut-static-safe.rs +++ b/tests/ui/static/reference-to-mut-static-safe.rs @@ -7,7 +7,7 @@ fn main() { static mut X: i32 = 1; let _x = &X; - //[e2024]~^ reference of mutable static is disallowed [E0796] + //[e2024]~^ creating a shared reference to a mutable static [E0796] //[e2021]~^^ use of mutable static is unsafe and requires unsafe function or block [E0133] - //[e2021]~^^^ shared reference of mutable static is discouraged [static_mut_ref] + //[e2021]~^^^ shared reference to mutable static is discouraged [static_mut_refs] } diff --git a/tests/ui/static/reference-to-mut-static-unsafe-fn.rs b/tests/ui/static/reference-to-mut-static-unsafe-fn.rs new file mode 100644 index 0000000000000..737fcb996af18 --- /dev/null +++ b/tests/ui/static/reference-to-mut-static-unsafe-fn.rs @@ -0,0 +1,26 @@ +// compile-flags: --edition 2024 -Z unstable-options + +fn main() {} + +unsafe fn _foo() { + static mut X: i32 = 1; + static mut Y: i32 = 1; + + let _y = &X; + //~^ ERROR creating a shared reference to a mutable static [E0796] + + let ref _a = X; + //~^ ERROR creating a shared reference to a mutable static [E0796] + + let ref mut _a = X; + //~^ ERROR creating a mutable reference to a mutable static [E0796] + + let (_b, _c) = (&X, &mut Y); + //~^ ERROR creating a shared reference to a mutable static [E0796] + //~^^ ERROR creating a mutable reference to a mutable static [E0796] + + foo(&X); + //~^ ERROR creating a shared reference to a mutable static [E0796] +} + +fn foo<'a>(_x: &'a i32) {} diff --git a/tests/ui/static/reference-to-mut-static-unsafe-fn.stderr b/tests/ui/static/reference-to-mut-static-unsafe-fn.stderr new file mode 100644 index 0000000000000..439a205a5f415 --- /dev/null +++ b/tests/ui/static/reference-to-mut-static-unsafe-fn.stderr @@ -0,0 +1,75 @@ +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static-unsafe-fn.rs:9:14 + | +LL | let _y = &X; + | ^^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let _y = addr_of!(X); + | ~~~~~~~~~~~ + +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static-unsafe-fn.rs:12:18 + | +LL | let ref _a = X; + | ^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let ref _a = addr_of!(X); + | ~~~~~~~~~~~ + +error[E0796]: creating a mutable reference to a mutable static + --> $DIR/reference-to-mut-static-unsafe-fn.rs:15:22 + | +LL | let ref mut _a = X; + | ^ mutablereference to mutable static + | + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior +help: use `addr_of_mut!` instead to create a raw pointer + | +LL | let ref mut _a = addr_of_mut!(X); + | ~~~~~~~~~~~~~~~ + +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static-unsafe-fn.rs:18:21 + | +LL | let (_b, _c) = (&X, &mut Y); + | ^^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let (_b, _c) = (addr_of!(X), &mut Y); + | ~~~~~~~~~~~ + +error[E0796]: creating a mutable reference to a mutable static + --> $DIR/reference-to-mut-static-unsafe-fn.rs:18:25 + | +LL | let (_b, _c) = (&X, &mut Y); + | ^^^^^^ mutablereference to mutable static + | + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior +help: use `addr_of_mut!` instead to create a raw pointer + | +LL | let (_b, _c) = (&X, addr_of_mut!(Y)); + | ~~~~~~~~~~~~~~~ + +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static-unsafe-fn.rs:22:9 + | +LL | foo(&X); + | ^^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | foo(addr_of!(X)); + | ~~~~~~~~~~~ + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0796`. diff --git a/tests/ui/static/reference-to-mut-static.e2021.stderr b/tests/ui/static/reference-to-mut-static.e2021.stderr new file mode 100644 index 0000000000000..f477e5ac6c5c2 --- /dev/null +++ b/tests/ui/static/reference-to-mut-static.e2021.stderr @@ -0,0 +1,91 @@ +error: creating a shared reference to mutable static is discouraged + --> $DIR/reference-to-mut-static.rs:16:18 + | +LL | let _y = &X; + | ^^ shared reference to mutable static + | + = note: for more information, see issue #114447 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +note: the lint level is defined here + --> $DIR/reference-to-mut-static.rs:6:9 + | +LL | #![deny(static_mut_refs)] + | ^^^^^^^^^^^^^^^ +help: use `addr_of!` instead to create a raw pointer + | +LL | let _y = addr_of!(X); + | ~~~~~~~~~~~ + +error: creating a mutable reference to mutable static is discouraged + --> $DIR/reference-to-mut-static.rs:20:18 + | +LL | let _y = &mut X; + | ^^^^^^ mutable reference to mutable static + | + = note: for more information, see issue #114447 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior +help: use `addr_of_mut!` instead to create a raw pointer + | +LL | let _y = addr_of_mut!(X); + | ~~~~~~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/reference-to-mut-static.rs:28:22 + | +LL | let ref _a = X; + | ^ shared reference to mutable static + | + = note: for more information, see issue #114447 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let ref _a = addr_of!(X); + | ~~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/reference-to-mut-static.rs:32:25 + | +LL | let (_b, _c) = (&X, &Y); + | ^^ shared reference to mutable static + | + = note: for more information, see issue #114447 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let (_b, _c) = (addr_of!(X), &Y); + | ~~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/reference-to-mut-static.rs:32:29 + | +LL | let (_b, _c) = (&X, &Y); + | ^^ shared reference to mutable static + | + = note: for more information, see issue #114447 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let (_b, _c) = (&X, addr_of!(Y)); + | ~~~~~~~~~~~ + +error: creating a shared reference to mutable static is discouraged + --> $DIR/reference-to-mut-static.rs:38:13 + | +LL | foo(&X); + | ^^ shared reference to mutable static + | + = note: for more information, see issue #114447 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | foo(addr_of!(X)); + | ~~~~~~~~~~~ + +error: aborting due to 6 previous errors + diff --git a/tests/ui/static/reference-to-mut-static.e2024.stderr b/tests/ui/static/reference-to-mut-static.e2024.stderr new file mode 100644 index 0000000000000..12af318572d91 --- /dev/null +++ b/tests/ui/static/reference-to-mut-static.e2024.stderr @@ -0,0 +1,75 @@ +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static.rs:16:18 + | +LL | let _y = &X; + | ^^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let _y = addr_of!(X); + | ~~~~~~~~~~~ + +error[E0796]: creating a mutable reference to a mutable static + --> $DIR/reference-to-mut-static.rs:20:18 + | +LL | let _y = &mut X; + | ^^^^^^ mutablereference to mutable static + | + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior +help: use `addr_of_mut!` instead to create a raw pointer + | +LL | let _y = addr_of_mut!(X); + | ~~~~~~~~~~~~~~~ + +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static.rs:28:22 + | +LL | let ref _a = X; + | ^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let ref _a = addr_of!(X); + | ~~~~~~~~~~~ + +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static.rs:32:25 + | +LL | let (_b, _c) = (&X, &Y); + | ^^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let (_b, _c) = (addr_of!(X), &Y); + | ~~~~~~~~~~~ + +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static.rs:32:29 + | +LL | let (_b, _c) = (&X, &Y); + | ^^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | let (_b, _c) = (&X, addr_of!(Y)); + | ~~~~~~~~~~~ + +error[E0796]: creating a shared reference to a mutable static + --> $DIR/reference-to-mut-static.rs:38:13 + | +LL | foo(&X); + | ^^ sharedreference to mutable static + | + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer + | +LL | foo(addr_of!(X)); + | ~~~~~~~~~~~ + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0796`. diff --git a/tests/ui/static/reference-to-mut-static.rs b/tests/ui/static/reference-to-mut-static.rs new file mode 100644 index 0000000000000..b9aecd5185a09 --- /dev/null +++ b/tests/ui/static/reference-to-mut-static.rs @@ -0,0 +1,50 @@ +// revisions: e2021 e2024 + +// [e2021] edition:2021 +// [e2024] compile-flags: --edition 2024 -Z unstable-options + +#![deny(static_mut_refs)] + +use std::ptr::{addr_of, addr_of_mut}; + +fn main() { + static mut X: i32 = 1; + + static mut Y: i32 = 1; + + unsafe { + let _y = &X; + //[e2024]~^ ERROR creating a shared reference to a mutable static [E0796] + //[e2021]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + + let _y = &mut X; + //[e2024]~^ ERROR creating a mutable reference to a mutable static [E0796] + //[e2021]~^^ ERROR mutable reference to mutable static is discouraged [static_mut_refs] + + let _z = addr_of_mut!(X); + + let _p = addr_of!(X); + + let ref _a = X; + //[e2024]~^ ERROR creating a shared reference to a mutable static [E0796] + //[e2021]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + + let (_b, _c) = (&X, &Y); + //[e2024]~^ ERROR creating a shared reference to a mutable static [E0796] + //[e2021]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + //[e2024]~^^^ ERROR creating a shared reference to a mutable static [E0796] + //[e2021]~^^^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + + foo(&X); + //[e2024]~^ ERROR creating a shared reference to a mutable static [E0796] + //[e2021]~^^ ERROR shared reference to mutable static is discouraged [static_mut_refs] + + static mut Z: &[i32; 3] = &[0, 1, 2]; + + let _ = Z.len(); + let _ = Z[0]; + let _ = format!("{:?}", Z); + } +} + +fn foo<'a>(_x: &'a i32) {} diff --git a/tests/ui/static/safe-extern-statics-mut.rs b/tests/ui/static/safe-extern-statics-mut.rs index 1c0662e0a6cec..05cc6dd01917c 100644 --- a/tests/ui/static/safe-extern-statics-mut.rs +++ b/tests/ui/static/safe-extern-statics-mut.rs @@ -10,8 +10,8 @@ extern "C" { fn main() { let b = B; //~ ERROR use of mutable static is unsafe let rb = &B; //~ ERROR use of mutable static is unsafe - //~^ WARN shared reference of mutable static is discouraged [static_mut_ref] + //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] let xb = XB; //~ ERROR use of mutable static is unsafe let xrb = &XB; //~ ERROR use of mutable static is unsafe - //~^ WARN shared reference of mutable static is discouraged [static_mut_ref] + //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] } diff --git a/tests/ui/static/safe-extern-statics-mut.stderr b/tests/ui/static/safe-extern-statics-mut.stderr index eda353ce6736f..9a4b651405f23 100644 --- a/tests/ui/static/safe-extern-statics-mut.stderr +++ b/tests/ui/static/safe-extern-statics-mut.stderr @@ -1,28 +1,28 @@ -warning: shared reference of mutable static is discouraged +warning: creating a shared reference to mutable static is discouraged --> $DIR/safe-extern-statics-mut.rs:12:14 | LL | let rb = &B; - | ^^ shared reference of mutable static + | ^^ shared reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of!` instead to create a raw pointer | LL | let rb = addr_of!(B); | ~~~~~~~~~~~ -warning: shared reference of mutable static is discouraged +warning: creating a shared reference to mutable static is discouraged --> $DIR/safe-extern-statics-mut.rs:15:15 | LL | let xrb = &XB; - | ^^^ shared reference of mutable static + | ^^^ shared reference to mutable static | = note: for more information, see issue #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 -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 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior +help: use `addr_of!` instead to create a raw pointer | LL | let xrb = addr_of!(XB); | ~~~~~~~~~~~~ diff --git a/tests/ui/statics/issue-15261.rs b/tests/ui/statics/issue-15261.rs index 14422329b7dc8..a6f99bd13c605 100644 --- a/tests/ui/statics/issue-15261.rs +++ b/tests/ui/statics/issue-15261.rs @@ -7,6 +7,6 @@ static mut n_mut: usize = 0; static n: &'static usize = unsafe { &n_mut }; -//~^ WARN shared reference of mutable static is discouraged [static_mut_ref] +//~^ WARN shared reference to mutable static is discouraged [static_mut_refs] fn main() {} diff --git a/tests/ui/statics/issue-15261.stderr b/tests/ui/statics/issue-15261.stderr index 72d88ce1b3832..c31793f3d8f15 100644 --- a/tests/ui/statics/issue-15261.stderr +++ b/tests/ui/statics/issue-15261.stderr @@ -1,14 +1,14 @@ -warning: shared reference of mutable static is discouraged +warning: creating a shared reference to mutable static is discouraged --> $DIR/issue-15261.rs:9:37 | LL | static n: &'static usize = unsafe { &n_mut }; - | ^^^^^^ shared reference of mutable static + | ^^^^^^ shared reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of!` instead to create a raw pointer | LL | static n: &'static usize = unsafe { addr_of!(n_mut) }; | ~~~~~~~~~~~~~~~ diff --git a/tests/ui/statics/static-mut-xc.rs b/tests/ui/statics/static-mut-xc.rs index 2fc265e02eaa3..f242836eccfbc 100644 --- a/tests/ui/statics/static-mut-xc.rs +++ b/tests/ui/statics/static-mut-xc.rs @@ -26,9 +26,9 @@ unsafe fn run() { static_mut_xc::a = -3; assert_eq!(static_mut_xc::a, -3); static_bound(&static_mut_xc::a); - //~^ WARN shared reference of mutable static is discouraged [static_mut_ref] + //~^ WARN shared reference to mutable static is discouraged [static_mut_refs] static_bound_set(&mut static_mut_xc::a); - //~^ WARN mutable reference of mutable static is discouraged [static_mut_ref] + //~^ WARN mutable reference to mutable static is discouraged [static_mut_refs] } pub fn main() { diff --git a/tests/ui/statics/static-mut-xc.stderr b/tests/ui/statics/static-mut-xc.stderr index 37aa336bc50f7..d381328c07178 100644 --- a/tests/ui/statics/static-mut-xc.stderr +++ b/tests/ui/statics/static-mut-xc.stderr @@ -1,28 +1,28 @@ -warning: shared reference of mutable static is discouraged +warning: creating a shared reference to mutable static is discouraged --> $DIR/static-mut-xc.rs:28:18 | LL | static_bound(&static_mut_xc::a); - | ^^^^^^^^^^^^^^^^^ shared reference of mutable static + | ^^^^^^^^^^^^^^^^^ shared reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of!` instead to create a raw pointer | LL | static_bound(addr_of!(static_mut_xc::a)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~ -warning: mutable reference of mutable static is discouraged +warning: creating a mutable reference to mutable static is discouraged --> $DIR/static-mut-xc.rs:30:22 | LL | static_bound_set(&mut static_mut_xc::a); - | ^^^^^^^^^^^^^^^^^^^^^ mutable reference of mutable static + | ^^^^^^^^^^^^^^^^^^^^^ mutable reference to mutable static | = note: for more information, see issue #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 -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 + = note: this will be a hard error in the 2024 edition + = note: this mutable reference has lifetime `'static`, but if the static gets accessed (read or written) by any other means, or any other reference is created, then any further use of this mutable reference is Undefined Behavior +help: use `addr_of_mut!` instead to create a raw pointer | LL | static_bound_set(addr_of_mut!(static_mut_xc::a)); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/ui/statics/static-recursive.rs b/tests/ui/statics/static-recursive.rs index 216beb0206d9c..52660888986da 100644 --- a/tests/ui/statics/static-recursive.rs +++ b/tests/ui/statics/static-recursive.rs @@ -1,7 +1,7 @@ // run-pass static mut S: *const u8 = unsafe { &S as *const *const u8 as *const u8 }; -//~^ WARN shared reference of mutable static is discouraged [static_mut_ref] +//~^ WARN shared reference to mutable static is discouraged [static_mut_refs] struct StaticDoubleLinked { prev: &'static StaticDoubleLinked, diff --git a/tests/ui/statics/static-recursive.stderr b/tests/ui/statics/static-recursive.stderr index 15888e5c68d84..cd285c6c2a463 100644 --- a/tests/ui/statics/static-recursive.stderr +++ b/tests/ui/statics/static-recursive.stderr @@ -1,14 +1,14 @@ -warning: shared reference of mutable static is discouraged +warning: creating a shared reference to mutable static is discouraged --> $DIR/static-recursive.rs:3:36 | LL | static mut S: *const u8 = unsafe { &S as *const *const u8 as *const u8 }; - | ^^ shared reference of mutable static + | ^^ shared reference to mutable static | = note: for more information, see issue #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: `#[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 + = note: this will be a hard error in the 2024 edition + = note: this shared reference has lifetime `'static`, but if the static ever gets mutated, or a mutable reference is created, then any further use of this shared reference is Undefined Behavior + = note: `#[warn(static_mut_refs)]` on by default +help: use `addr_of!` instead to create a raw pointer | LL | static mut S: *const u8 = unsafe { addr_of!(S) as *const *const u8 as *const u8 }; | ~~~~~~~~~~~ diff --git a/tests/ui/thread-local/thread-local-static.rs b/tests/ui/thread-local/thread-local-static.rs index a2c1954881f89..3da1b80a5dd5b 100644 --- a/tests/ui/thread-local/thread-local-static.rs +++ b/tests/ui/thread-local/thread-local-static.rs @@ -2,7 +2,7 @@ #![feature(thread_local)] #![feature(const_swap)] -#![allow(static_mut_ref)] +#![allow(static_mut_refs)] #[thread_local] static mut STATIC_VAR_2: [u32; 8] = [4; 8];