Skip to content

Commit a8aa687

Browse files
committed
Update test for E0796 and static_mut_ref lint
1 parent 18edf9a commit a8aa687

File tree

54 files changed

+805
-237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+805
-237
lines changed

compiler/rustc_codegen_cranelift/example/mini_core_hello_world.rs

+3
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ fn start<T: Termination + 'static>(
111111
}
112112

113113
static mut NUM: u8 = 6 * 7;
114+
115+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
116+
#[allow(static_mut_ref)]
114117
static NUM_REF: &'static u8 = unsafe { &NUM };
115118

116119
unsafe fn zeroed<T>() -> T {

compiler/rustc_codegen_gcc/example/mini_core_hello_world.rs

+3
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ fn start<T: Termination + 'static>(
9898
}
9999

100100
static mut NUM: u8 = 6 * 7;
101+
102+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
103+
#[allow(static_mut_ref)]
101104
static NUM_REF: &'static u8 = unsafe { &NUM };
102105

103106
macro_rules! assert {

library/panic_unwind/src/seh.rs

+4
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ cfg_if::cfg_if! {
261261
}
262262
}
263263

264+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
265+
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
264266
pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
265267
use core::intrinsics::atomic_store_seqcst;
266268

@@ -322,6 +324,8 @@ pub unsafe fn panic(data: Box<dyn Any + Send>) -> u32 {
322324
_CxxThrowException(throw_ptr, &mut THROW_INFO as *mut _ as *mut _);
323325
}
324326

