Skip to content

Commit d2ef8ec

Browse files
committed
Auto merge of #155193 - JonathanBrouwer:args_used_check, r=<try>
Check arguments of attributes where no arguments are expected
2 parents 540f43a + e179568 commit d2ef8ec

File tree

16 files changed

+517
-21
lines changed

16 files changed

+517
-21
lines changed

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<S: Stage>(
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
@@ -25,7 +25,7 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
2525
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
2626
let single = cx.single_element_list(args, cx.attr_span)?;
2727

28-
let res = match single.meta_item().and_then(|i| i.path().word().map(|i| i.name)) {
28+
let res = match single.meta_item_no_args().and_then(|i| i.path().word().map(|i| i.name)) {
2929
Some(sym::size) => OptimizeAttr::Size,
3030
Some(sym::speed) => OptimizeAttr::Speed,
3131
Some(sym::none) => OptimizeAttr::DoNotOptimize,
@@ -80,7 +80,7 @@ impl<S: Stage> SingleAttributeParser<S> 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
};
@@ -375,7 +375,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
375375
return;
376376
};
377377

378-
match l.meta_item().and_then(|i| i.path().word_sym()) {
378+
match l.meta_item_no_args().and_then(|i| i.path().word_sym()) {
379379
Some(sym::compiler) => {
380380
if !cx.features().used_with_arg() {
381381
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<S: Stage> AttributeParser<S> for OnConstParser {
1717
template!(List: &[r#"/*opt*/ message = "...", /*opt*/ label = "...", /*opt*/ note = "...""#]),
1818
|this, cx, args| {
1919
if !cx.features().diagnostic_on_const() {
20+
args.ignore_args();
2021
return;
2122
}
2223

compiler/rustc_attr_parsing/src/attributes/doc.rs

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

205205
// FIXME: convert list into a Vec of `AttributeKind` because current code is awful.
206206
for attr in list.mixed() {
207+
// Arguments of `attr` are checked via the span, so can be safely ignored
208+
attr.ignore_args();
207209
self.attribute.test_attrs.push(attr.span());
208210
}
209211
}

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<S: Stage> SingleAttributeParser<S> 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<'_, '_, S>, _: &ArgParser) -> Option<AttributeKind> {
17+
fn convert(_: &mut AcceptContext<'_, '_, S>, 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
@@ -42,7 +42,7 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
4242
return None;
4343
};
4444

45-
match l.meta_item().and_then(|i| i.path().word_sym()) {
45+
match l.meta_item_no_args().and_then(|i| i.path().word_sym()) {
4646
Some(sym::always) => {
4747
Some(AttributeKind::Inline(InlineAttr::Always, cx.attr_span))
4848
}

compiler/rustc_attr_parsing/src/attributes/instruction_set.rs

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

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

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

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

compiler/rustc_attr_parsing/src/attributes/rustc_dump.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<S: Stage> CombineAttributeParser<S> for RustcDumpLayoutParser {
110110

111111
let mut result = Vec::new();
112112
for item in items.mixed() {
113-
let Some(arg) = item.meta_item() else {
113+
let Some(arg) = item.meta_item_no_args() else {
114114
cx.adcx().expected_not_literal(item.span());
115115
continue;
116116
};

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcMustImplementOneOfParser {
4949

5050
let mut errored = false;
5151
for argument in inputs {
52-
let Some(meta) = argument.meta_item() else {
52+
let Some(meta) = argument.meta_item_no_args() else {
5353
cx.adcx().expected_identifier(argument.span());
5454
return None;
5555
};
@@ -945,7 +945,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcIfThisChangedParser {
945945
cx.adcx().expected_single_argument(attr_span, list.len());
946946
return None;
947947
};
948-
let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
948+
let Some(ident) = item.meta_item_no_args().and_then(|item| item.ident()) else {
949949
cx.adcx().expected_identifier(item.span());
950950
return None;
951951
};
@@ -1003,7 +1003,7 @@ impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
10031003
cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
10041004
}
10051005
let item = cx.single_element_list(args, cx.attr_span)?;
1006-
let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
1006+
let Some(ident) = item.meta_item_no_args().and_then(|item| item.ident()) else {
10071007
cx.adcx().expected_identifier(item.span());
10081008
return None;
10091009
};

0 commit comments

Comments
 (0)