Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reject #[thread_local] attribute on non-static items #95006

Merged
merged 1 commit into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions compiler/rustc_passes/src/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl CheckAttrVisitor<'_> {
self.check_rustc_must_implement_one_of(attr, span, target)
}
sym::target_feature => self.check_target_feature(hir_id, attr, span, target),
sym::thread_local => self.check_thread_local(attr, span, target),
sym::track_caller => {
self.check_track_caller(hir_id, attr.span, attrs, span, target)
}
Expand Down Expand Up @@ -521,6 +522,21 @@ impl CheckAttrVisitor<'_> {
}
}

/// Checks if the `#[thread_local]` attribute on `item` is valid. Returns `true` if valid.
fn check_thread_local(&self, attr: &Attribute, span: Span, target: Target) -> bool {
match target {
Target::ForeignStatic | Target::Static => true,
_ => {
self.tcx
.sess
.struct_span_err(attr.span, "attribute should be applied to a static")
.span_label(span, "not a static")
.emit();
false
}
}
}

fn doc_attr_str_error(&self, meta: &NestedMetaItem, attr_name: &str) {
self.tcx
.sess
Expand Down
30 changes: 30 additions & 0 deletions src/test/ui/thread-local/non-static.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Check that #[thread_local] attribute is rejected on non-static items.
#![feature(thread_local)]

#[thread_local]
//~^ ERROR attribute should be applied to a static
const A: u32 = 0;

#[thread_local]
//~^ ERROR attribute should be applied to a static
fn main() {
#[thread_local] || {};
//~^ ERROR attribute should be applied to a static
}

struct S {
#[thread_local]
//~^ ERROR attribute should be applied to a static
a: String,
b: String,
}

#[thread_local]
// Static. OK.
static B: u32 = 0;

extern "C" {
#[thread_local]
// Foreign static. OK.
static C: u32;
}
38 changes: 38 additions & 0 deletions src/test/ui/thread-local/non-static.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
error: attribute should be applied to a static
--> $DIR/non-static.rs:4:1
|
LL | #[thread_local]
| ^^^^^^^^^^^^^^^
LL |
LL | const A: u32 = 0;
| ----------------- not a static

error: attribute should be applied to a static
--> $DIR/non-static.rs:8:1
|
LL | #[thread_local]
| ^^^^^^^^^^^^^^^
LL |
LL | / fn main() {
LL | | #[thread_local] || {};
LL | |
LL | | }
| |_- not a static

error: attribute should be applied to a static
--> $DIR/non-static.rs:11:5
|
LL | #[thread_local] || {};
| ^^^^^^^^^^^^^^^ ----- not a static

error: attribute should be applied to a static
--> $DIR/non-static.rs:16:5
|
LL | #[thread_local]
| ^^^^^^^^^^^^^^^
LL |
LL | a: String,
| --------- not a static

error: aborting due to 4 previous errors