Skip to content

Commit bdb51ea

Browse files
Rollup merge of #155193 - JonathanBrouwer:args_used_check, r=jdonszelmann
Check arguments of attributes where no arguments are expected This PR does the following: - Add a debug assertion to `rustc_attr_parsing`, to ensure we never forget to check the arguments of a meta item again - Removes the unused `#[derive(Clone)]` from `ArgParser` as that would break this debug assertion - **[BREAKING]** Properly check that `#[inline(always(...))]` gets no arguments - **[BREAKING]** Properly check that `#[instruction_set(arm::a32(...))]` gets no arguments - **[BREAKING]** Properly check that `#[macro_export(local_inner_macros(...))]` gets no arguments. Fixes #154977 - **[BREAKING]** Properly check that `#[used(compiler(...))]` gets no arguments. - Properly check that `#[optimize(size(...))]` gets no arguments. - Properly check that `#[coverage(on(...))]` gets no arguments. - Properly check that `#[rustc_dump_layout(debug(...))]` gets no arguments. - Properly check that `#[rustc_abi(debug(...))]` gets no arguments. - Properly check that `#![test_runner(arg(...))]` gets no arguments. - Properly check that `#[rustc_must_implement_one_of(arg(...))]` gets no arguments. - Properly check that `#[allow_internal_unstable(arg(...))]` gets no arguments. - Properly check that `#[unstable_feature_bound(arg(...))]` gets no arguments. - Properly check that `#[rustc_allow_const_fn_unstable(arg(...))]` gets no arguments. - Properly check that `#[rustc_if_this_changed(arg(...))]` gets no arguments. - Properly check that `#[rustc_then_this_would_need(arg(...))]` gets no arguments. r? @jdonszelmann
2 parents 48a5e56 + 3fcbca4 commit bdb51ea

18 files changed

Lines changed: 562 additions & 20 deletions

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ fn parse_unstable(
9191

9292
for param in list.mixed() {
9393
let param_span = param.span();
94-
if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
94+
if let Some(ident) = param.meta_item_no_args().and_then(|i| i.path().word()) {
9595
res.push(ident.name);
9696
} else {
9797
cx.emit_err(session_diagnostics::ExpectsFeatures {

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl SingleAttributeParser for OptimizeParser {
2626
fn convert(cx: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
2727
let single = cx.expect_single_element_list(args, cx.attr_span)?;
2828

29-
let res = match single.meta_item().and_then(|i| i.path().word().map(|i| i.name)) {
29+
let res = match single.meta_item_no_args().and_then(|i| i.path().word().map(|i| i.name)) {
3030
Some(sym::size) => OptimizeAttr::Size,
3131
Some(sym::speed) => OptimizeAttr::Speed,
3232
Some(sym::none) => OptimizeAttr::DoNotOptimize,
@@ -80,7 +80,7 @@ impl SingleAttributeParser for CoverageParser {
8080
let mut fail_incorrect_argument =
8181
|span| cx.adcx().expected_specific_argument(span, &[sym::on, sym::off]);
8282

83-
let Some(arg) = arg.meta_item() else {
83+
let Some(arg) = arg.meta_item_no_args() else {
8484
fail_incorrect_argument(arg.span());
8585
return None;
8686
};
@@ -373,7 +373,7 @@ impl AttributeParser for UsedParser {
373373
return;
374374
};
375375

376-
match l.meta_item().and_then(|i| i.path().word_sym()) {
376+
match l.meta_item_no_args().and_then(|i| i.path().word_sym()) {
377377
Some(sym::compiler) => {
378378
if !cx.features().used_with_arg() {
379379
feature_err(

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_const.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ impl AttributeParser for OnConstParser {
1717
|this, cx, args| {
1818
if !cx.features().diagnostic_on_const() {
1919
// `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
20+
args.ignore_args();
2021
return;
2122
}
2223

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_move.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl OnMoveParser {
2020
fn parse<'sess>(&mut self, cx: &mut AcceptContext<'_, 'sess>, args: &ArgParser, mode: Mode) {
2121
if !cx.features().diagnostic_on_move() {
2222
// `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
23+
args.ignore_args();
2324
return;
2425
}
2526

compiler/rustc_attr_parsing/src/attributes/diagnostic/on_unknown.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ impl OnUnknownParser {
1818
&& !features.diagnostic_on_unknown()
1919
{
2020
// `UnknownDiagnosticAttribute` is emitted in rustc_resolve/macros.rs
21+
args.ignore_args();
2122
return;
2223
}
2324
let span = cx.attr_span;

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ impl DocParser {
190190

191191
// FIXME: convert list into a Vec of `AttributeKind` because current code is awful.
192192
for attr in list.mixed() {
193+
// Arguments of `attr` are checked via the span, so can be safely ignored
194+
attr.ignore_args();
193195
self.attribute.test_attrs.push(attr.span());
194196
}
195197
}

compiler/rustc_attr_parsing/src/attributes/dummy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ impl SingleAttributeParser for RustcDummyParser {
1414
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(ALL_TARGETS);
1515
const TEMPLATE: AttributeTemplate = template!(Word); // Anything, really
1616

17-
fn convert(_: &mut AcceptContext<'_, '_>, _: &ArgParser) -> Option<AttributeKind> {
17+
fn convert(_: &mut AcceptContext<'_, '_>, args: &ArgParser) -> Option<AttributeKind> {
18+
args.ignore_args();
1819
Some(AttributeKind::RustcDummy)
1920
}
2021
}

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl SingleAttributeParser for InlineParser {
3939
ArgParser::List(list) => {
4040
let l = cx.expect_single(list)?;
4141

42-
match l.meta_item().and_then(|i| i.path().word_sym()) {
42+
match l.meta_item_no_args().and_then(|i| i.path().word_sym()) {
4343
Some(sym::always) => {
4444
Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
4545
}

compiler/rustc_attr_parsing/src/attributes/instruction_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl SingleAttributeParser for InstructionSetParser {
2121
const POSSIBLE_ARM_SYMBOLS: &[Symbol] = &[sym::a32, sym::t32];
2222
let maybe_meta_item = cx.expect_single_element_list(args, cx.attr_span)?;
2323

24-
let Some(meta_item) = maybe_meta_item.meta_item() else {
24+
let Some(meta_item) = maybe_meta_item.meta_item_no_args() else {
2525
cx.adcx().expected_specific_argument(maybe_meta_item.span(), POSSIBLE_SYMBOLS);
2626
return None;
2727
};

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl SingleAttributeParser for MacroExportParser {
145145
cx.adcx().warn_ill_formed_attribute_input(INVALID_MACRO_EXPORT_ARGUMENTS);
146146
return None;
147147
};
148-
match l.meta_item().and_then(|i| i.path().word_sym()) {
148+
match l.meta_item_no_args().and_then(|i| i.path().word_sym()) {
149149
Some(sym::local_inner_macros) => true,
150150
_ => {
151151
cx.adcx().warn_ill_formed_attribute_input(INVALID_MACRO_EXPORT_ARGUMENTS);

0 commit comments

Comments
 (0)