Skip to content

Commit

Permalink
Add test for E0796 error
Browse files Browse the repository at this point in the history
Add test for `static_mut_ref` lint
  • Loading branch information
obeis committed Nov 14, 2023
1 parent 83b2c95 commit b4f1f06
Show file tree
Hide file tree
Showing 12 changed files with 220 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 2,22 @@

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

struct Foo { x: [usize; 2] }
struct Foo {
x: [usize; 2],
}

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

impl Foo {
fn x(&mut self) -> &mut usize { &mut self.x[0] }
fn x(&mut self) -> &mut usize {
&mut self.x[0]
}
}

fn main() {
unsafe {
let sfoo: *mut Foo = &mut SFOO;
//~^ WARN use of mutable static is discouraged [static_mut_ref]
let x = (*sfoo).x();
(*sfoo).x[1] = 1;
*x = 1;
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/borrowck/borrowck-unsafe-static-mutable-borrows.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,16 @@
warning: use of mutable static is discouraged
--> $DIR/borrowck-unsafe-static-mutable-borrows.rs:19:30
|
LL | let sfoo: *mut Foo = &mut SFOO;
| ^^^^^^^^^ use of mutable static
|
= note: use 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: use `addr_of_mut!` macro
|
LL | let sfoo: *mut Foo = addr_of_mut!(SFOO);
| ~~~~~~~~~~~~~~~~~~

warning: 1 warning emitted

20 changes: 20 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_block.e2015.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
error: use of mutable static is discouraged
--> $DIR/use_of_mutable_static_unsafe_block.rs:18:18
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: use 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/use_of_mutable_static_unsafe_block.rs:8:9
|
LL | #![deny(static_mut_ref)]
| ^^^^^^^^^^^^^^
help: use `addr_of_mut!` macro
|
LL | let _y = addr_of_mut!(X);
| ~~~~~~~~~~~~~~~

error: aborting due to previous error

20 changes: 20 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_block.e2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
error: use of mutable static is discouraged
--> $DIR/use_of_mutable_static_unsafe_block.rs:18:18
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: use 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/use_of_mutable_static_unsafe_block.rs:8:9
|
LL | #![deny(static_mut_ref)]
| ^^^^^^^^^^^^^^
help: use `addr_of_mut!` macro
|
LL | let _y = addr_of_mut!(X);
| ~~~~~~~~~~~~~~~

error: aborting due to previous error

20 changes: 20 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_block.e2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
error: use of mutable static is discouraged
--> $DIR/use_of_mutable_static_unsafe_block.rs:18:18
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: use 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/use_of_mutable_static_unsafe_block.rs:8:9
|
LL | #![deny(static_mut_ref)]
| ^^^^^^^^^^^^^^
help: use `addr_of_mut!` macro
|
LL | let _y = addr_of_mut!(X);
| ~~~~~~~~~~~~~~~

error: aborting due to previous error

15 changes: 15 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_block.e2024.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,15 @@
error[E0796]: use of mutable static is disallowed
--> $DIR/use_of_mutable_static_unsafe_block.rs:18:18
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
help: use `addr_of_mut!` macro
|
LL | let _y = addr_of_mut!(X);
| ~~~~~~~~~~~~~~~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0796`.
28 changes: 28 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_block.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,28 @@
// revisions: e2015 e2018 e2021 e2024

// [e2015] edition:2015
// [e2018] edition:2018
// [e2021] edition:2021
// [e2024] compile-flags: --edition 2024 -Zunstable-options

#![deny(static_mut_ref)]

use std::ptr::addr_of_mut;

fn main() {
static mut X: i32 = 1;

static mut FOO: [u8; 3] = [1, 2, 3];

unsafe {
let _y = &X;
//[e2024]~^ ERROR use of mutable static is disallowed
//[e2021]~^^ ERROR use of mutable static is discouraged [static_mut_ref]
//[e2018]~^^^ ERROR use of mutable static is discouraged [static_mut_ref]
//[e2015]~^^^^ ERROR use of mutable static is discouraged [static_mut_ref]

let _z = addr_of_mut!(X);

let _ = FOO.len();
}
}
20 changes: 20 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_fn.e2015.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
error: use of mutable static is discouraged
--> $DIR/use_of_mutable_static_unsafe_fn.rs:14:14
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: use 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/use_of_mutable_static_unsafe_fn.rs:8:9
|
LL | #![deny(static_mut_ref)]
| ^^^^^^^^^^^^^^
help: use `addr_of_mut!` macro
|
LL | let _y = addr_of_mut!(X);
| ~~~~~~~~~~~~~~~

error: aborting due to previous error

20 changes: 20 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_fn.e2018.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
error: use of mutable static is discouraged
--> $DIR/use_of_mutable_static_unsafe_fn.rs:14:14
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: use 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/use_of_mutable_static_unsafe_fn.rs:8:9
|
LL | #![deny(static_mut_ref)]
| ^^^^^^^^^^^^^^
help: use `addr_of_mut!` macro
|
LL | let _y = addr_of_mut!(X);
| ~~~~~~~~~~~~~~~

error: aborting due to previous error

20 changes: 20 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_fn.e2021.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,20 @@
error: use of mutable static is discouraged
--> $DIR/use_of_mutable_static_unsafe_fn.rs:14:14
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: use 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/use_of_mutable_static_unsafe_fn.rs:8:9
|
LL | #![deny(static_mut_ref)]
| ^^^^^^^^^^^^^^
help: use `addr_of_mut!` macro
|
LL | let _y = addr_of_mut!(X);
| ~~~~~~~~~~~~~~~

error: aborting due to previous error

15 changes: 15 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_fn.e2024.stderr
Original file line number Diff line number Diff line change
@@ -0,0 1,15 @@
error[E0796]: use of mutable static is disallowed
--> $DIR/use_of_mutable_static_unsafe_fn.rs:14:14
|
LL | let _y = &X;
| ^^ use of mutable static
|
= note: mutable statics can be written to by multiple threads: aliasing violations or data races will cause undefined behavior
help: use `addr_of_mut!` macro
|
LL | let _y = addr_of_mut!(X);
| ~~~~~~~~~~~~~~~

error: aborting due to previous error

For more information about this error, try `rustc --explain E0796`.
19 changes: 19 additions & 0 deletions tests/ui/static/use_of_mutable_static_unsafe_fn.rs
Original file line number Diff line number Diff line change
@@ -0,0 1,19 @@
// revisions: e2015 e2018 e2021 e2024

// [e2015] edition:2015
// [e2018] edition:2018
// [e2021] edition:2021
// [e2024] compile-flags: --edition 2024 -Zunstable-options

#![deny(static_mut_ref)]

fn main() {}

unsafe fn _foo() {
static mut X: i32 = 1;
let _y = &X;
//[e2024]~^ ERROR use of mutable static is disallowed
//[e2021]~^^ ERROR use of mutable static is discouraged [static_mut_ref]
//[e2018]~^^^ ERROR use of mutable static is discouraged [static_mut_ref]
//[e2015]~^^^^ ERROR use of mutable static is discouraged [static_mut_ref]
}

0 comments on commit b4f1f06

Please sign in to comment.