327+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
328+
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
325329
pub unsafe fn cleanup(payload: *mut u8) -> Box<dyn Any + Send> {
326330
// A null payload here means that we got here from the catch (...) of
327331
// __rust_try. This happens when a non-Rust foreign exception is caught.

library/std/src/panicking.rs

+2
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,8 @@ pub mod panic_count {
337337
#[doc(hidden)]
338338
#[cfg(not(feature = "panic_immediate_abort"))]
339339
#[unstable(feature = "update_panic_count", issue = "none")]
340+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
341+
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
340342
pub mod panic_count {
341343
use crate::cell::Cell;
342344
use crate::sync::atomic::{AtomicUsize, Ordering};

library/std/src/sys/common/thread_local/fast_local.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub macro thread_local_inner {
1313
(@key $t:ty, const $init:expr) => {{
1414
#[inline]
1515
#[deny(unsafe_op_in_unsafe_fn)]
16+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
17+
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
1618
unsafe fn __getit(
1719
_init: $crate::option::Option<&mut $crate::option::Option<$t>>,
1820
) -> $crate::option::Option<&'static $t> {

library/std/src/sys/common/thread_local/static_local.rs

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub macro thread_local_inner {
1111
(@key $t:ty, const $init:expr) => {{
1212
#[inline] // see comments below
1313
#[deny(unsafe_op_in_unsafe_fn)]
14+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
15+
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
1416
unsafe fn __getit(
1517
_init: $crate::option::Option<&mut $crate::option::Option<$t>>,
1618
) -> $crate::option::Option<&'static $t> {

library/std/src/thread/local.rs

+2
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ impl<T: 'static> fmt::Debug for LocalKey<T> {
180180
#[stable(feature = "rust1", since = "1.0.0")]
181181
#[cfg_attr(not(test), rustc_diagnostic_item = "thread_local_macro")]
182182
#[allow_internal_unstable(thread_local_internals)]
183+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
184+
#[cfg_attr(not(bootstrap), allow(static_mut_ref))]
183185
macro_rules! thread_local {
184186
// empty (base case for the recursion)
185187
() => {};

src/tools/lint-docs/src/groups.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ static GROUP_DESCRIPTIONS: &[(&str, &str)] = &[
1515
("future-incompatible", "Lints that detect code that has future-compatibility problems"),
1616
("rust-2018-compatibility", "Lints used to transition code from the 2015 edition to 2018"),
1717
("rust-2021-compatibility", "Lints used to transition code from the 2018 edition to 2021"),
18+
("rust-2024-compatibility", "Lints used to transition code from the 2021 edition to 2024"),
1819
];
1920

2021
type LintGroups = BTreeMap<String, BTreeSet<String>>;

src/tools/miri/tests/fail/tls/tls_static_dealloc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Ensure that thread-local statics get deallocated when the thread dies.
22
33
#![feature(thread_local)]
4+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
5+
#![allow(static_mut_ref)]
46

57
#[thread_local]
68
static mut TLS: u8 = 0;

src/tools/miri/tests/pass/static_mut.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
static mut FOO: i32 = 42;
2+
3+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
4+
#[allow(static_mut_ref)]
25
static BAR: Foo = Foo(unsafe { &FOO as *const _ });
36

47
#[allow(dead_code)]

src/tools/miri/tests/pass/tls/tls_static.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
//! test, we also check that thread-locals act as per-thread statics.
99
1010
#![feature(thread_local)]
11+
// FIXME: Use `SyncUnsafeCell` instead of allowing `static_mut_ref` lint
12+
#![allow(static_mut_ref)]
1113

1214
use std::thread;
1315

tests/ui/abi/statics/static-mut-foreign.rs

+2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ unsafe fn run() {
3333
rust_dbg_static_mut = -3;
3434
assert_eq!(rust_dbg_static_mut, -3);
3535
static_bound(&rust_dbg_static_mut);
36+
//~^ WARN shared reference of mutable static is discouraged [static_mut_ref]
3637
static_bound_set(&mut rust_dbg_static_mut);
38+
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
3739
}
3840

3941
pub fn main() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
warning: shared reference of mutable static is discouraged
2+
--> $DIR/static-mut-foreign.rs:35:18
3+
|
4+
LL | static_bound(&rust_dbg_static_mut);
5+
| ^^^^^^^^^^^^^^^^^^^^ shared reference of mutable static
6+
|
7+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
8+
= note: reference of mutable static is a hard error from 2024 edition
9+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
10+
= note: `#[warn(static_mut_ref)]` on by default
11+
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
12+
|
13+
LL | static_bound(addr_of!(rust_dbg_static_mut));
14+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15+
16+
warning: mutable reference of mutable static is discouraged
17+
--> $DIR/static-mut-foreign.rs:37:22
18+
|
19+
LL | static_bound_set(&mut rust_dbg_static_mut);
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^ mutable reference of mutable static
21+
|
22+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
23+
= note: reference of mutable static is a hard error from 2024 edition
24+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
25+
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
26+
|
27+
LL | static_bound_set(addr_of_mut!(rust_dbg_static_mut));
28+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29+
30+
warning: 2 warnings emitted
31+

tests/ui/borrowck/borrowck-access-permissions.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
1-
static static_x : i32 = 1;
2-
static mut static_x_mut : i32 = 1;
1+
static static_x: i32 = 1;
2+
static mut static_x_mut: i32 = 1;
33

44
fn main() {
55
let x = 1;
66
let mut x_mut = 1;
77

8-
{ // borrow of local
8+
{
9+
// borrow of local
910
let _y1 = &mut x; //~ ERROR [E0596]
1011
let _y2 = &mut x_mut; // No error
1112
}
1213

13-
{ // borrow of static
14+
{
15+
// borrow of static
1416
let _y1 = &mut static_x; //~ ERROR [E0596]
15-
unsafe { let _y2 = &mut static_x_mut; } // No error
17+
unsafe {
18+
let _y2 = &mut static_x_mut;
19+
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
20+
}
1621
}
1722

18-
{ // borrow of deref to box
23+
{
24+
// borrow of deref to box
1925
let box_x = Box::new(1);
2026
let mut box_x_mut = Box::new(1);
2127

2228
let _y1 = &mut *box_x; //~ ERROR [E0596]
2329
let _y2 = &mut *box_x_mut; // No error
2430
}
2531

26-
{ // borrow of deref to reference
32+
{
33+
// borrow of deref to reference
2734
let ref_x = &x;
2835
let ref_x_mut = &mut x_mut;
2936

3037
let _y1 = &mut *ref_x; //~ ERROR [E0596]
3138
let _y2 = &mut *ref_x_mut; // No error
3239
}
3340

34-
{ // borrow of deref to pointer
35-
let ptr_x : *const _ = &x;
36-
let ptr_mut_x : *mut _ = &mut x_mut;
41+
{
42+
// borrow of deref to pointer
43+
let ptr_x: *const _ = &x;
44+
let ptr_mut_x: *mut _ = &mut x_mut;
3745

3846
unsafe {
3947
let _y1 = &mut *ptr_x; //~ ERROR [E0596]
4048
let _y2 = &mut *ptr_mut_x; // No error
4149
}
4250
}
4351

44-
{ // borrowing mutably through an immutable reference
45-
struct Foo<'a> { f: &'a mut i32, g: &'a i32 };
52+
{
53+
// borrowing mutably through an immutable reference
54+
struct Foo<'a> {
55+
f: &'a mut i32,
56+
g: &'a i32,
57+
};
4658
let mut foo = Foo { f: &mut x_mut, g: &x };
4759
let foo_ref = &foo;
4860
let _y = &mut *foo_ref.f; //~ ERROR [E0596]

tests/ui/borrowck/borrowck-access-permissions.stderr

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1+
warning: mutable reference of mutable static is discouraged
2+
--> $DIR/borrowck-access-permissions.rs:18:23
3+
|
4+
LL | let _y2 = &mut static_x_mut;
5+
| ^^^^^^^^^^^^^^^^^ mutable reference of mutable static
6+
|
7+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
8+
= note: reference of mutable static is a hard error from 2024 edition
9+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
10+
= note: `#[warn(static_mut_ref)]` on by default
11+
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
12+
|
13+
LL | let _y2 = addr_of_mut!(static_x_mut);
14+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
15+
116
error[E0596]: cannot borrow `x` as mutable, as it is not declared as mutable
2-
--> $DIR/borrowck-access-permissions.rs:9:19
17+
--> $DIR/borrowck-access-permissions.rs:10:19
318
|
419
LL | let _y1 = &mut x;
520
| ^^^^^^ cannot borrow as mutable
@@ -10,13 +25,13 @@ LL | let mut x = 1;
1025
| +++
1126

1227
error[E0596]: cannot borrow immutable static item `static_x` as mutable
13-
--> $DIR/borrowck-access-permissions.rs:14:19
28+
--> $DIR/borrowck-access-permissions.rs:16:19
1429
|
1530
LL | let _y1 = &mut static_x;
1631
| ^^^^^^^^^^^^^ cannot borrow as mutable
1732

1833
error[E0596]: cannot borrow `*box_x` as mutable, as `box_x` is not declared as mutable
19-
--> $DIR/borrowck-access-permissions.rs:22:19
34+
--> $DIR/borrowck-access-permissions.rs:28:19
2035
|
2136
LL | let _y1 = &mut *box_x;
2237
| ^^^^^^^^^^^ cannot borrow as mutable
@@ -27,7 +42,7 @@ LL | let mut box_x = Box::new(1);
2742
| +++
2843

2944
error[E0596]: cannot borrow `*ref_x` as mutable, as it is behind a `&` reference
30-
--> $DIR/borrowck-access-permissions.rs:30:19
45+
--> $DIR/borrowck-access-permissions.rs:37:19
3146
|
3247
LL | let _y1 = &mut *ref_x;
3348
| ^^^^^^^^^^^ `ref_x` is a `&` reference, so the data it refers to cannot be borrowed as mutable
@@ -38,18 +53,18 @@ LL | let ref_x = &mut x;
3853
| +++
3954

4055
error[E0596]: cannot borrow `*ptr_x` as mutable, as it is behind a `*const` pointer
41-
--> $DIR/borrowck-access-permissions.rs:39:23
56+
--> $DIR/borrowck-access-permissions.rs:47:23
4257
|
4358
LL | let _y1 = &mut *ptr_x;
4459
| ^^^^^^^^^^^ `ptr_x` is a `*const` pointer, so the data it refers to cannot be borrowed as mutable
4560
|
4661
help: consider changing this to be a mutable pointer
4762
|
48-
LL | let ptr_x : *const _ = &mut x;
49-
| +++
63+
LL | let ptr_x: *const _ = &mut x;
64+
| +++
5065

5166
error[E0596]: cannot borrow `*foo_ref.f` as mutable, as it is behind a `&` reference
52-
--> $DIR/borrowck-access-permissions.rs:48:18
67+
--> $DIR/borrowck-access-permissions.rs:60:18
5368
|
5469
LL | let _y = &mut *foo_ref.f;
5570
| ^^^^^^^^^^^^^^^ `foo_ref` is a `&` reference, so the data it refers to cannot be borrowed as mutable
@@ -59,6 +74,6 @@ help: consider changing this to be a mutable reference
5974
LL | let foo_ref = &mut foo;
6075
| +++
6176

62-
error: aborting due to 6 previous errors
77+
error: aborting due to 6 previous errors; 1 warning emitted
6378

6479
For more information about this error, try `rustc --explain E0596`.

tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22

33
// Test file taken from issue 45129 (https://github.com/rust-lang/rust/issues/45129)
44

5-
struct Foo { x: [usize; 2] }
5+
struct Foo {
6+
x: [usize; 2],
7+
}
68

79
static mut SFOO: Foo = Foo { x: [23, 32] };
810

911
impl Foo {
10-
fn x(&mut self) -> &mut usize { &mut self.x[0] }
12+
fn x(&mut self) -> &mut usize {
13+
&mut self.x[0]
14+
}
1115
}
1216

1317
fn main() {
1418
unsafe {
1519
let sfoo: *mut Foo = &mut SFOO;
20+
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
1621
let x = (*sfoo).x();
1722
(*sfoo).x[1] += 1;
1823
*x += 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
warning: mutable reference of mutable static is discouraged
2+
--> $DIR/borrowck-unsafe-static-mutable-borrows.rs:19:30
3+
|
4+
LL | let sfoo: *mut Foo = &mut SFOO;
5+
| ^^^^^^^^^ mutable reference of mutable static
6+
|
7+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
8+
= note: reference of mutable static is a hard error from 2024 edition
9+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
10+
= note: `#[warn(static_mut_ref)]` on by default
11+
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
12+
|
13+
LL | let sfoo: *mut Foo = addr_of_mut!(SFOO);
14+
| ~~~~~~~~~~~~~~~~~~
15+
16+
warning: 1 warning emitted
17+

tests/ui/borrowck/issue-20801.rs

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ fn imm_ref() -> &'static T {
1212

1313
fn mut_ref() -> &'static mut T {
1414
unsafe { &mut GLOBAL_MUT_T }
15+
//~^ WARN mutable reference of mutable static is discouraged [static_mut_ref]
1516
}
1617

1718
fn mut_ptr() -> *mut T {

0 commit comments

Comments
 (0)