Skip to content

Commit b4f1f06

Browse files
committed
Add test for E0796 error
Add test for `static_mut_ref` lint
1 parent 83b2c95 commit b4f1f06

12 files changed

+220
-2
lines changed

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 use 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,16 @@
1+
warning: use 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+
| ^^^^^^^^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error from 2024 edition
8+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
9+
= note: `#[warn(static_mut_ref)]` on by default
10+
help: use `addr_of_mut!` macro
11+
|
12+
LL | let sfoo: *mut Foo = addr_of_mut!(SFOO);
13+
| ~~~~~~~~~~~~~~~~~~
14+
15+
warning: 1 warning emitted
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: use of mutable static is discouraged
2+
--> $DIR/use_of_mutable_static_unsafe_block.rs:18:18
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error from 2024 edition
8+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
9+
note: the lint level is defined here
10+
--> $DIR/use_of_mutable_static_unsafe_block.rs:8:9
11+
|
12+
LL | #![deny(static_mut_ref)]
13+
| ^^^^^^^^^^^^^^
14+
help: use `addr_of_mut!` macro
15+
|
16+
LL | let _y = addr_of_mut!(X);
17+
| ~~~~~~~~~~~~~~~
18+
19+
error: aborting due to previous error
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: use of mutable static is discouraged
2+
--> $DIR/use_of_mutable_static_unsafe_block.rs:18:18
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error from 2024 edition
8+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
9+
note: the lint level is defined here
10+
--> $DIR/use_of_mutable_static_unsafe_block.rs:8:9
11+
|
12+
LL | #![deny(static_mut_ref)]
13+
| ^^^^^^^^^^^^^^
14+
help: use `addr_of_mut!` macro
15+
|
16+
LL | let _y = addr_of_mut!(X);
17+
| ~~~~~~~~~~~~~~~
18+
19+
error: aborting due to previous error
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: use of mutable static is discouraged
2+
--> $DIR/use_of_mutable_static_unsafe_block.rs:18:18
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error from 2024 edition
8+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
9+
note: the lint level is defined here
10+
--> $DIR/use_of_mutable_static_unsafe_block.rs:8:9
11+
|
12+
LL | #![deny(static_mut_ref)]
13+
| ^^^^^^^^^^^^^^
14+
help: use `addr_of_mut!` macro
15+
|
16+
LL | let _y = addr_of_mut!(X);
17+
| ~~~~~~~~~~~~~~~
18+
19+
error: aborting due to previous error
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0796]: use of mutable static is disallowed
2+
--> $DIR/use_of_mutable_static_unsafe_block.rs:18:18
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
8+
help: use `addr_of_mut!` macro
9+
|
10+
LL | let _y = addr_of_mut!(X);
11+
| ~~~~~~~~~~~~~~~
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0796`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// revisions: e2015 e2018 e2021 e2024
2+
3+
// [e2015] edition:2015
4+
// [e2018] edition:2018
5+
// [e2021] edition:2021
6+
// [e2024] compile-flags: --edition 2024 -Zunstable-options
7+
8+
#![deny(static_mut_ref)]
9+
10+
use std::ptr::addr_of_mut;
11+
12+
fn main() {
13+
static mut X: i32 = 1;
14+
15+
static mut FOO: [u8; 3] = [1, 2, 3];
16+
17+
unsafe {
18+
let _y = &X;
19+
//[e2024]~^ ERROR use of mutable static is disallowed
20+
//[e2021]~^^ ERROR use of mutable static is discouraged [static_mut_ref]
21+
//[e2018]~^^^ ERROR use of mutable static is discouraged [static_mut_ref]
22+
//[e2015]~^^^^ ERROR use of mutable static is discouraged [static_mut_ref]
23+
24+
let _z = addr_of_mut!(X);
25+
26+
let _ = FOO.len();
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: use of mutable static is discouraged
2+
--> $DIR/use_of_mutable_static_unsafe_fn.rs:14:14
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error from 2024 edition
8+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
9+
note: the lint level is defined here
10+
--> $DIR/use_of_mutable_static_unsafe_fn.rs:8:9
11+
|
12+
LL | #![deny(static_mut_ref)]
13+
| ^^^^^^^^^^^^^^
14+
help: use `addr_of_mut!` macro
15+
|
16+
LL | let _y = addr_of_mut!(X);
17+
| ~~~~~~~~~~~~~~~
18+
19+
error: aborting due to previous error
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: use of mutable static is discouraged
2+
--> $DIR/use_of_mutable_static_unsafe_fn.rs:14:14
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error from 2024 edition
8+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
9+
note: the lint level is defined here
10+
--> $DIR/use_of_mutable_static_unsafe_fn.rs:8:9
11+
|
12+
LL | #![deny(static_mut_ref)]
13+
| ^^^^^^^^^^^^^^
14+
help: use `addr_of_mut!` macro
15+
|
16+
LL | let _y = addr_of_mut!(X);
17+
| ~~~~~~~~~~~~~~~
18+
19+
error: aborting due to previous error
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: use of mutable static is discouraged
2+
--> $DIR/use_of_mutable_static_unsafe_fn.rs:14:14
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: use of mutable static is a hard error from 2024 edition
8+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
9+
note: the lint level is defined here
10+
--> $DIR/use_of_mutable_static_unsafe_fn.rs:8:9
11+
|
12+
LL | #![deny(static_mut_ref)]
13+
| ^^^^^^^^^^^^^^
14+
help: use `addr_of_mut!` macro
15+
|
16+
LL | let _y = addr_of_mut!(X);
17+
| ~~~~~~~~~~~~~~~
18+
19+
error: aborting due to previous error
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0796]: use of mutable static is disallowed
2+
--> $DIR/use_of_mutable_static_unsafe_fn.rs:14:14
3+
|
4+
LL | let _y = &X;
5+
| ^^ use of mutable static
6+
|
7+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
8+
help: use `addr_of_mut!` macro
9+
|
10+
LL | let _y = addr_of_mut!(X);
11+
| ~~~~~~~~~~~~~~~
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0796`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// revisions: e2015 e2018 e2021 e2024
2+
3+
// [e2015] edition:2015
4+
// [e2018] edition:2018
5+
// [e2021] edition:2021
6+
// [e2024] compile-flags: --edition 2024 -Zunstable-options
7+
8+
#![deny(static_mut_ref)]
9+
10+
fn main() {}
11+
12+
unsafe fn _foo() {
13+
static mut X: i32 = 1;
14+
let _y = &X;
15+
//[e2024]~^ ERROR use of mutable static is disallowed
16+
//[e2021]~^^ ERROR use of mutable static is discouraged [static_mut_ref]
17+
//[e2018]~^^^ ERROR use of mutable static is discouraged [static_mut_ref]
18+
//[e2015]~^^^^ ERROR use of mutable static is discouraged [static_mut_ref]
19+
}

0 commit comments

Comments
 (0)