Skip to content

Commit

Permalink
Add test for E0796 and static_mut_ref lint
Browse files Browse the repository at this point in the history
  • Loading branch information
obeis committed Dec 29, 2023
1 parent a4a774a commit cd07eb1
Show file tree
Hide file tree
Showing 8 changed files with 356 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tests/ui/static/reference-of-mut-static-safe.e2021.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 15 additions & 0 deletions tests/ui/static/reference-of-mut-static-safe.e2024.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions tests/ui/static/reference-of-mut-static-safe.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 23 additions & 0 deletions tests/ui/static/reference-of-mut-static-unsafe-fn.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 63 additions & 0 deletions tests/ui/static/reference-of-mut-static-unsafe-fn.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 91 additions & 0 deletions tests/ui/static/reference-of-mut-static.e2021.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 75 additions & 0 deletions tests/ui/static/reference-of-mut-static.e2024.stderr
Original file line number Diff line number Diff line change
@@ -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 changes: 50 additions & 0 deletions tests/ui/static/reference-of-mut-static.rs
Original file line number Diff line number Diff line change
@@ -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) {}

0 comments on commit cd07eb1

Please sign in to comment.