mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-15 05:26:47 +00:00
Improve wording of static_mut_ref
Rename `static_mut_ref` lint to `static_mut_refs`.
This commit is contained in:
parent
eeeb021954
commit
408eeae59d
@ -112,8 +112,8 @@ fn start<T: Termination + 'static>(
|
||||
|
||||
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>() -> T {
|
||||
|
@ -99,8 +99,8 @@ fn start<T: Termination + 'static>(
|
||||
|
||||
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 {
|
||||
|
@ -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.
|
||||
|
@ -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 = 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_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_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
|
||||
|
||||
|
@ -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 },
|
||||
);
|
||||
}
|
||||
|
@ -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)]
|
||||
|
@ -325,6 +325,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
|
||||
|
@ -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 {
|
||||
|
@ -261,8 +261,9 @@ 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
|
||||
#[cfg_attr(bootstrap, allow(static_mut_ref))]
|
||||
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
|
||||
pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
|
||||
use core::intrinsics::atomic_store_seqcst;
|
||||
|
||||
@ -324,8 +325,9 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> 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
|
||||
#[cfg_attr(bootstrap, allow(static_mut_ref))]
|
||||
#[cfg_attr(not(bootstrap), allow(static_mut_refs))]
|
||||
pub unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send> {
|
||||
// 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.
|
||||
|
@ -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};
|
||||
|
@ -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> {
|
||||
|
@ -11,8 +11,9 @@ 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
|
||||
#[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> {
|
||||
|
@ -180,8 +180,8 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
|
||||
#[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)
|
||||
() => {};
|
||||
|
@ -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;
|
||||
|
@ -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)]
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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() {
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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));
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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);
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -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;
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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);
|
||||
| ~~~~~~~~~~~~~~~~~~
|
||||
|
@ -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 {
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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) }
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -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,));
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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));
|
||||
| ~~~~~~~~~~~~~~~
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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) };
|
||||
| ~~~~~~~~~~~~~~~~
|
||||
|
@ -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]+)?(<imm>)?─*╼ )+ *│.*" -> "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
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -1,4 +1,4 @@
|
||||
#![allow(static_mut_ref)]
|
||||
#![allow(static_mut_refs)]
|
||||
|
||||
const C1: &'static mut [usize] = &mut [];
|
||||
//~^ ERROR: mutable references are not allowed
|
||||
|
@ -3,7 +3,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]+)?(<imm>)?─*╼ )+ *│.*" -> "HEX_DUMP"
|
||||
#![feature(exclusive_range_pattern, half_open_range_patterns_in_slices)]
|
||||
#![allow(static_mut_ref)]
|
||||
#![allow(static_mut_refs)]
|
||||
|
||||
extern crate static_cross_crate;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -5,13 +5,13 @@ LL | const MUH: Meh = Meh {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:27:1
|
||||
--> $DIR/mutable_references_err.rs:28:1
|
||||
|
|
||||
LL | const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0080]: it is undefined behavior to use this value
|
||||
--> $DIR/mutable_references_err.rs:32:1
|
||||
--> $DIR/mutable_references_err.rs:33:1
|
||||
|
|
||||
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered mutable reference in a `const` or `static`
|
||||
@ -22,13 +22,13 @@ LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
|
||||
}
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:35:1
|
||||
--> $DIR/mutable_references_err.rs:36:1
|
||||
|
|
||||
LL | const BLUNT: &mut i32 = &mut 42;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0080]: it is undefined behavior to use this value
|
||||
--> $DIR/mutable_references_err.rs:40:1
|
||||
--> $DIR/mutable_references_err.rs:41:1
|
||||
|
|
||||
LL | static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const _ as *mut _) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered mutable reference or box pointing to read-only memory
|
||||
@ -39,7 +39,7 @@ LL | static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const
|
||||
}
|
||||
|
||||
error[E0080]: it is undefined behavior to use this value
|
||||
--> $DIR/mutable_references_err.rs:47:1
|
||||
--> $DIR/mutable_references_err.rs:48:1
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered reference to mutable memory in `const`
|
||||
@ -50,49 +50,49 @@ LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
|
||||
}
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/mutable_references_err.rs:49:34
|
||||
--> $DIR/mutable_references_err.rs:50:34
|
||||
|
|
||||
LL | const READS_FROM_MUTABLE: i32 = *POINTS_TO_MUTABLE1;
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $DIR/mutable_references_err.rs:51:43
|
||||
--> $DIR/mutable_references_err.rs:52:43
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
|
||||
| ^^^^^^^^^^^^^ constant accesses mutable global memory
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:55:1
|
||||
--> $DIR/mutable_references_err.rs:56:1
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE_INNER: *const i32 = &mut 42 as *mut _ as *const _;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:57:1
|
||||
--> $DIR/mutable_references_err.rs:58:1
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE_INNER2: *const i32 = &mut 42 as *const _;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:59:1
|
||||
--> $DIR/mutable_references_err.rs:60:1
|
||||
|
|
||||
LL | const INTERIOR_MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:69:1
|
||||
--> $DIR/mutable_references_err.rs:72:1
|
||||
|
|
||||
LL | const RAW_SYNC: SyncPtr<AtomicI32> = SyncPtr { x: &AtomicI32::new(42) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:71:1
|
||||
--> $DIR/mutable_references_err.rs:74:1
|
||||
|
|
||||
LL | const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
|
||||
LL | const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x: &mut 42 as *mut _ as *const _ };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:73:1
|
||||
--> $DIR/mutable_references_err.rs:76:1
|
||||
|
|
||||
LL | const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -100,77 +100,77 @@ LL | const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
|
||||
warning: skipping const checks
|
||||
|
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:18:8
|
||||
--> $DIR/mutable_references_err.rs:19:8
|
||||
|
|
||||
LL | x: &UnsafeCell::new(42),
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:27:27
|
||||
--> $DIR/mutable_references_err.rs:28:27
|
||||
|
|
||||
LL | const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check for `const_refs_to_static` feature
|
||||
--> $DIR/mutable_references_err.rs:32:40
|
||||
--> $DIR/mutable_references_err.rs:33:40
|
||||
|
|
||||
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
|
||||
| ^^^
|
||||
help: skipping check for `const_mut_refs` feature
|
||||
--> $DIR/mutable_references_err.rs:32:35
|
||||
--> $DIR/mutable_references_err.rs:33:35
|
||||
|
|
||||
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
|
||||
| ^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:35:25
|
||||
--> $DIR/mutable_references_err.rs:36:25
|
||||
|
|
||||
LL | const BLUNT: &mut i32 = &mut 42;
|
||||
| ^^^^^^^
|
||||
help: skipping check for `const_mut_refs` feature
|
||||
--> $DIR/mutable_references_err.rs:40:49
|
||||
--> $DIR/mutable_references_err.rs:41:49
|
||||
|
|
||||
LL | static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const _ as *mut _) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check for `const_mut_refs` feature
|
||||
--> $DIR/mutable_references_err.rs:40:49
|
||||
--> $DIR/mutable_references_err.rs:41:49
|
||||
|
|
||||
LL | static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const _ as *mut _) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check for `const_refs_to_static` feature
|
||||
--> $DIR/mutable_references_err.rs:47:44
|
||||
--> $DIR/mutable_references_err.rs:48:44
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
|
||||
| ^^^^^^^
|
||||
help: skipping check for `const_refs_to_static` feature
|
||||
--> $DIR/mutable_references_err.rs:51:45
|
||||
--> $DIR/mutable_references_err.rs:52:45
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
|
||||
| ^^^^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:55:45
|
||||
--> $DIR/mutable_references_err.rs:56:45
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE_INNER: *const i32 = &mut 42 as *mut _ as *const _;
|
||||
| ^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:57:46
|
||||
--> $DIR/mutable_references_err.rs:58:46
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE_INNER2: *const i32 = &mut 42 as *const _;
|
||||
| ^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:59:47
|
||||
--> $DIR/mutable_references_err.rs:60:47
|
||||
|
|
||||
LL | const INTERIOR_MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:69:51
|
||||
--> $DIR/mutable_references_err.rs:72:51
|
||||
|
|
||||
LL | const RAW_SYNC: SyncPtr<AtomicI32> = SyncPtr { x: &AtomicI32::new(42) };
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:71:50
|
||||
--> $DIR/mutable_references_err.rs:74:49
|
||||
|
|
||||
LL | const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
|
||||
| ^^^^^^^
|
||||
LL | const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x: &mut 42 as *mut _ as *const _ };
|
||||
| ^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:73:51
|
||||
--> $DIR/mutable_references_err.rs:76:51
|
||||
|
|
||||
LL | const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
|
||||
| ^^^^^^
|
||||
|
@ -5,13 +5,13 @@ LL | const MUH: Meh = Meh {
|
||||
| ^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:27:1
|
||||
--> $DIR/mutable_references_err.rs:28:1
|
||||
|
|
||||
LL | const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0080]: it is undefined behavior to use this value
|
||||
--> $DIR/mutable_references_err.rs:32:1
|
||||
--> $DIR/mutable_references_err.rs:33:1
|
||||
|
|
||||
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered mutable reference in a `const` or `static`
|
||||
@ -22,13 +22,13 @@ LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
|
||||
}
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:35:1
|
||||
--> $DIR/mutable_references_err.rs:36:1
|
||||
|
|
||||
LL | const BLUNT: &mut i32 = &mut 42;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0080]: it is undefined behavior to use this value
|
||||
--> $DIR/mutable_references_err.rs:40:1
|
||||
--> $DIR/mutable_references_err.rs:41:1
|
||||
|
|
||||
LL | static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const _ as *mut _) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered mutable reference or box pointing to read-only memory
|
||||
@ -39,7 +39,7 @@ LL | static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const
|
||||
}
|
||||
|
||||
error[E0080]: it is undefined behavior to use this value
|
||||
--> $DIR/mutable_references_err.rs:47:1
|
||||
--> $DIR/mutable_references_err.rs:48:1
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered reference to mutable memory in `const`
|
||||
@ -50,49 +50,49 @@ LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
|
||||
}
|
||||
|
||||
note: erroneous constant encountered
|
||||
--> $DIR/mutable_references_err.rs:49:34
|
||||
--> $DIR/mutable_references_err.rs:50:34
|
||||
|
|
||||
LL | const READS_FROM_MUTABLE: i32 = *POINTS_TO_MUTABLE1;
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0080]: evaluation of constant value failed
|
||||
--> $DIR/mutable_references_err.rs:51:43
|
||||
--> $DIR/mutable_references_err.rs:52:43
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
|
||||
| ^^^^^^^^^^^^^ constant accesses mutable global memory
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:55:1
|
||||
--> $DIR/mutable_references_err.rs:56:1
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE_INNER: *const i32 = &mut 42 as *mut _ as *const _;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:57:1
|
||||
--> $DIR/mutable_references_err.rs:58:1
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE_INNER2: *const i32 = &mut 42 as *const _;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:59:1
|
||||
--> $DIR/mutable_references_err.rs:60:1
|
||||
|
|
||||
LL | const INTERIOR_MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:69:1
|
||||
--> $DIR/mutable_references_err.rs:72:1
|
||||
|
|
||||
LL | const RAW_SYNC: SyncPtr<AtomicI32> = SyncPtr { x: &AtomicI32::new(42) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:71:1
|
||||
--> $DIR/mutable_references_err.rs:74:1
|
||||
|
|
||||
LL | const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
|
||||
LL | const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x: &mut 42 as *mut _ as *const _ };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
error: encountered mutable pointer in final value of constant
|
||||
--> $DIR/mutable_references_err.rs:73:1
|
||||
--> $DIR/mutable_references_err.rs:76:1
|
||||
|
|
||||
LL | const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
@ -100,77 +100,77 @@ LL | const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
|
||||
warning: skipping const checks
|
||||
|
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:18:8
|
||||
--> $DIR/mutable_references_err.rs:19:8
|
||||
|
|
||||
LL | x: &UnsafeCell::new(42),
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:27:27
|
||||
--> $DIR/mutable_references_err.rs:28:27
|
||||
|
|
||||
LL | const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check for `const_refs_to_static` feature
|
||||
--> $DIR/mutable_references_err.rs:32:40
|
||||
--> $DIR/mutable_references_err.rs:33:40
|
||||
|
|
||||
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
|
||||
| ^^^
|
||||
help: skipping check for `const_mut_refs` feature
|
||||
--> $DIR/mutable_references_err.rs:32:35
|
||||
--> $DIR/mutable_references_err.rs:33:35
|
||||
|
|
||||
LL | const SUBTLE: &mut i32 = unsafe { &mut FOO };
|
||||
| ^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:35:25
|
||||
--> $DIR/mutable_references_err.rs:36:25
|
||||
|
|
||||
LL | const BLUNT: &mut i32 = &mut 42;
|
||||
| ^^^^^^^
|
||||
help: skipping check for `const_mut_refs` feature
|
||||
--> $DIR/mutable_references_err.rs:40:49
|
||||
--> $DIR/mutable_references_err.rs:41:49
|
||||
|
|
||||
LL | static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const _ as *mut _) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check for `const_mut_refs` feature
|
||||
--> $DIR/mutable_references_err.rs:40:49
|
||||
--> $DIR/mutable_references_err.rs:41:49
|
||||
|
|
||||
LL | static mut MUT_TO_READONLY: &mut i32 = unsafe { &mut *(&READONLY as *const _ as *mut _) };
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check for `const_refs_to_static` feature
|
||||
--> $DIR/mutable_references_err.rs:47:44
|
||||
--> $DIR/mutable_references_err.rs:48:44
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE1: &i32 = unsafe { &MUTABLE };
|
||||
| ^^^^^^^
|
||||
help: skipping check for `const_refs_to_static` feature
|
||||
--> $DIR/mutable_references_err.rs:51:45
|
||||
--> $DIR/mutable_references_err.rs:52:45
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE2: &i32 = unsafe { &*MUTABLE_REF };
|
||||
| ^^^^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:55:45
|
||||
--> $DIR/mutable_references_err.rs:56:45
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE_INNER: *const i32 = &mut 42 as *mut _ as *const _;
|
||||
| ^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:57:46
|
||||
--> $DIR/mutable_references_err.rs:58:46
|
||||
|
|
||||
LL | const POINTS_TO_MUTABLE_INNER2: *const i32 = &mut 42 as *const _;
|
||||
| ^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:59:47
|
||||
--> $DIR/mutable_references_err.rs:60:47
|
||||
|
|
||||
LL | const INTERIOR_MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
|
||||
| ^^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:69:51
|
||||
--> $DIR/mutable_references_err.rs:72:51
|
||||
|
|
||||
LL | const RAW_SYNC: SyncPtr<AtomicI32> = SyncPtr { x: &AtomicI32::new(42) };
|
||||
| ^^^^^^^^^^^^^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:71:50
|
||||
--> $DIR/mutable_references_err.rs:74:49
|
||||
|
|
||||
LL | const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
|
||||
| ^^^^^^^
|
||||
LL | const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x: &mut 42 as *mut _ as *const _ };
|
||||
| ^^^^^^^
|
||||
help: skipping check that does not even have a feature gate
|
||||
--> $DIR/mutable_references_err.rs:73:51
|
||||
--> $DIR/mutable_references_err.rs:76:51
|
||||
|
|
||||
LL | const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
|
||||
| ^^^^^^
|
||||
|
@ -1,9 +1,9 @@
|
||||
//@ 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;
|
||||
use std::sync::atomic::*;
|
||||
|
||||
// this test ensures that our mutability story is sound
|
||||
|
||||
@ -14,7 +14,8 @@ unsafe impl Sync for Meh {}
|
||||
|
||||
// the following will never be ok! no interior mut behind consts, because
|
||||
// all allocs interned here will be marked immutable.
|
||||
const MUH: Meh = Meh { //~ ERROR: mutable pointer in final value
|
||||
const MUH: Meh = Meh {
|
||||
//~^ ERROR encountered mutable pointer in final value of constant
|
||||
x: &UnsafeCell::new(42),
|
||||
};
|
||||
|
||||
@ -59,7 +60,9 @@ const POINTS_TO_MUTABLE_INNER2: *const i32 = &mut 42 as *const _;
|
||||
const INTERIOR_MUTABLE_BEHIND_RAW: *mut i32 = &UnsafeCell::new(42) as *const _ as *mut _;
|
||||
//~^ ERROR: mutable pointer in final value
|
||||
|
||||
struct SyncPtr<T> { x : *const T }
|
||||
struct SyncPtr<T> {
|
||||
x: *const T,
|
||||
}
|
||||
unsafe impl<T> Sync for SyncPtr<T> {}
|
||||
|
||||
// These pass the lifetime checks because of the "tail expression" / "outer scope" rule.
|
||||
@ -68,7 +71,7 @@ unsafe impl<T> Sync for SyncPtr<T> {}
|
||||
// (Also see `static-no-inner-mut` for similar tests on `static`.)
|
||||
const RAW_SYNC: SyncPtr<AtomicI32> = SyncPtr { x: &AtomicI32::new(42) };
|
||||
//~^ ERROR mutable pointer in final value
|
||||
const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
|
||||
const RAW_MUT_CAST: SyncPtr<i32> = SyncPtr { x: &mut 42 as *mut _ as *const _ };
|
||||
//~^ ERROR mutable pointer in final value
|
||||
const RAW_MUT_COERCE: SyncPtr<i32> = SyncPtr { x: &mut 0 };
|
||||
//~^ ERROR mutable pointer in final value
|
||||
|
@ -1,8 +1,9 @@
|
||||
//@ 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
|
||||
pub version: i32,
|
||||
}
|
||||
|
||||
pub struct Interface {
|
||||
@ -10,20 +11,14 @@ pub struct Interface {
|
||||
pub c_ptr: Option<&'static wl_interface>,
|
||||
}
|
||||
|
||||
pub static mut wl_callback_interface: wl_interface = wl_interface {
|
||||
version: 0,
|
||||
};
|
||||
pub static mut wl_callback_interface: wl_interface = wl_interface { version: 0 };
|
||||
|
||||
pub static WL_CALLBACK_INTERFACE: Interface = Interface {
|
||||
other_interfaces: &[],
|
||||
c_ptr: Some(unsafe { &wl_callback_interface }),
|
||||
};
|
||||
pub static WL_CALLBACK_INTERFACE: Interface =
|
||||
Interface { other_interfaces: &[], c_ptr: Some(unsafe { &wl_callback_interface }) };
|
||||
|
||||
// This static contains a promoted that points to a static that points to a mutable static.
|
||||
pub static WL_SURFACE_INTERFACE: Interface = Interface {
|
||||
other_interfaces: &[&WL_CALLBACK_INTERFACE],
|
||||
c_ptr: None,
|
||||
};
|
||||
pub static WL_SURFACE_INTERFACE: Interface =
|
||||
Interface { other_interfaces: &[&WL_CALLBACK_INTERFACE], c_ptr: None };
|
||||
|
||||
// And another variant of the same thing, this time with interior mutability.
|
||||
use std::sync::OnceLock;
|
||||
|
@ -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];
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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
|
||||
| ~~~~~~~~~~~~~~~~
|
||||
|
@ -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 };
|
||||
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
|
||||
//~^ WARN mutable reference to mutable static is discouraged [static_mut_refs]
|
||||
|
||||
fn main() {}
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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) };
|
||||
| ~~~~~~~~~~~~~~~
|
||||
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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
|
||||
| ~~~~~~~~~~~~~~~~
|
||||
|
@ -5,5 +5,5 @@ extern "C" {
|
||||
|
||||
fn main() {
|
||||
println!("{:p}", unsafe { &symbol });
|
||||
//~^ WARN: shared reference of mutable static is discouraged
|
||||
//~^ WARN creating a shared reference to mutable static is discouraged [static_mut_refs]
|
||||
}
|
||||
|
@ -6,17 +6,17 @@ LL | pub static mut symbol: [i8];
|
||||
|
|
||||
= help: the trait `Sized` is not implemented for `[i8]`
|
||||
|
||||
warning: shared reference of mutable static is discouraged
|
||||
warning: creating a shared reference to mutable static is discouraged
|
||||
--> $DIR/issue-54410.rs:7:31
|
||||
|
|
||||
LL | println!("{:p}", unsafe { &symbol });
|
||||
| ^^^^^^^ shared reference of mutable static
|
||||
| ^^^^^^^ shared reference to mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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 | println!("{:p}", unsafe { addr_of!(symbol) });
|
||||
| ~~~~~~~~~~~~~~~~
|
||||
|
@ -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]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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) } }
|
||||
| ~~~~~~~~~~~~~~~~
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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`.
|
@ -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`.
|
@ -7,17 +7,20 @@ unsafe fn _foo() {
|
||||
static mut Y: i32 = 1;
|
||||
|
||||
let _y = &X;
|
||||
//~^ ERROR reference of mutable static is disallowed
|
||||
//~^ ERROR creating a shared reference to a mutable static [E0796]
|
||||
|
||||
let ref _a = X;
|
||||
//~^ ERROR reference of mutable static is disallowed
|
||||
//~^ ERROR creating a shared reference to a mutable static [E0796]
|
||||
|
||||
let (_b, _c) = (&X, &Y);
|
||||
//~^ ERROR reference of mutable static is disallowed
|
||||
//~^^ ERROR reference of mutable static is disallowed
|
||||
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 reference of mutable static is disallowed
|
||||
//~^ ERROR creating a shared reference to a mutable static [E0796]
|
||||
}
|
||||
|
||||
fn foo<'a>(_x: &'a i32) {}
|
||||
|
@ -1,63 +1,75 @@
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:9:14
|
||||
|
|
||||
LL | let _y = &X;
|
||||
| ^^ reference of mutable static
|
||||
| ^^ shared reference to 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
|
||||
= 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]: reference of mutable static is disallowed
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:12:18
|
||||
|
|
||||
LL | let ref _a = X;
|
||||
| ^ reference of mutable static
|
||||
| ^ shared reference to 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
|
||||
= 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]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:15:21
|
||||
error[E0796]: creating a mutable reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:15:22
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ reference of mutable static
|
||||
LL | let ref mut _a = X;
|
||||
| ^ mutable reference to 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
|
||||
= 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) = (addr_of!(X), &Y);
|
||||
LL | let ref mut _a = addr_of_mut!(X);
|
||||
| ~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:18:21
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &mut Y);
|
||||
| ^^ shared reference 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]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:15:25
|
||||
error[E0796]: creating a mutable reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:18:25
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ reference of mutable static
|
||||
LL | let (_b, _c) = (&X, &mut Y);
|
||||
| ^^^^^^ mutable reference to 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
|
||||
= 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!(Y));
|
||||
| ~~~~~~~~~~~
|
||||
LL | let (_b, _c) = (&X, addr_of_mut!(Y));
|
||||
| ~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:19:9
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:22:9
|
||||
|
|
||||
LL | foo(&X);
|
||||
| ^^ reference of mutable static
|
||||
| ^^ shared reference to 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
|
||||
= 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 5 previous errors
|
||||
error: aborting due to 6 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0796`.
|
||||
|
@ -1,88 +1,88 @@
|
||||
error: shared reference of mutable static is discouraged
|
||||
error: creating a shared reference to mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:16:18
|
||||
|
|
||||
LL | let _y = &X;
|
||||
| ^^ shared reference of mutable static
|
||||
| ^^ shared reference to mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: 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-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 | #![deny(static_mut_refs)]
|
||||
| ^^^^^^^^^^^^^^^
|
||||
help: use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let _y = addr_of!(X);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: mutable reference of mutable static is discouraged
|
||||
error: creating a mutable reference to mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:20:18
|
||||
|
|
||||
LL | let _y = &mut X;
|
||||
| ^^^^^^ mutable reference of mutable static
|
||||
| ^^^^^^ mutable reference to mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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 | let _y = addr_of_mut!(X);
|
||||
| ~~~~~~~~~~~~~~~
|
||||
|
||||
error: shared reference of mutable static is discouraged
|
||||
error: creating a shared reference to mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:28:22
|
||||
|
|
||||
LL | let ref _a = X;
|
||||
| ^ shared reference of mutable static
|
||||
| ^ shared reference to mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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 ref _a = addr_of!(X);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: shared reference of mutable static is discouraged
|
||||
error: creating a shared reference to mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:32:25
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ shared reference of mutable static
|
||||
| ^^ shared reference to mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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 (_b, _c) = (addr_of!(X), &Y);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: shared reference of mutable static is discouraged
|
||||
error: creating a shared reference to mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:32:29
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ shared reference of mutable static
|
||||
| ^^ shared reference to mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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 (_b, _c) = (&X, addr_of!(Y));
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: shared reference of mutable static is discouraged
|
||||
error: creating a shared reference to mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:38:13
|
||||
|
|
||||
LL | foo(&X);
|
||||
| ^^ shared reference of mutable static
|
||||
| ^^ shared reference to mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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 | foo(addr_of!(X));
|
||||
| ~~~~~~~~~~~
|
||||
|
@ -1,71 +1,71 @@
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static.rs:16:18
|
||||
|
|
||||
LL | let _y = &X;
|
||||
| ^^ reference of mutable static
|
||||
| ^^ shared reference to 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
|
||||
= 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]: reference of mutable static is disallowed
|
||||
error[E0796]: creating a mutable reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static.rs:20:18
|
||||
|
|
||||
LL | let _y = &mut X;
|
||||
| ^^^^^^ reference of mutable static
|
||||
| ^^^^^^ mutable reference to 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
|
||||
= 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]: reference of mutable static is disallowed
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static.rs:28:22
|
||||
|
|
||||
LL | let ref _a = X;
|
||||
| ^ reference of mutable static
|
||||
| ^ shared reference to 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
|
||||
= 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]: reference of mutable static is disallowed
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static.rs:32:25
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ reference of mutable static
|
||||
| ^^ shared reference to 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
|
||||
= 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]: reference of mutable static is disallowed
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static.rs:32:29
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ reference of mutable static
|
||||
| ^^ shared reference to 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
|
||||
= 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]: reference of mutable static is disallowed
|
||||
error[E0796]: creating a shared reference to a mutable static
|
||||
--> $DIR/reference-of-mut-static.rs:38:13
|
||||
|
|
||||
LL | foo(&X);
|
||||
| ^^ reference of mutable static
|
||||
| ^^ shared reference to 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
|
||||
= 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));
|
||||
| ~~~~~~~~~~~
|
||||
|
@ -3,7 +3,7 @@
|
||||
//@ [e2021] edition:2021
|
||||
//@ [e2024] compile-flags: --edition 2024 -Z unstable-options
|
||||
|
||||
#![deny(static_mut_ref)]
|
||||
#![deny(static_mut_refs)]
|
||||
|
||||
use std::ptr::{addr_of, addr_of_mut};
|
||||
|
||||
@ -14,30 +14,30 @@ fn main() {
|
||||
|
||||
unsafe {
|
||||
let _y = &X;
|
||||
//[e2024]~^ ERROR reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
|
||||
//[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 reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR mutable reference of mutable static is discouraged [static_mut_ref]
|
||||
//[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 reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
|
||||
//[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 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]
|
||||
//[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 reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
|
||||
//[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];
|
||||
|
||||
|
26
tests/ui/static/reference-to-mut-static-safe.e2021.stderr
Normal file
26
tests/ui/static/reference-to-mut-static-safe.e2021.stderr
Normal file
@ -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 <https://github.com/rust-lang/rust/issues/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`.
|
15
tests/ui/static/reference-to-mut-static-safe.e2024.stderr
Normal file
15
tests/ui/static/reference-to-mut-static-safe.e2024.stderr
Normal file
@ -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;
|
||||
| ^^ shared reference 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`.
|
@ -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]
|
||||
}
|
26
tests/ui/static/reference-to-mut-static-unsafe-fn.rs
Normal file
26
tests/ui/static/reference-to-mut-static-unsafe-fn.rs
Normal file
@ -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) {}
|
75
tests/ui/static/reference-to-mut-static-unsafe-fn.stderr
Normal file
75
tests/ui/static/reference-to-mut-static-unsafe-fn.stderr
Normal file
@ -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;
|
||||
| ^^ shared reference 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;
|
||||
| ^ shared reference 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;
|
||||
| ^ mutable reference 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);
|
||||
| ^^ shared reference 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);
|
||||
| ^^^^^^ mutable reference 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);
|
||||
| ^^ shared reference 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`.
|
91
tests/ui/static/reference-to-mut-static.e2021.stderr
Normal file
91
tests/ui/static/reference-to-mut-static.e2021.stderr
Normal file
@ -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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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 <https://github.com/rust-lang/rust/issues/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
|
||||
|
75
tests/ui/static/reference-to-mut-static.e2024.stderr
Normal file
75
tests/ui/static/reference-to-mut-static.e2024.stderr
Normal file
@ -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;
|
||||
| ^^ shared reference 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;
|
||||
| ^^^^^^ mutable reference 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;
|
||||
| ^ shared reference 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);
|
||||
| ^^ shared reference 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);
|
||||
| ^^ shared reference 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);
|
||||
| ^^ shared reference 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`.
|
50
tests/ui/static/reference-to-mut-static.rs
Normal file
50
tests/ui/static/reference-to-mut-static.rs
Normal file
@ -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) {}
|
@ -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]
|
||||
}
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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);
|
||||
| ~~~~~~~~~~~~
|
||||
|
@ -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() {}
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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) };
|
||||
| ~~~~~~~~~~~~~~~
|
||||
|
@ -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() {
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
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));
|
||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
@ -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,
|
||||
|
@ -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 <https://github.com/rust-lang/rust/issues/114447>
|
||||
= note: reference of mutable static is a hard error from 2024 edition
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
= note: `#[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 };
|
||||
| ~~~~~~~~~~~
|
||||
|
@ -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];
|
||||
|
Loading…
Reference in New Issue
Block a user