Skip to content

Commit f8b5f9c

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

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
@@ -347,3 +347,50 @@ impl<S: Stage> CombineAttributeParser<S> for FeatureParser {
347347
res
348348
}
349349
}
350+
351+
pub(crate) struct RegisterToolParser;
352+
353+
impl<S: Stage> CombineAttributeParser<S> for RegisterToolParser {
354+
const PATH: &[Symbol] = &[sym::register_tool];
355+
type Item = Ident;
356+
const CONVERT: ConvertFn<Self::Item> = AttributeKind::RegisterTool;
357+
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
358+
const TEMPLATE: AttributeTemplate = template!(List: &["tool1, tool2, ..."]);
359+
360+
fn extend(
361+
cx: &mut AcceptContext<'_, '_, S>,
362+
args: &ArgParser,
363+
) -> impl IntoIterator<Item = Self::Item> {
364+
let ArgParser::List(list) = args else {
365+
cx.expected_list(cx.attr_span, args);
366+
return Vec::new();
367+
};
368+
369+
if list.is_empty() {
370+
cx.warn_empty_attribute(cx.attr_span);
371+
}
372+
373+
let mut res = Vec::new();
374+
375+
for elem in list.mixed() {
376+
let Some(elem) = elem.meta_item() else {
377+
cx.expected_identifier(elem.span());
378+
continue;
379+
};
380+
if let Err(arg_span) = elem.args().no_args() {
381+
cx.expected_no_args(arg_span);
382+
continue;
383+
}
384+
385+
let path = elem.path();
386+
let Some(ident) = path.word() else {
387+
cx.expected_identifier(path.span());
388+
continue;
389+
};
390+
391+
res.push(ident);
392+
}
393+
394+
res
395+
}
396+
}

compiler/rustc_attr_parsing/src/context.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ attribute_parsers!(
160160
Combine<FeatureParser>,
161161
Combine<ForceTargetFeatureParser>,
162162
Combine<LinkParser>,
163+
Combine<RegisterToolParser>,
163164
Combine<ReprParser>,
164165
Combine<RustcCleanParser>,
165166
Combine<RustcLayoutParser>,

compiler/rustc_hir/src/attrs/data_structures.rs

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

1142+
/// Represents `#[register_tool]`
1143+
RegisterTool(ThinVec<Ident>, Span),
1144+
11421145
/// Represents [`#[repr]`](https://doc.rust-lang.org/stable/reference/type-layout.html#representations).
11431146
Repr {
11441147
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
@@ -92,6 +92,7 @@ impl AttributeKind {
9292
ProfilerRuntime => No,
9393
RecursionLimit { .. } => No,
9494
ReexportTestHarnessMain(..) => No,
95+
RegisterTool(..) => No,
9596
Repr { .. } => No,
9697
RustcAbi { .. } => No,
9798
RustcAllocator => No,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
289289
| AttributeKind::ProfilerRuntime
290290
| AttributeKind::RecursionLimit { .. }
291291
| AttributeKind::ReexportTestHarnessMain(..)
292+
| AttributeKind::RegisterTool(..)
292293
// handled below this loop and elsewhere
293294
| AttributeKind::Repr { .. }
294295
| AttributeKind::RustcAbi { .. }
@@ -407,8 +408,7 @@ impl<'tcx> CheckAttrVisitor<'tcx> {
407408
| sym::rustc_layout
408409
| sym::rustc_autodiff
409410
// crate-level attrs, are checked below
410-
| sym::feature
411-
| sym::register_tool,
411+
| sym::feature,
412412
..
413413
] => {}
414414
[name, rest@..] => {

0 commit comments

Comments
 (0)