Skip to content

Commit 71857c8

Browse files
Port #[register_tool] to the new attribute parsers
Co-authored-by: Jana Dönszelmann <jana@donsz.nl>
1 parent 1500f0f commit 71857c8

5 files changed

Lines changed: 54 additions & 2 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,50 @@ impl<S: Stage> NoArgsAttributeParser<S> for DefaultLibAllocatorParser {
301301
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Crate)]);
302302
const CREATE: fn(Span) -> AttributeKind = |_| AttributeKind::DefaultLibAllocator;
303303
}
304+
305+
pub(crate) struct RegisterToolParser;
306+
307+
impl<S: Stage> CombineAttributeParser<S> for RegisterToolParser {
308+
const PATH: &[Symbol] = &[sym::register_tool];
309+
type Item = Ident;
310+
const CONVERT: ConvertFn<Self::Item> = AttributeKind::RegisterTool;
311+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
312+
const TEMPLATE: AttributeTemplate = template!(List: &["tool1, tool2, ..."]);
313+
314+
fn extend(
315+
cx: &mut AcceptContext<'_, '_, S>,
316+
args: &ArgParser,
317+
) -> impl IntoIterator<Item = Self::Item> {
318+
let ArgParser::List(list) = args else {
319+
cx.expected_list(cx.attr_span, args);
320+
return Vec::new();
321+
};
322+
323+
if list.is_empty() {
324+
cx.warn_empty_attribute(cx.attr_span);
325+
}
326+
327+
let mut res = Vec::new();
328+
329+
for elem in list.mixed() {
330+
let Some(elem) = elem.meta_item() else {
331+
cx.expected_identifier(elem.span());
332+
continue;
333+
};
334+
if let Err(arg_span) = elem.args().no_args() {
335+
cx.expected_no_args(arg_span);
336+
continue;
337+
}
338+
339+
let path = elem.path();
340+
let Some(ident) = path.word() else {
341+
cx.expected_identifier(path.span());
342+
continue;
343+
};
344+
345+
res.push(ident);
346+
}
347+
348+
res
349+
}
350+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ attribute_parsers!(
159159
Combine<DebuggerViualizerParser>,
160160
Combine<ForceTargetFeatureParser>,
161161
Combine<LinkParser>,
162+
Combine<RegisterToolParser>,
162163
Combine<ReprParser>,
163164
Combine<RustcCleanParser>,
164165
Combine<RustcLayoutParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,9 @@ pub enum AttributeKind {
11361136
/// Represents `#[reexport_test_harness_main]`
11371137
ReexportTestHarnessMain(Symbol),
11381138

1139+
/// Represents `#[register_tool]`
1140+
RegisterTool(ThinVec<Ident>, Span),
1141+
11391142
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
11401143
Repr {
11411144
reprs: ThinVec<(ReprAttr, Span)>,

compiler/rustc_hir/src/attrs/encode_cross_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ impl AttributeKind {
9191
ProfilerRuntime => No,
9292
RecursionLimit { .. } => No,
9393
ReexportTestHarnessMain(..) => No,
94+
RegisterTool(..) => No,
9495
Repr { .. } => No,
9596
RustcAbi { .. } => No,
9697
RustcAllocator => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
288288
| AttributeKind::ProfilerRuntime
289289
| AttributeKind::RecursionLimit { .. }
290290
| AttributeKind::ReexportTestHarnessMain(..)
291+
| AttributeKind::RegisterTool(..)
291292
// handled below this loop and elsewhere
292293
| AttributeKind::Repr { .. }
293294
| AttributeKind::RustcAbi { .. }
@@ -406,8 +407,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
406407
| sym::rustc_autodiff
407408
| sym::rustc_inherit_overflow_checks
408409
// crate-level attrs, are checked below
409-
| sym::feature
410-
| sym::register_tool,
410+
| sym::feature,
411411
..
412412
] => {}
413413
[name, rest@..] => {

0 commit comments

Comments
 (0)