Skip to content

Commit 18edf9a

Browse files
committed
Add test for E0796 and static_mut_ref lint
1 parent e36f7b7 commit 18edf9a

8 files changed

+356
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
warning: shared reference of mutable static is discouraged
2+
--> $DIR/reference-of-mut-static-safe.rs:9:14
3+
|
4+
LL | let _x = &X;
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 | let _x = addr_of!(X);
14+
| ~~~~~~~~~~~
15+
16+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
17+
--> $DIR/reference-of-mut-static-safe.rs:9:14
18+
|
19+
LL | let _x = &X;
20+
| ^^ use of mutable static
21+
|
22+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
23+
24+
error: aborting due to 1 previous error; 1 warning emitted
25+
26+
For more information about this error, try `rustc --explain E0133`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0796]: reference of mutable static is disallowed
2+
--> $DIR/reference-of-mut-static-safe.rs:9:14
3+
|
4+
LL | let _x = &X;
5+
| ^^ reference 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: 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
9+
|
10+
LL | let _x = addr_of!(X);
11+
| ~~~~~~~~~~~
12+
13+
error: aborting due to 1 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,13 @@
1+
// revisions: e2021 e2024
2+
3+
// [e2021] edition:2021
4+
// [e2024] compile-flags: --edition 2024 -Z unstable-options
5+
6+
fn main() {
7+
static mut X: i32 = 1;
8+
9+
let _x = &X;
10+
//[e2024]~^ reference of mutable static is disallowed [E0796]
11+
//[e2021]~^^ use of mutable static is unsafe and requires unsafe function or block [E0133]
12+
//[e2021]~^^^ shared reference of mutable static is discouraged [static_mut_ref]
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// compile-flags: --edition 2024 -Z unstable-options
2+
3+
fn main() {}
4+
5+
unsafe fn _foo() {
6+
static mut X: i32 = 1;
7+
static mut Y: i32 = 1;
8+
9+
let _y = &X;
10+
//~^ ERROR reference of mutable static is disallowed
11+
12+
let ref _a = X;
13+
//~^ ERROR reference of mutable static is disallowed
14+
15+
let (_b, _c) = (&X, &Y);
16+
//~^ ERROR reference of mutable static is disallowed
17+
//~^^ ERROR reference of mutable static is disallowed
18+
19+
foo(&X);
20+
//~^ ERROR reference of mutable static is disallowed
21+
}
22+
23+
fn foo<'a>(_x: &'a i32) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
error[E0796]: reference of mutable static is disallowed
2+
--> $DIR/reference-of-mut-static-unsafe-fn.rs:9:14
3+
|
4+
LL | let _y = &X;
5+
| ^^ reference 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: 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
9+
|
10+
LL | let _y = addr_of!(X);
11+
| ~~~~~~~~~~~
12+
13+
error[E0796]: reference of mutable static is disallowed
14+
--> $DIR/reference-of-mut-static-unsafe-fn.rs:12:18
15+
|
16+
LL | let ref _a = X;
17+
| ^ reference of mutable static
18+
|
19+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
20+
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
21+
|
22+
LL | let ref _a = addr_of!(X);
23+
| ~~~~~~~~~~~
24+
25+
error[E0796]: reference of mutable static is disallowed
26+
--> $DIR/reference-of-mut-static-unsafe-fn.rs:15:21
27+
|
28+
LL | let (_b, _c) = (&X, &Y);
29+
| ^^ reference of mutable static
30+
|
31+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
32+
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
33+
|
34+
LL | let (_b, _c) = (addr_of!(X), &Y);
35+
| ~~~~~~~~~~~
36+
37+
error[E0796]: reference of mutable static is disallowed
38+
--> $DIR/reference-of-mut-static-unsafe-fn.rs:15:25
39+
|
40+
LL | let (_b, _c) = (&X, &Y);
41+
| ^^ reference of mutable static
42+
|
43+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
44+
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
45+
|
46+
LL | let (_b, _c) = (&X, addr_of!(Y));
47+
| ~~~~~~~~~~~
48+
49+
error[E0796]: reference of mutable static is disallowed
50+
--> $DIR/reference-of-mut-static-unsafe-fn.rs:19:9
51+
|
52+
LL | foo(&X);
53+
| ^^ reference of mutable static
54+
|
55+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
56+
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
57+
|
58+
LL | foo(addr_of!(X));
59+
| ~~~~~~~~~~~
60+
61+
error: aborting due to 5 previous errors
62+
63+
For more information about this error, try `rustc --explain E0796`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
error: shared reference of mutable static is discouraged
2+
--> $DIR/reference-of-mut-static.rs:16:18
3+
|
4+
LL | let _y = &X;
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: the lint level is defined here
11+
--> $DIR/reference-of-mut-static.rs:6:9
12+
|
13+
LL | #![deny(static_mut_ref)]
14+
| ^^^^^^^^^^^^^^
15+
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
16+
|
17+
LL | let _y = addr_of!(X);
18+
| ~~~~~~~~~~~
19+
20+
error: mutable reference of mutable static is discouraged
21+
--> $DIR/reference-of-mut-static.rs:20:18
22+
|
23+
LL | let _y = &mut X;
24+
| ^^^^^^ mutable reference of mutable static
25+
|
26+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
27+
= note: reference of mutable static is a hard error from 2024 edition
28+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
29+
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
30+
|
31+
LL | let _y = addr_of_mut!(X);
32+
| ~~~~~~~~~~~~~~~
33+
34+
error: shared reference of mutable static is discouraged
35+
--> $DIR/reference-of-mut-static.rs:28:22
36+
|
37+
LL | let ref _a = X;
38+
| ^ shared reference of mutable static
39+
|
40+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
41+
= note: reference of mutable static is a hard error from 2024 edition
42+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
43+
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
44+
|
45+
LL | let ref _a = addr_of!(X);
46+
| ~~~~~~~~~~~
47+
48+
error: shared reference of mutable static is discouraged
49+
--> $DIR/reference-of-mut-static.rs:32:25
50+
|
51+
LL | let (_b, _c) = (&X, &Y);
52+
| ^^ shared reference of mutable static
53+
|
54+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
55+
= note: reference of mutable static is a hard error from 2024 edition
56+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
57+
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
58+
|
59+
LL | let (_b, _c) = (addr_of!(X), &Y);
60+
| ~~~~~~~~~~~
61+
62+
error: shared reference of mutable static is discouraged
63+
--> $DIR/reference-of-mut-static.rs:32:29
64+
|
65+
LL | let (_b, _c) = (&X, &Y);
66+
| ^^ shared reference of mutable static
67+
|
68+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
69+
= note: reference of mutable static is a hard error from 2024 edition
70+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
71+
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
72+
|
73+
LL | let (_b, _c) = (&X, addr_of!(Y));
74+
| ~~~~~~~~~~~
75+
76+
error: shared reference of mutable static is discouraged
77+
--> $DIR/reference-of-mut-static.rs:38:13
78+
|
79+
LL | foo(&X);
80+
| ^^ shared reference of mutable static
81+
|
82+
= note: for more information, see issue #114447 <https://github.com/rust-lang/rust/issues/114447>
83+
= note: reference of mutable static is a hard error from 2024 edition
84+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
85+
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
86+
|
87+
LL | foo(addr_of!(X));
88+
| ~~~~~~~~~~~
89+
90+
error: aborting due to 6 previous errors
91+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
error[E0796]: reference of mutable static is disallowed
2+
--> $DIR/reference-of-mut-static.rs:16:18
3+
|
4+
LL | let _y = &X;
5+
| ^^ reference 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: 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
9+
|
10+
LL | let _y = addr_of!(X);
11+
| ~~~~~~~~~~~
12+
13+
error[E0796]: reference of mutable static is disallowed
14+
--> $DIR/reference-of-mut-static.rs:20:18
15+
|
16+
LL | let _y = &mut X;
17+
| ^^^^^^ reference of mutable static
18+
|
19+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
20+
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
21+
|
22+
LL | let _y = addr_of_mut!(X);
23+
| ~~~~~~~~~~~~~~~
24+
25+
error[E0796]: reference of mutable static is disallowed
26+
--> $DIR/reference-of-mut-static.rs:28:22
27+
|
28+
LL | let ref _a = X;
29+
| ^ reference of mutable static
30+
|
31+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
32+
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
33+
|
34+
LL | let ref _a = addr_of!(X);
35+
| ~~~~~~~~~~~
36+
37+
error[E0796]: reference of mutable static is disallowed
38+
--> $DIR/reference-of-mut-static.rs:32:25
39+
|
40+
LL | let (_b, _c) = (&X, &Y);
41+
| ^^ reference of mutable static
42+
|
43+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
44+
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
45+
|
46+
LL | let (_b, _c) = (addr_of!(X), &Y);
47+
| ~~~~~~~~~~~
48+
49+
error[E0796]: reference of mutable static is disallowed
50+
--> $DIR/reference-of-mut-static.rs:32:29
51+
|
52+
LL | let (_b, _c) = (&X, &Y);
53+
| ^^ reference of mutable static
54+
|
55+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
56+
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
57+
|
58+
LL | let (_b, _c) = (&X, addr_of!(Y));
59+
| ~~~~~~~~~~~
60+
61+
error[E0796]: reference of mutable static is disallowed
62+
--> $DIR/reference-of-mut-static.rs:38:13
63+
|
64+
LL | foo(&X);
65+
| ^^ reference of mutable static
66+
|
67+
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
68+
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
69+
|
70+
LL | foo(addr_of!(X));
71+
| ~~~~~~~~~~~
72+
73+
error: aborting due to 6 previous errors
74+
75+
For more information about this error, try `rustc --explain E0796`.
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// revisions: e2021 e2024
2+
3+
// [e2021] edition:2021
4+
// [e2024] compile-flags: --edition 2024 -Z unstable-options
5+
6+
#![deny(static_mut_ref)]
7+
8+
use std::ptr::{addr_of, addr_of_mut};
9+
10+
fn main() {
11+
static mut X: i32 = 1;
12+
13+
static mut Y: i32 = 1;
14+
15+
unsafe {
16+
let _y = &X;
17+
//[e2024]~^ ERROR reference of mutable static is disallowed
18+
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
19+
20+
let _y = &mut X;
21+
//[e2024]~^ ERROR reference of mutable static is disallowed
22+
//[e2021]~^^ ERROR mutable reference of mutable static is discouraged [static_mut_ref]
23+
24+
let _z = addr_of_mut!(X);
25+
26+
let _p = addr_of!(X);
27+
28+
let ref _a = X;
29+
//[e2024]~^ ERROR reference of mutable static is disallowed
30+
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
31+
32+
let (_b, _c) = (&X, &Y);
33+
//[e2024]~^ ERROR reference of mutable static is disallowed
34+
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
35+
//[e2024]~^^^ ERROR reference of mutable static is disallowed
36+
//[e2021]~^^^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
37+
38+
foo(&X);
39+
//[e2024]~^ ERROR reference of mutable static is disallowed
40+
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
41+
42+
static mut Z: &[i32; 3] = &[0, 1, 2];
43+
44+
let _ = Z.len();
45+
let _ = Z[0];
46+
let _ = format!("{:?}", Z);
47+
}
48+
}
49+
50+
fn foo<'a>(_x: &'a i32) {}

0 commit comments

Comments
 (0)