Skip to content

Commit

Permalink
Reorder extern static errors over static mut
Browse files Browse the repository at this point in the history
Extern statics are much more spicy than static mut: they may be uninit!
Issue their errors first, and in doing so make miri happier.
  • Loading branch information
workingjubilee committed Jun 1, 2024
1 parent b34ded0 commit d570375
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 31 deletions.
7 changes: 4 additions & 3 deletions compiler/rustc_mir_build/src/check_unsafety.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,13 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
if let ExprKind::StaticRef { def_id, .. } | ExprKind::ThreadLocalRef(def_id) =
self.thir[arg].kind
{
if self.tcx.is_mutable_static(def_id) && allow_implicit_static_deref {
// The requirements for an extern static are much more stringent
if self.tcx.is_foreign_item(def_id) {
self.requires_unsafe(expr.span, UseOfExternStatic);
} else if self.tcx.is_mutable_static(def_id) && allow_implicit_static_deref {
// we're only taking the address of the implicit place expr, it's fine
} else if self.tcx.is_mutable_static(def_id) {
self.requires_unsafe(expr.span, UseOfMutableStatic);
} else if self.tcx.is_foreign_item(def_id) {
self.requires_unsafe(expr.span, UseOfExternStatic);
}
} else if self.thir[arg].ty.is_unsafe_ptr() {
self.requires_unsafe(expr.span, DerefOfRawPointer);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/miri/tests/pass/static_mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ptr::addr_of;

static mut FOO: i32 = 42;

static BAR: Foo = Foo(unsafe { addr_of!(FOO) });
static BAR: Foo = Foo(addr_of!(FOO));

#[allow(dead_code)]
struct Foo(*const i32);
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/static/safe-extern-statics-mut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ extern "C" {
}

fn main() {
let b = B; //~ ERROR use of mutable static is unsafe
let rb = &B; //~ ERROR use of mutable static is unsafe
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
let xb = XB; //~ ERROR use of mutable static is unsafe
let xrb = &XB; //~ ERROR use of mutable static is unsafe
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
let b = B; //~ static is unsafe
let rb = &B; //~ static is unsafe
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
let xb = XB; //~ static is unsafe
let xrb = &XB; //~ static is unsafe
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
}
24 changes: 12 additions & 12 deletions tests/ui/static/safe-extern-statics-mut.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,37 @@ help: use `addr_of!` instead to create a raw pointer
LL | let xrb = addr_of!(XB);
| ~~~~~~~~~~~~

error[E0133]: use of mutable static is unsafe and requires unsafe function or block
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/safe-extern-statics-mut.rs:11:13
|
LL | let b = B;
| ^ use of mutable static
| ^ use of extern static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error[E0133]: use of mutable static is unsafe and requires unsafe function or block
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/safe-extern-statics-mut.rs:12:15
|
LL | let rb = &B;
| ^ use of mutable static
| ^ use of extern static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error[E0133]: use of mutable static is unsafe and requires unsafe function or block
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/safe-extern-statics-mut.rs:14:14
|
LL | let xb = XB;
| ^^ use of mutable static
| ^^ use of extern static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error[E0133]: use of mutable static is unsafe and requires unsafe function or block
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/safe-extern-statics-mut.rs:15:16
|
LL | let xrb = &XB;
| ^^ use of mutable static
| ^^ use of extern static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error: aborting due to 4 previous errors; 2 warnings emitted

Expand Down
18 changes: 9 additions & 9 deletions tests/ui/static/static-mut-foreign-requires-unsafe.stderr
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/static-mut-foreign-requires-unsafe.rs:6:5
|
LL | a += 3;
| ^ use of mutable static
| ^ use of extern static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error[E0133]: use of mutable static is unsafe and requires unsafe function or block
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/static-mut-foreign-requires-unsafe.rs:7:5
|
LL | a = 4;
| ^ use of mutable static
| ^ use of extern static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error[E0133]: use of mutable static is unsafe and requires unsafe function or block
error[E0133]: use of extern static is unsafe and requires unsafe function or block
--> $DIR/static-mut-foreign-requires-unsafe.rs:8:14
|
LL | let _b = a;
| ^ use of mutable static
| ^ use of extern static
|
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior

error: aborting due to 3 previous errors

Expand Down

0 comments on commit d570375

Please sign in to comment.