1
Fork 0

Rollup merge of #95006 - tmiasko:thread-local-static, r=wesleywiser

Reject `#[thread_local]` attribute on non-static items
This commit is contained in:
Dylan DPC 2022-04-16 19:42:02 +02:00 committed by GitHub
commit 3dced80298
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 84 additions and 0 deletions

View file

@ -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)
}
@ -523,6 +524,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