Skip to content

Commit 3dced80

Browse files
authored
Rollup merge of #95006 - tmiasko:thread-local-static, r=wesleywiser
Reject `#[thread_local]` attribute on non-static items
2 parents 22d5546 + 5e76103 commit 3dced80

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
}
@@ -523,6 +524,21 @@ impl CheckAttrVisitor<'_> {
523524
}
524525
}
525526

527+
/// Checks if the `#[thread_local]` attribute on `item` is valid. Returns `true` if valid.
528+
fn check_thread_local(&self, attr: &Attribute, span: Span, target: Target) -> bool {
529+
match target {
530+
Target::ForeignStatic | Target::Static => true,
531+
_ => {
532+
self.tcx
533+
.sess
534+
.struct_span_err(attr.span, "attribute should be applied to a static")
535+
.span_label(span, "not a static")
536+
.emit();
537+
false
538+
}
539+
}
540+
}
541+
526542
fn doc_attr_str_error(&self, meta: &NestedMetaItem, attr_name: &str) {
527543
self.tcx
528544
.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)