mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-31 22:41:50 +00:00
Add test for E0796
and static_mut_ref
lint
This commit is contained in:
parent
e36f7b7a5c
commit
18edf9a64e
26
tests/ui/static/reference-of-mut-static-safe.e2021.stderr
Normal file
26
tests/ui/static/reference-of-mut-static-safe.e2021.stderr
Normal file
@ -0,0 +1,26 @@
|
||||
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:14
|
||||
|
|
||||
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-of-mut-static-safe.e2024.stderr
Normal file
15
tests/ui/static/reference-of-mut-static-safe.e2024.stderr
Normal file
@ -0,0 +1,15 @@
|
||||
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`.
|
13
tests/ui/static/reference-of-mut-static-safe.rs
Normal file
13
tests/ui/static/reference-of-mut-static-safe.rs
Normal file
@ -0,0 +1,13 @@
|
||||
// revisions: e2021 e2024
|
||||
|
||||
// [e2021] edition:2021
|
||||
// [e2024] compile-flags: --edition 2024 -Z unstable-options
|
||||
|
||||
fn main() {
|
||||
static mut X: i32 = 1;
|
||||
|
||||
let _x = &X;
|
||||
//[e2024]~^ reference of mutable static is disallowed [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]
|
||||
}
|
23
tests/ui/static/reference-of-mut-static-unsafe-fn.rs
Normal file
23
tests/ui/static/reference-of-mut-static-unsafe-fn.rs
Normal file
@ -0,0 +1,23 @@
|
||||
// compile-flags: --edition 2024 -Z unstable-options
|
||||
|
||||
fn main() {}
|
||||
|
||||
unsafe fn _foo() {
|
||||
static mut X: i32 = 1;
|
||||
static mut Y: i32 = 1;
|
||||
|
||||
let _y = &X;
|
||||
//~^ ERROR reference of mutable static is disallowed
|
||||
|
||||
let ref _a = X;
|
||||
//~^ ERROR reference of mutable static is disallowed
|
||||
|
||||
let (_b, _c) = (&X, &Y);
|
||||
//~^ ERROR reference of mutable static is disallowed
|
||||
//~^^ ERROR reference of mutable static is disallowed
|
||||
|
||||
foo(&X);
|
||||
//~^ ERROR reference of mutable static is disallowed
|
||||
}
|
||||
|
||||
fn foo<'a>(_x: &'a i32) {}
|
63
tests/ui/static/reference-of-mut-static-unsafe-fn.stderr
Normal file
63
tests/ui/static/reference-of-mut-static-unsafe-fn.stderr
Normal file
@ -0,0 +1,63 @@
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:9:14
|
||||
|
|
||||
LL | let _y = &X;
|
||||
| ^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let _y = addr_of!(X);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:12:18
|
||||
|
|
||||
LL | let ref _a = X;
|
||||
| ^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let ref _a = addr_of!(X);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:15:21
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let (_b, _c) = (addr_of!(X), &Y);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:15:25
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let (_b, _c) = (&X, addr_of!(Y));
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static-unsafe-fn.rs:19:9
|
||||
|
|
||||
LL | foo(&X);
|
||||
| ^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | foo(addr_of!(X));
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0796`.
|
91
tests/ui/static/reference-of-mut-static.e2021.stderr
Normal file
91
tests/ui/static/reference-of-mut-static.e2021.stderr
Normal file
@ -0,0 +1,91 @@
|
||||
error: shared reference of mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:16:18
|
||||
|
|
||||
LL | let _y = &X;
|
||||
| ^^ shared reference of mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <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: the lint level is defined here
|
||||
--> $DIR/reference-of-mut-static.rs:6:9
|
||||
|
|
||||
LL | #![deny(static_mut_ref)]
|
||||
| ^^^^^^^^^^^^^^
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let _y = addr_of!(X);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: mutable reference of mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:20:18
|
||||
|
|
||||
LL | let _y = &mut X;
|
||||
| ^^^^^^ mutable reference of mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <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
|
||||
|
|
||||
LL | let _y = addr_of_mut!(X);
|
||||
| ~~~~~~~~~~~~~~~
|
||||
|
||||
error: shared reference of mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:28:22
|
||||
|
|
||||
LL | let ref _a = X;
|
||||
| ^ shared reference of mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <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
|
||||
|
|
||||
LL | let ref _a = addr_of!(X);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: shared reference of mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:32:25
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ shared reference of mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <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
|
||||
|
|
||||
LL | let (_b, _c) = (addr_of!(X), &Y);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: shared reference of mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:32:29
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ shared reference of mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <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
|
||||
|
|
||||
LL | let (_b, _c) = (&X, addr_of!(Y));
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: shared reference of mutable static is discouraged
|
||||
--> $DIR/reference-of-mut-static.rs:38:13
|
||||
|
|
||||
LL | foo(&X);
|
||||
| ^^ shared reference of mutable static
|
||||
|
|
||||
= note: for more information, see issue #114447 <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
|
||||
|
|
||||
LL | foo(addr_of!(X));
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
75
tests/ui/static/reference-of-mut-static.e2024.stderr
Normal file
75
tests/ui/static/reference-of-mut-static.e2024.stderr
Normal file
@ -0,0 +1,75 @@
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static.rs:16:18
|
||||
|
|
||||
LL | let _y = &X;
|
||||
| ^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let _y = addr_of!(X);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static.rs:20:18
|
||||
|
|
||||
LL | let _y = &mut X;
|
||||
| ^^^^^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: mutable references are dangerous since if there's any other pointer or reference used for that static while the reference lives, that's UB; use `addr_of_mut!` instead to create a raw pointer
|
||||
|
|
||||
LL | let _y = addr_of_mut!(X);
|
||||
| ~~~~~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static.rs:28:22
|
||||
|
|
||||
LL | let ref _a = X;
|
||||
| ^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let ref _a = addr_of!(X);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static.rs:32:25
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let (_b, _c) = (addr_of!(X), &Y);
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static.rs:32:29
|
||||
|
|
||||
LL | let (_b, _c) = (&X, &Y);
|
||||
| ^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | let (_b, _c) = (&X, addr_of!(Y));
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error[E0796]: reference of mutable static is disallowed
|
||||
--> $DIR/reference-of-mut-static.rs:38:13
|
||||
|
|
||||
LL | foo(&X);
|
||||
| ^^ reference of mutable static
|
||||
|
|
||||
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
|
||||
help: shared references are dangerous since if there's any kind of mutation of that static while the reference lives, that's UB; use `addr_of!` instead to create a raw pointer
|
||||
|
|
||||
LL | foo(addr_of!(X));
|
||||
| ~~~~~~~~~~~
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0796`.
|
50
tests/ui/static/reference-of-mut-static.rs
Normal file
50
tests/ui/static/reference-of-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_ref)]
|
||||
|
||||
use std::ptr::{addr_of, addr_of_mut};
|
||||
|
||||
fn main() {
|
||||
static mut X: i32 = 1;
|
||||
|
||||
static mut Y: i32 = 1;
|
||||
|
||||
unsafe {
|
||||
let _y = &X;
|
||||
//[e2024]~^ ERROR reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
|
||||
|
||||
let _y = &mut X;
|
||||
//[e2024]~^ ERROR reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR mutable reference of mutable static is discouraged [static_mut_ref]
|
||||
|
||||
let _z = addr_of_mut!(X);
|
||||
|
||||
let _p = addr_of!(X);
|
||||
|
||||
let ref _a = X;
|
||||
//[e2024]~^ ERROR reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
|
||||
|
||||
let (_b, _c) = (&X, &Y);
|
||||
//[e2024]~^ ERROR reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
|
||||
//[e2024]~^^^ ERROR reference of mutable static is disallowed
|
||||
//[e2021]~^^^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
|
||||
|
||||
foo(&X);
|
||||
//[e2024]~^ ERROR reference of mutable static is disallowed
|
||||
//[e2021]~^^ ERROR shared reference of mutable static is discouraged [static_mut_ref]
|
||||
|
||||
static mut Z: &[i32; 3] = &[0, 1, 2];
|
||||
|
||||
let _ = Z.len();
|
||||
let _ = Z[0];
|
||||
let _ = format!("{:?}", Z);
|
||||
}
|
||||
}
|
||||
|
||||
fn foo<'a>(_x: &'a i32) {}
|
Loading…
Reference in New Issue
Block a user