Skip to content

Commit 931692f

Browse files
committed
Recognise thread local statics in THIR unsafeck
1 parent e1fcecb commit 931692f

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
394394
}
395395
}
396396
ExprKind::Deref { arg } => {
397-
if let ExprKind::StaticRef { def_id, .. } = self.thir[arg].kind {
397+
if let ExprKind::StaticRef { def_id, .. } | ExprKind::ThreadLocalRef(def_id) =
398+
self.thir[arg].kind
399+
{
398400
if self.tcx.is_mutable_static(def_id) {
399401
self.requires_unsafe(expr.span, UseOfMutableStatic);
400402
} else if self.tcx.is_foreign_item(def_id) {

tests/ui/thread-local/thread-local-static.stderr tests/ui/thread-local/thread-local-static.mir.stderr

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0658]: mutable references are not allowed in constant functions
2-
--> $DIR/thread-local-static.rs:7:12
2+
--> $DIR/thread-local-static.rs:10:12
33
|
44
LL | const fn g(x: &mut [u32; 8]) {
55
| ^
@@ -8,21 +8,21 @@ LL | const fn g(x: &mut [u32; 8]) {
88
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
99

1010
error[E0625]: thread-local statics cannot be accessed at compile-time
11-
--> $DIR/thread-local-static.rs:9:28
11+
--> $DIR/thread-local-static.rs:12:28
1212
|
1313
LL | std::mem::swap(x, &mut STATIC_VAR_2)
1414
| ^^^^^^^^^^^^
1515

1616
error[E0013]: constant functions cannot refer to statics
17-
--> $DIR/thread-local-static.rs:9:28
17+
--> $DIR/thread-local-static.rs:12:28
1818
|
1919
LL | std::mem::swap(x, &mut STATIC_VAR_2)
2020
| ^^^^^^^^^^^^
2121
|
2222
= help: consider extracting the value of the `static` to a `const`, and referring to that
2323

2424
error[E0658]: mutable references are not allowed in constant functions
25-
--> $DIR/thread-local-static.rs:9:23
25+
--> $DIR/thread-local-static.rs:12:23
2626
|
2727
LL | std::mem::swap(x, &mut STATIC_VAR_2)
2828
| ^^^^^^^^^^^^^^^^^
@@ -31,7 +31,7 @@ LL | std::mem::swap(x, &mut STATIC_VAR_2)
3131
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
3232

3333
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
34-
--> $DIR/thread-local-static.rs:9:23
34+
--> $DIR/thread-local-static.rs:12:23
3535
|
3636
LL | std::mem::swap(x, &mut STATIC_VAR_2)
3737
| ^^^^^^^^^^^^^^^^^ use of mutable static

tests/ui/thread-local/thread-local-static.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// edition:2018
2+
// revisions: mir thir
3+
//thir: -Zthir-unsafeck
24

35
#![feature(thread_local)]
46
#![feature(const_swap)]
7+
58
#[thread_local]
69
static mut STATIC_VAR_2: [u32; 8] = [4; 8];
710
const fn g(x: &mut [u32; 8]) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0658]: mutable references are not allowed in constant functions
2+
--> $DIR/thread-local-static.rs:10:12
3+
|
4+
LL | const fn g(x: &mut [u32; 8]) {
5+
| ^
6+
|
7+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
8+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
9+
10+
error[E0625]: thread-local statics cannot be accessed at compile-time
11+
--> $DIR/thread-local-static.rs:12:28
12+
|
13+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
14+
| ^^^^^^^^^^^^
15+
16+
error[E0013]: constant functions cannot refer to statics
17+
--> $DIR/thread-local-static.rs:12:28
18+
|
19+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
20+
| ^^^^^^^^^^^^
21+
|
22+
= help: consider extracting the value of the `static` to a `const`, and referring to that
23+
24+
error[E0658]: mutable references are not allowed in constant functions
25+
--> $DIR/thread-local-static.rs:12:23
26+
|
27+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
28+
| ^^^^^^^^^^^^^^^^^
29+
|
30+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
31+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
32+
33+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
34+
--> $DIR/thread-local-static.rs:12:23
35+
|
36+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
37+
| ^^^^^^^^^^^^^^^^^ use of mutable static
38+
|
39+
= note: mutable statics can be mutated by multiple threads: aliasing violations or data races will cause undefined behavior
40+
41+
error: aborting due to 5 previous errors
42+
43+
Some errors have detailed explanations: E0013, E0133, E0625, E0658.
44+
For more information about an error, try `rustc --explain E0013`.

0 commit comments

Comments
 (0)