Skip to content

Commit 120f0d4

Browse files
committed
Port #[thread_local] to attribute parser
1 parent d0835ad commit 120f0d4

File tree

11 files changed

+48
-58
lines changed

11 files changed

+48
-58
lines changed

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,16 @@ impl<S: Stage> SingleAttributeParser<S> for SanitizeParser {
690690
}
691691
}
692692

693+
pub(crate) struct ThreadLocalParser;
694+
695+
impl<S: Stage> NoArgsAttributeParser<S> for ThreadLocalParser {
696+
const PATH: &[Symbol] = &[sym::thread_local];
697+
const ON_DUPLICATE: OnDuplicate<S> = OnDuplicate::WarnButFutureError;
698+
const ALLOWED_TARGETS: AllowedTargets =
699+
AllowedTargets::AllowList(&[Allow(Target::Static), Allow(Target::ForeignStatic)]);
700+
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::ThreadLocal;
701+
}
702+
693703
pub(crate) struct RustcPassIndirectlyInNonRusticAbisParser;
694704

695705
impl<S: Stage> NoArgsAttributeParser<S> for RustcPassIndirectlyInNonRusticAbisParser {

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::attributes::codegen_attrs::{
2323
ColdParser, CoverageParser, EiiExternItemParser, ExportNameParser, ForceTargetFeatureParser,
2424
NakedParser, NoMangleParser, ObjcClassParser, ObjcSelectorParser, OptimizeParser,
2525
RustcPassIndirectlyInNonRusticAbisParser, SanitizeParser, TargetFeatureParser,
26-
TrackCallerParser, UsedParser,
26+
ThreadLocalParser, TrackCallerParser, UsedParser,
2727
};
2828
use crate::attributes::confusables::ConfusablesParser;
2929
use crate::attributes::crate_level::{
@@ -269,6 +269,7 @@ attribute_parsers!(
269269
Single<WithoutArgs<RustcShouldNotBeCalledOnConstItems>>,
270270
Single<WithoutArgs<SpecializationTraitParser>>,
271271
Single<WithoutArgs<StdInternalSymbolParser>>,
272+
Single<WithoutArgs<ThreadLocalParser>>,
272273
Single<WithoutArgs<TrackCallerParser>>,
273274
Single<WithoutArgs<TypeConstParser>>,
274275
Single<WithoutArgs<UnsafeSpecializationMarkerParser>>,

compiler/rustc_codegen_ssa/src/codegen_attrs.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,9 @@ fn process_builtin_attrs(
350350
codegen_fn_attrs.flags |= CodegenFnAttrFlags::EXTERNALLY_IMPLEMENTABLE_ITEM;
351351
}
352352
}
353+
AttributeKind::ThreadLocal => {
354+
codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL
355+
}
353356
_ => {}
354357
}
355358
}
@@ -366,7 +369,6 @@ fn process_builtin_attrs(
366369
sym::rustc_allocator_zeroed => {
367370
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
368371
}
369-
sym::thread_local => codegen_fn_attrs.flags |= CodegenFnAttrFlags::THREAD_LOCAL,
370372
sym::instruction_set => {
371373
codegen_fn_attrs.instruction_set = parse_instruction_set_attr(tcx, attr)
372374
}

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,9 @@ pub enum AttributeKind {
10101010
/// `#[unsafe(force_target_feature(enable = "...")]`.
10111011
TargetFeature { features: ThinVec<(Symbol, Span)>, attr_span: Span, was_forced: bool },
10121012

1013+
/// Represents `#[thread_local]`
1014+
ThreadLocal,
1015+
10131016
/// Represents `#[track_caller]`
10141017
TrackCaller(Span),
10151018

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ impl AttributeKind {
114114
Stability { .. } => Yes,
115115
StdInternalSymbol(..) => No,
116116
TargetFeature { .. } => No,
117+
ThreadLocal => No,
117118
TrackCaller(..) => Yes,
118119
TypeConst(..) => Yes,
119120
TypeLengthLimit { .. } => No,

compiler/rustc_passes/messages.ftl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,6 @@ passes_sanitize_attribute_not_allowed =
484484
.no_body = function has no body
485485
.help = sanitize attribute can be applied to a function (with body), impl block, or module
486486
487-
passes_should_be_applied_to_static =
488-
attribute should be applied to a static
489-
.label = not a static
490-
491487
passes_should_be_applied_to_trait =
492488
attribute should be applied to a trait
493489
.label = not a trait

compiler/rustc_passes/src/check_attr.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
297297
| AttributeKind::RustcPassIndirectlyInNonRusticAbis(..)
298298
| AttributeKind::PinV2(..)
299299
| AttributeKind::WindowsSubsystem(..)
300+
| AttributeKind::ThreadLocal
300301
) => { /* do nothing */ }
301302
Attribute::Unparsed(attr_item) => {
302303
style = Some(attr_item.style);
@@ -310,7 +311,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
310311
[sym::diagnostic, sym::on_const, ..] => {
311312
self.check_diagnostic_on_const(attr.span(), hir_id, target, item)
312313
}
313-
[sym::thread_local, ..] => self.check_thread_local(attr, span, target),
314314
[sym::rustc_clean, ..]
315315
| [sym::rustc_dirty, ..]
316316
| [sym::rustc_if_this_changed, ..]
@@ -768,19 +768,6 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
768768
}
769769
}
770770

771-
/// Checks if the `#[thread_local]` attribute on `item` is valid.
772-
fn check_thread_local(&self, attr: &Attribute, span: Span, target: Target) {
773-
match target {
774-
Target::ForeignStatic | Target::Static => {}
775-
_ => {
776-
self.dcx().emit_err(errors::AttrShouldBeAppliedToStatic {
777-
attr_span: attr.span(),
778-
defn_span: span,
779-
});
780-
}
781-
}
782-
}
783-
784771
fn check_doc_alias_value(&self, span: Span, hir_id: HirId, target: Target, alias: Symbol) {
785772
if let Some(location) = match target {
786773
Target::AssocTy => {

compiler/rustc_passes/src/errors.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,6 @@ pub(crate) struct AttrShouldBeAppliedToTrait {
9898
pub defn_span: Span,
9999
}
100100

101-
#[derive(Diagnostic)]
102-
#[diag(passes_should_be_applied_to_static)]
103-
pub(crate) struct AttrShouldBeAppliedToStatic {
104-
#[primary_span]
105-
pub attr_span: Span,
106-
#[label]
107-
pub defn_span: Span,
108-
}
109-
110101
#[derive(Diagnostic)]
111102
#[diag(passes_doc_alias_bad_location)]
112103
pub(crate) struct DocAliasBadLocation<'a> {

tests/ui/attributes/malformed-attrs.stderr

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ LL | #[forbid(lint1, lint2, ...)]
141141
LL | #[forbid(lint1, lint2, lint3, reason = "...")]
142142
| +++++++++++++++++++++++++++++++++++++
143143

144-
error: malformed `thread_local` attribute input
145-
--> $DIR/malformed-attrs.rs:210:1
146-
|
147-
LL | #[thread_local()]
148-
| ^^^^^^^^^^^^^^^^^ help: must be of the form: `#[thread_local]`
149-
150144
error: the `#[proc_macro]` attribute is only usable with crates of the `proc-macro` crate type
151145
--> $DIR/malformed-attrs.rs:105:1
152146
|
@@ -618,6 +612,15 @@ LL | #[non_exhaustive = 1]
618612
| | didn't expect any arguments here
619613
| help: must be of the form: `#[non_exhaustive]`
620614

615+
error[E0565]: malformed `thread_local` attribute input
616+
--> $DIR/malformed-attrs.rs:210:1
617+
|
618+
LL | #[thread_local()]
619+
| ^^^^^^^^^^^^^^--^
620+
| | |
621+
| | didn't expect any arguments here
622+
| help: must be of the form: `#[thread_local]`
623+
621624
error[E0565]: malformed `no_link` attribute input
622625
--> $DIR/malformed-attrs.rs:214:1
623626
|

tests/ui/thread-local/non-static.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
#![feature(thread_local)]
33

44
#[thread_local]
5-
//~^ ERROR attribute should be applied to a static
5+
//~^ ERROR `#[thread_local]` attribute cannot be used on constants
66
const A: u32 = 0;
77

88
#[thread_local]
9-
//~^ ERROR attribute should be applied to a static
9+
//~^ ERROR `#[thread_local]` attribute cannot be used on functions
1010
fn main() {
1111
#[thread_local] || {};
12-
//~^ ERROR attribute should be applied to a static
12+
//~^ ERROR `#[thread_local]` attribute cannot be used on closures
1313
}
1414

1515
struct S {
1616
#[thread_local]
17-
//~^ ERROR attribute should be applied to a static
17+
//~^ ERROR `#[thread_local]` attribute cannot be used on struct fields
1818
a: String,
1919
b: String,
2020
}

0 commit comments

Comments
 (0)