Skip to content

Commit 97dd613

Browse files
Rollup merge of #155696 - scrabsha:push-kxqstpltlwzn, r=JonathanBrouwer
Add a higher-level API for parsing attributes
2 parents 2c639cc + 067ef3d commit 97dd613

23 files changed

Lines changed: 110 additions & 151 deletions

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fn parse_unstable<S: Stage>(
8181
) -> impl IntoIterator<Item = Symbol> {
8282
let mut res = Vec::new();
8383

84-
let Some(list) = args.list() else {
84+
let Some(list) = args.as_list() else {
8585
cx.emit_err(session_diagnostics::ExpectsFeatureList {
8686
span: cx.attr_span,
8787
name: symbol.to_ident_string(),

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,9 @@ pub fn parse_cfg<S: Stage>(
4444
cx: &mut AcceptContext<'_, '_, S>,
4545
args: &ArgParser,
4646
) -> Option<CfgEntry> {
47-
let ArgParser::List(list) = args else {
48-
let attr_span = cx.attr_span;
49-
cx.adcx().expected_list(attr_span, args);
50-
return None;
51-
};
47+
let list = cx.expect_list(args, cx.attr_span)?;
5248

53-
let Some(single) = list.single() else {
49+
let Some(single) = list.as_single() else {
5450
let target = cx.target;
5551
let mut adcx = cx.adcx();
5652
if list.is_empty() {
@@ -93,7 +89,7 @@ pub fn parse_cfg_entry<S: Stage>(
9389
MetaItemOrLitParser::MetaItemParser(meta) => match meta.args() {
9490
ArgParser::List(list) => match meta.path().word_sym() {
9591
Some(sym::not) => {
96-
let Some(single) = list.single() else {
92+
let Some(single) = list.as_single() else {
9793
return Err(cx.adcx().expected_single_argument(list.span, list.len()));
9894
};
9995
CfgEntry::Not(Box::new(parse_cfg_entry(cx, single)?), list.span)
@@ -136,7 +132,7 @@ fn parse_cfg_entry_version<S: Stage>(
136132
meta_span: Span,
137133
) -> Result<CfgEntry, ErrorGuaranteed> {
138134
try_gate_cfg(sym::version, meta_span, cx.sess(), cx.features_option());
139-
let Some(version) = list.single() else {
135+
let Some(version) = list.as_single() else {
140136
return Err(
141137
cx.emit_err(session_diagnostics::ExpectedSingleVersionLiteral { span: list.span })
142138
);

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
2424
const TEMPLATE: AttributeTemplate = template!(List: &["size", "speed", "none"]);
2525

2626
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
27-
let single = cx.single_element_list(args, cx.attr_span)?;
27+
let single = cx.expect_single_element_list(args, cx.attr_span)?;
2828

2929
let res = match single.meta_item().and_then(|i| i.path().word().map(|i| i.name)) {
3030
Some(sym::size) => OptimizeAttr::Size,
@@ -75,7 +75,7 @@ impl<S: Stage> SingleAttributeParser<S> for CoverageParser {
7575
const TEMPLATE: AttributeTemplate = template!(OneOf: &[sym::off, sym::on]);
7676

7777
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
78-
let arg = cx.single_element_list(args, cx.attr_span)?;
78+
let arg = cx.expect_single_element_list(args, cx.attr_span)?;
7979

8080
let mut fail_incorrect_argument =
8181
|span| cx.adcx().expected_specific_argument(span, &[sym::on, sym::off]);
@@ -371,8 +371,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
371371
let used_by = match args {
372372
ArgParser::NoArgs => UsedBy::Default,
373373
ArgParser::List(list) => {
374-
let Some(l) = list.single() else {
375-
cx.adcx().expected_single_argument(list.span, list.len());
374+
let Some(l) = cx.expect_single(list) else {
376375
return;
377376
};
378377

@@ -463,9 +462,7 @@ fn parse_tf_attribute<S: Stage>(
463462
args: &ArgParser,
464463
) -> impl IntoIterator<Item = (Symbol, Span)> {
465464
let mut features = Vec::new();
466-
let ArgParser::List(list) = args else {
467-
let attr_span = cx.attr_span;
468-
cx.adcx().expected_list(attr_span, args);
465+
let Some(list) = cx.expect_list(args, cx.attr_span) else {
469466
return features;
470467
};
471468
if list.is_empty() {
@@ -588,11 +585,7 @@ impl<S: Stage> SingleAttributeParser<S> for SanitizeParser {
588585
]);
589586

590587
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
591-
let Some(list) = args.list() else {
592-
let attr_span = cx.attr_span;
593-
cx.adcx().expected_list(attr_span, args);
594-
return None;
595-
};
588+
let list = cx.expect_list(args, cx.attr_span)?;
596589

597590
let mut on_set = SanitizerSet::empty();
598591
let mut off_set = SanitizerSet::empty();
@@ -719,11 +712,7 @@ impl<S: Stage> SingleAttributeParser<S> for PatchableFunctionEntryParser {
719712
const TEMPLATE: AttributeTemplate = template!(List: &["prefix_nops = m, entry_nops = n"]);
720713

721714
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
722-
let Some(meta_item_list) = args.list() else {
723-
let attr_span = cx.attr_span;
724-
cx.adcx().expected_list(attr_span, args);
725-
return None;
726-
};
715+
let meta_item_list = cx.expect_list(args, cx.attr_span)?;
727716

728717
let mut prefix = None;
729718
let mut entry = None;

compiler/rustc_attr_parsing/src/attributes/confusables.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ impl<S: Stage> AttributeParser<S> for ConfusablesParser {
1212
&[sym::rustc_confusables],
1313
template!(List: &[r#""name1", "name2", ..."#]),
1414
|this, cx, args| {
15-
let Some(list) = args.list() else {
16-
let attr_span = cx.attr_span;
17-
cx.adcx().expected_list(attr_span, args);
18-
return;
19-
};
15+
let Some(list) = cx.expect_list(args, cx.attr_span) else { return };
2016

2117
if list.is_empty() {
2218
cx.emit_err(EmptyConfusables { span: cx.attr_span });

compiler/rustc_attr_parsing/src/attributes/crate_level.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,7 @@ impl<S: Stage> CombineAttributeParser<S> for FeatureParser {
314314
cx: &mut AcceptContext<'_, '_, S>,
315315
args: &ArgParser,
316316
) -> impl IntoIterator<Item = Self::Item> {
317-
let ArgParser::List(list) = args else {
318-
let attr_span = cx.attr_span;
319-
cx.adcx().expected_list(attr_span, args);
317+
let Some(list) = cx.expect_list(args, cx.attr_span) else {
320318
return Vec::new();
321319
};
322320

@@ -362,9 +360,7 @@ impl<S: Stage> CombineAttributeParser<S> for RegisterToolParser {
362360
cx: &mut AcceptContext<'_, '_, S>,
363361
args: &ArgParser,
364362
) -> impl IntoIterator<Item = Self::Item> {
365-
let ArgParser::List(list) = args else {
366-
let attr_span = cx.attr_span;
367-
cx.adcx().expected_list(attr_span, args);
363+
let Some(list) = cx.expect_list(args, cx.attr_span) else {
368364
return Vec::new();
369365
};
370366

compiler/rustc_attr_parsing/src/attributes/debugger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl<S: Stage> CombineAttributeParser<S> for DebuggerViualizerParser {
2020
cx: &mut AcceptContext<'_, '_, S>,
2121
args: &ArgParser,
2222
) -> impl IntoIterator<Item = Self::Item> {
23-
let single = cx.single_element_list(args, cx.attr_span)?;
23+
let single = cx.expect_single_element_list(args, cx.attr_span)?;
2424
let Some(mi) = single.meta_item() else {
2525
cx.adcx().expected_name_value(single.span(), None);
2626
return None;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ fn parse_directive_items<'p, S: Stage>(
344344
}
345345
(Mode::RustcOnUnimplemented, sym::on) => {
346346
if is_root {
347-
let items = or_malformed!(item.args().list()?);
347+
let items = or_malformed!(item.args().as_list()?);
348348
let mut iter = items.mixed();
349349
let condition: &MetaItemOrLitParser = match iter.next() {
350350
Some(c) => c,
@@ -554,7 +554,7 @@ fn parse_predicate(input: &MetaItemOrLitParser) -> Result<Predicate, InvalidOnCl
554554
sym::any => Ok(Predicate::Any(parse_predicate_sequence(mis)?)),
555555
sym::all => Ok(Predicate::All(parse_predicate_sequence(mis)?)),
556556
sym::not => {
557-
if let Some(single) = mis.single() {
557+
if let Some(single) = mis.as_single() {
558558
Ok(Predicate::Not(Box::new(parse_predicate(single)?)))
559559
} else {
560560
Err(InvalidOnClause::ExpectedOnePredInNot { span: mis.span })

compiler/rustc_attr_parsing/src/attributes/doc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl DocParser {
199199
self.attribute.no_crate_inject = Some(path.span())
200200
}
201201
Some(sym::attr) => {
202-
let Some(list) = args.list() else {
202+
let Some(list) = args.as_list() else {
203203
// FIXME: remove this method once merged and uncomment the line below instead.
204204
// cx.expected_list(cx.attr_span, args);
205205
let span = cx.attr_span;
@@ -587,7 +587,7 @@ impl DocParser {
587587
}),
588588
Some(sym::auto_cfg) => self.parse_auto_cfg(cx, path, args),
589589
Some(sym::test) => {
590-
let Some(list) = args.list() else {
590+
let Some(list) = args.as_list() else {
591591
cx.emit_dyn_lint(
592592
rustc_session::lint::builtin::INVALID_DOC_ATTRIBUTES,
593593
|dcx, level| DocTestTakesList.into_diag(dcx, level),

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
3737
match args {
3838
ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
3939
ArgParser::List(list) => {
40-
let Some(l) = list.single() else {
41-
cx.adcx().expected_single_argument(list.span, list.len());
42-
return None;
43-
};
40+
let l = cx.expect_single(list)?;
4441

4542
match l.meta_item().and_then(|i| i.path().word_sym()) {
4643
Some(sym::always) => {
@@ -78,10 +75,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
7875
let reason = match args {
7976
ArgParser::NoArgs => None,
8077
ArgParser::List(list) => {
81-
let Some(l) = list.single() else {
82-
cx.adcx().expected_single_argument(list.span, list.len());
83-
return None;
84-
};
78+
let l = cx.expect_single(list)?;
8579

8680
let Some(reason) = l.lit().and_then(|i| i.kind.str()) else {
8781
cx.adcx().expected_string_literal(l.span(), l.lit());

compiler/rustc_attr_parsing/src/attributes/instruction_set.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl<S: Stage> SingleAttributeParser<S> for InstructionSetParser {
1919
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
2020
const POSSIBLE_SYMBOLS: &[Symbol] = &[sym::arm_a32, sym::arm_t32];
2121
const POSSIBLE_ARM_SYMBOLS: &[Symbol] = &[sym::a32, sym::t32];
22-
let maybe_meta_item = cx.single_element_list(args, cx.attr_span)?;
22+
let maybe_meta_item = cx.expect_single_element_list(args, cx.attr_span)?;
2323

2424
let Some(meta_item) = maybe_meta_item.meta_item() else {
2525
cx.adcx().expected_specific_argument(maybe_meta_item.span(), POSSIBLE_SYMBOLS);

0 commit comments

Comments
 (0)