Skip to content

Commit 39275c1

Browse files
committed
Make THIR unsafe checker recognise thread_local statics
1 parent 98108dc commit 39275c1

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

compiler/rustc_mir_build/src/check_unsafety.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,9 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
379379
}
380380
}
381381
ExprKind::Deref { arg } => {
382-
if let ExprKind::StaticRef { def_id, .. } = self.thir[arg].kind {
382+
if let ExprKind::StaticRef { def_id, .. } | ExprKind::ThreadLocalRef(def_id) =
383+
self.thir[arg].kind
384+
{
383385
if self.tcx.is_mutable_static(def_id) {
384386
self.requires_unsafe(expr.span, UseOfMutableStatic);
385387
} else if self.tcx.is_foreign_item(def_id) {

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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// edition:2018
22

3+
// revisions: mir thir
4+
// [thir]compile-flags: -Zthir-unsafeck
5+
36
#![feature(thread_local)]
47
#![feature(const_swap)]
58
#[thread_local]
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
error[E0133]: use of mutable static is unsafe and requires unsafe function or block
2+
--> $DIR/thread-local-static.rs:12:28
3+
|
4+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
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[E0658]: mutable references are not allowed in constant functions
10+
--> $DIR/thread-local-static.rs:10:12
11+
|
12+
LL | const fn g(x: &mut [u32; 8]) {
13+
| ^
14+
|
15+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
16+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
17+
18+
error[E0625]: thread-local statics cannot be accessed at compile-time
19+
--> $DIR/thread-local-static.rs:12:28
20+
|
21+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
22+
| ^^^^^^^^^^^^
23+
24+
error[E0013]: constant functions cannot refer to statics
25+
--> $DIR/thread-local-static.rs:12:28
26+
|
27+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
28+
| ^^^^^^^^^^^^
29+
|
30+
= help: consider extracting the value of the `static` to a `const`, and referring to that
31+
32+
error[E0658]: mutable references are not allowed in constant functions
33+
--> $DIR/thread-local-static.rs:12:23
34+
|
35+
LL | std::mem::swap(x, &mut STATIC_VAR_2)
36+
| ^^^^^^^^^^^^^^^^^
37+
|
38+
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information
39+
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable
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)