Skip to content

Commit 5e76103

Browse files
committed
Reject #[thread_local] attribute on non-static items
1 parent d8e5647 commit 5e76103

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

compiler/rustc_passes/src/check_attr.rs

+16
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ impl CheckAttrVisitor<'_> {
8080
self.check_rustc_must_implement_one_of(attr, span, target)
8181
}
8282
sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
83+
sym::thread_local => self.check_thread_local(attr, span, target),
8384
sym::track_caller => {
8485
self.check_track_caller(hir_id, attr.span, attrs, span, target)
8586
}
@@ -521,6 +522,21 @@ impl CheckAttrVisitor<'_> {
521522
}
522523
}
523524

525+
/// Checks if the `#[thread_local]` attribute on `item` is valid. Returns `true` if valid.
526+
fn check_thread_local(&self, attr: &Attribute, span: Span, target: Target) -> bool {
527+
match target {
528+
Target::ForeignStatic | Target::Static => true,
529+
_ => {
530+
self.tcx
531+
.sess
532+
.struct_span_err(attr.span, "attribute should be applied to a static")
533+
.span_label(span, "not a static")
534+
.emit();
535+
false
536+
}
537+
}
538+
}
539+
524540
fn doc_attr_str_error(&self, meta: &NestedMetaItem, attr_name: &str) {
525541
self.tcx
526542
.sess
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Check that #[thread_local] attribute is rejected on non-static items.
2+
#![feature(thread_local)]
3+
4+
#[thread_local]
5+
//~^ ERROR attribute should be applied to a static
6+
const A: u32 = 0;
7+
8+
#[thread_local]
9+
//~^ ERROR attribute should be applied to a static
10+
fn main() {
11+
#[thread_local] || {};
12+
//~^ ERROR attribute should be applied to a static
13+
}
14+
15+
struct S {
16+
#[thread_local]
17+
//~^ ERROR attribute should be applied to a static
18+
a: String,
19+
b: String,
20+
}
21+
22+
#[thread_local]
23+
// Static. OK.
24+
static B: u32 = 0;
25+
26+
extern "C" {
27+
#[thread_local]
28+
// Foreign static. OK.
29+
static C: u32;
30+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error: attribute should be applied to a static
2+
--> $DIR/non-static.rs:4:1
3+
|
4+
LL | #[thread_local]
5+
| ^^^^^^^^^^^^^^^
6+
LL |
7+
LL | const A: u32 = 0;
8+
| ----------------- not a static
9+
10+
error: attribute should be applied to a static
11+
--> $DIR/non-static.rs:8:1
12+
|
13+
LL | #[thread_local]
14+
| ^^^^^^^^^^^^^^^
15+
LL |
16+
LL | / fn main() {
17+
LL | | #[thread_local] || {};
18+
LL | |
19+
LL | | }
20+
| |_- not a static
21+
22+
error: attribute should be applied to a static
23+
--> $DIR/non-static.rs:11:5
24+
|
25+
LL | #[thread_local] || {};
26+
| ^^^^^^^^^^^^^^^ ----- not a static
27+
28+
error: attribute should be applied to a static
29+
--> $DIR/non-static.rs:16:5
30+
|
31+
LL | #[thread_local]
32+
| ^^^^^^^^^^^^^^^
33+
LL |
34+
LL | a: String,
35+
| --------- not a static
36+
37+
error: aborting due to 4 previous errors
38+

0 commit comments

Comments
 (0)