Skip to content

Commit 0e1afc4

Browse files
Check for use of mutable/extern statics in THIR unsafeck
1 parent 6b327aa commit 0e1afc4

26 files changed

+238
-28
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,20 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
169169
}
170170
}
171171
}
172-
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
173-
self.requires_unsafe(expr.span, UseOfInlineAssembly);
174-
}
175172
ExprKind::Deref { arg } => {
176-
if self.thir[arg].ty.is_unsafe_ptr() {
173+
if let ExprKind::StaticRef { def_id, .. } = self.thir[arg].kind {
174+
if self.tcx.is_mutable_static(def_id) {
175+
self.requires_unsafe(expr.span, UseOfMutableStatic);
176+
} else if self.tcx.is_foreign_item(def_id) {
177+
self.requires_unsafe(expr.span, UseOfExternStatic);
178+
}
179+
} else if self.thir[arg].ty.is_unsafe_ptr() {
177180
self.requires_unsafe(expr.span, DerefOfRawPointer);
178181
}
179182
}
183+
ExprKind::InlineAsm { .. } | ExprKind::LlvmInlineAsm { .. } => {
184+
self.requires_unsafe(expr.span, UseOfInlineAssembly);
185+
}
180186
ExprKind::Adt {
181187
adt_def,
182188
variant_index: _,
@@ -242,9 +248,7 @@ enum UnsafeOpKind {
242248
UseOfInlineAssembly,
243249
InitializingTypeWith,
244250
CastOfPointerToInt,
245-
#[allow(dead_code)] // FIXME
246251
UseOfMutableStatic,
247-
#[allow(dead_code)] // FIXME
248252
UseOfExternStatic,
249253
DerefOfRawPointer,
250254
#[allow(dead_code)] // FIXME

src/test/ui/intrinsics/issue-28575.stderr src/test/ui/intrinsics/issue-28575.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2-
--> $DIR/issue-28575.rs:8:5
2+
--> $DIR/issue-28575.rs:11:5
33
|
44
LL | FOO()
55
| ^^^ use of extern static

src/test/ui/intrinsics/issue-28575.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
#![feature(intrinsics)]
25

36
extern "C" {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2+
--> $DIR/issue-28575.rs:11:5
3+
|
4+
LL | FOO()
5+
| ^^^ use of extern static
6+
|
7+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/issues/issue-14227.stderr src/test/ui/issues/issue-14227.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2-
--> $DIR/issue-14227.rs:4:21
2+
--> $DIR/issue-14227.rs:7:21
33
|
44
LL | static CRASH: u32 = symbol;
55
| ^^^^^^ use of extern static

src/test/ui/issues/issue-14227.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
extern "C" {
25
pub static symbol: u32;
36
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2+
--> $DIR/issue-14227.rs:7:21
3+
|
4+
LL | static CRASH: u32 = symbol;
5+
| ^^^^^^ use of extern static
6+
|
7+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/issues/issue-16538.stderr src/test/ui/issues/issue-16538.mir.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
2-
--> $DIR/issue-16538.rs:11:27
2+
--> $DIR/issue-16538.rs:14:27
33
|
44
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

77
error[E0277]: `*const usize` cannot be shared between threads safely
8-
--> $DIR/issue-16538.rs:11:1
8+
--> $DIR/issue-16538.rs:14:1
99
|
1010
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const usize` cannot be shared between threads safely
@@ -14,7 +14,7 @@ LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
1414
= note: shared static variables must have a type that implements `Sync`
1515

1616
error[E0133]: use of extern static is unsafe and requires unsafe function or block
17-
--> $DIR/issue-16538.rs:11:34
17+
--> $DIR/issue-16538.rs:14:34
1818
|
1919
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
2020
| ^^^^ use of extern static

src/test/ui/issues/issue-16538.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
mod Y {
25
pub type X = usize;
36
extern "C" {
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
2+
--> $DIR/issue-16538.rs:14:27
3+
|
4+
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error[E0277]: `*const usize` cannot be shared between threads safely
8+
--> $DIR/issue-16538.rs:14:1
9+
|
10+
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `*const usize` cannot be shared between threads safely
12+
|
13+
= help: the trait `Sync` is not implemented for `*const usize`
14+
= note: shared static variables must have a type that implements `Sync`
15+
16+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
17+
--> $DIR/issue-16538.rs:14:34
18+
|
19+
LL | static foo: *const Y::X = Y::foo(Y::x as *const Y::X);
20+
| ^^^^ use of extern static
21+
|
22+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
23+
24+
error: aborting due to 3 previous errors
25+
26+
Some errors have detailed explanations: E0015, E0133, E0277.
27+
For more information about an error, try `rustc --explain E0015`.

src/test/ui/issues/issue-28324.stderr src/test/ui/issues/issue-28324.mir.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2-
--> $DIR/issue-28324.rs:5:24
2+
--> $DIR/issue-28324.rs:8:24
33
|
44
LL | pub static BAZ: u32 = *&error_message_count;
55
| ^^^^^^^^^^^^^^^^^^^^ use of extern static

src/test/ui/issues/issue-28324.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
extern "C" {
25
static error_message_count: u32;
36
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2+
--> $DIR/issue-28324.rs:8:25
3+
|
4+
LL | pub static BAZ: u32 = *&error_message_count;
5+
| ^^^^^^^^^^^^^^^^^^^ use of extern static
6+
|
7+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
8+
9+
error: aborting due to previous error
10+
11+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/safe-extern-statics-mut.stderr src/test/ui/safe-extern-statics-mut.mir.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
2-
--> $DIR/safe-extern-statics-mut.rs:11:13
2+
--> $DIR/safe-extern-statics-mut.rs:13:13
33
|
44
LL | let b = B;
55
| ^ use of mutable static
66
|
77
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
88

99
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
10-
--> $DIR/safe-extern-statics-mut.rs:12:14
10+
--> $DIR/safe-extern-statics-mut.rs:14:14
1111
|
1212
LL | let rb = &B;
1313
| ^^ use of mutable static
1414
|
1515
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
1616

1717
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
18-
--> $DIR/safe-extern-statics-mut.rs:13:14
18+
--> $DIR/safe-extern-statics-mut.rs:15:14
1919
|
2020
LL | let xb = XB;
2121
| ^^ use of mutable static
2222
|
2323
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
2424

2525
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
26-
--> $DIR/safe-extern-statics-mut.rs:14:15
26+
--> $DIR/safe-extern-statics-mut.rs:16:15
2727
|
2828
LL | let xrb = &XB;
2929
| ^^^ use of mutable static

src/test/ui/safe-extern-statics-mut.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// aux-build:extern-statics.rs
2+
// revisions: mir thir
3+
// [thir]compile-flags: -Z thir-unsafeck
24

35
extern crate extern_statics;
46
use extern_statics::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
2+
--> $DIR/safe-extern-statics-mut.rs:13:13
3+
|
4+
LL | let b = B;
5+
| ^ use of mutable static
6+
|
7+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
8+
9+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
10+
--> $DIR/safe-extern-statics-mut.rs:14:15
11+
|
12+
LL | let rb = &B;
13+
| ^ use of mutable static
14+
|
15+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
16+
17+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
18+
--> $DIR/safe-extern-statics-mut.rs:15:14
19+
|
20+
LL | let xb = XB;
21+
| ^^ use of mutable static
22+
|
23+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
24+
25+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
26+
--> $DIR/safe-extern-statics-mut.rs:16:16
27+
|
28+
LL | let xrb = &XB;
29+
| ^^ use of mutable static
30+
|
31+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
32+
33+
error: aborting due to 4 previous errors
34+
35+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/safe-extern-statics.stderr src/test/ui/safe-extern-statics.mir.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2-
--> $DIR/safe-extern-statics.rs:11:13
2+
--> $DIR/safe-extern-statics.rs:13:13
33
|
44
LL | let a = A;
55
| ^ use of extern static
66
|
77
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
88

99
error[E0133]: use of extern static is unsafe and requires unsafe function or block
10-
--> $DIR/safe-extern-statics.rs:12:14
10+
--> $DIR/safe-extern-statics.rs:14:14
1111
|
1212
LL | let ra = &A;
1313
| ^^ use of extern static
1414
|
1515
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
1616

1717
error[E0133]: use of extern static is unsafe and requires unsafe function or block
18-
--> $DIR/safe-extern-statics.rs:13:14
18+
--> $DIR/safe-extern-statics.rs:15:14
1919
|
2020
LL | let xa = XA;
2121
| ^^ use of extern static
2222
|
2323
= 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[E0133]: use of extern static is unsafe and requires unsafe function or block
26-
--> $DIR/safe-extern-statics.rs:14:15
26+
--> $DIR/safe-extern-statics.rs:16:15
2727
|
2828
LL | let xra = &XA;
2929
| ^^^ use of extern static

src/test/ui/safe-extern-statics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// aux-build:extern-statics.rs
2+
// revisions: mir thir
3+
// [thir]compile-flags: -Z thir-unsafeck
24

35
extern crate extern_statics;
46
use extern_statics::*;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
2+
--> $DIR/safe-extern-statics.rs:13:13
3+
|
4+
LL | let a = A;
5+
| ^ use of extern static
6+
|
7+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
8+
9+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
10+
--> $DIR/safe-extern-statics.rs:14:15
11+
|
12+
LL | let ra = &A;
13+
| ^ use of extern static
14+
|
15+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
16+
17+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
18+
--> $DIR/safe-extern-statics.rs:15:14
19+
|
20+
LL | let xa = XA;
21+
| ^^ use of extern static
22+
|
23+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
24+
25+
error[E0133]: use of extern static is unsafe and requires unsafe function or block
26+
--> $DIR/safe-extern-statics.rs:16:16
27+
|
28+
LL | let xra = &XA;
29+
| ^^ use of extern static
30+
|
31+
= note: extern statics are not controlled by the Rust type system: invalid data, aliasing violations or data races will cause undefined behavior
32+
33+
error: aborting due to 4 previous errors
34+
35+
For more information about this error, try `rustc --explain E0133`.

src/test/ui/static/static-mut-foreign-requires-unsafe.stderr src/test/ui/static/static-mut-foreign-requires-unsafe.mir.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
2-
--> $DIR/static-mut-foreign-requires-unsafe.rs:6:5
2+
--> $DIR/static-mut-foreign-requires-unsafe.rs:9:5
33
|
44
LL | a += 3;
55
| ^^^^^^ use of mutable static
66
|
77
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
88

99
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
10-
--> $DIR/static-mut-foreign-requires-unsafe.rs:7:5
10+
--> $DIR/static-mut-foreign-requires-unsafe.rs:10:5
1111
|
1212
LL | a = 4;
1313
| ^^^^^ use of mutable static
1414
|
1515
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
1616

1717
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
18-
--> $DIR/static-mut-foreign-requires-unsafe.rs:8:14
18+
--> $DIR/static-mut-foreign-requires-unsafe.rs:11:14
1919
|
2020
LL | let _b = a;
2121
| ^ use of mutable static

src/test/ui/static/static-mut-foreign-requires-unsafe.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// revisions: mir thir
2+
// [thir]compile-flags: -Z thir-unsafeck
3+
14
extern "C" {
25
static mut a: i32;
36
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
2+
--> $DIR/static-mut-foreign-requires-unsafe.rs:9:5
3+
|
4+
LL | a += 3;
5+
| ^ use of mutable static
6+
|
7+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
8+
9+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
10+
--> $DIR/static-mut-foreign-requires-unsafe.rs:10:5
11+
|
12+
LL | a = 4;
13+
| ^ use of mutable static
14+
|
15+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
16+
17+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
18+
--> $DIR/static-mut-foreign-requires-unsafe.rs:11:14
19+
|
20+
LL | let _b = a;
21+
| ^ use of mutable static
22+
|
23+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
24+
25+
error: aborting due to 3 previous errors
26+
27+
For more information about this error, try `rustc --explain E0133`.

0 commit comments

Comments
 (0)