Skip to content

Commit d570375

Browse files
Reorder extern static errors over static mut
Extern statics are much more spicy than static mut: they may be uninit! Issue their errors first, and in doing so make miri happier.
1 parent b34ded0 commit d570375

File tree

5 files changed

+32
-31
lines changed

5 files changed

+32
-31
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -464,12 +464,13 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
464464
if let ExprKind::StaticRef { def_id, .. } | ExprKind::ThreadLocalRef(def_id) =
465465
self.thir[arg].kind
466466
{
467-
if self.tcx.is_mutable_static(def_id) && allow_implicit_static_deref {
467+
// The requirements for an extern static are much more stringent
468+
if self.tcx.is_foreign_item(def_id) {
469+
self.requires_unsafe(expr.span, UseOfExternStatic);
470+
} else if self.tcx.is_mutable_static(def_id) && allow_implicit_static_deref {
468471
// we're only taking the address of the implicit place expr, it's fine
469472
} else if self.tcx.is_mutable_static(def_id) {
470473
self.requires_unsafe(expr.span, UseOfMutableStatic);
471-
} else if self.tcx.is_foreign_item(def_id) {
472-
self.requires_unsafe(expr.span, UseOfExternStatic);
473474
}
474475
} else if self.thir[arg].ty.is_unsafe_ptr() {
475476
self.requires_unsafe(expr.span, DerefOfRawPointer);

src/tools/miri/tests/pass/static_mut.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::ptr::addr_of;
22

33
static mut FOO: i32 = 42;
44

5-
static BAR: Foo = Foo(unsafe { addr_of!(FOO) });
5+
static BAR: Foo = Foo(addr_of!(FOO));
66

77
#[allow(dead_code)]
88
struct Foo(*const i32);

tests/ui/static/safe-extern-statics-mut.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ extern "C" {
88
}
99

1010
fn main() {
11-
let b = B; //~ ERROR use of mutable static is unsafe
12-
let rb = &B; //~ ERROR use of mutable static is unsafe
13-
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
14-
let xb = XB; //~ ERROR use of mutable static is unsafe
15-
let xrb = &XB; //~ ERROR use of mutable static is unsafe
16-
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
11+
let b = B; //~ static is unsafe
12+
let rb = &B; //~ static is unsafe
13+
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
14+
let xb = XB; //~ static is unsafe
15+
let xrb = &XB; //~ static is unsafe
16+
//~^ WARN shared reference to mutable static is discouraged [static_mut_refs]
1717
}

tests/ui/static/safe-extern-statics-mut.stderr

+12-12
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,37 @@ help: use `addr_of!` instead to create a raw pointer
2727
LL | let xrb = addr_of!(XB);
2828
| ~~~~~~~~~~~~
2929

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

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

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

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

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

tests/ui/static/static-mut-foreign-requires-unsafe.stderr

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
1-
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
1+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
22
--> $DIR/static-mut-foreign-requires-unsafe.rs:6:5
33
|
44
LL | a += 3;
5-
| ^ use of mutable static
5+
| ^ use of extern static
66
|
7-
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
7+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
88

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

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

2525
error: aborting due to 3 previous errors
2626

0 commit comments

Comments
 (0)