Skip to content

Commit 5f36a7f

Browse files
committed
Auto merge of #155145 - jhpratt:rollup-atReM8h, r=jhpratt
Rollup of 3 pull requests Successful merges: - #154827 (distinguish "expected a single argument" and "expected an argument" on attribute parsing) - #155104 (bootstrap: auto-patch libgccjit.so for NixOS) - #155120 (Use a linting node closer the parsing of `#[cfg_attr]`)
2 parents c29effd + b2088a2 commit 5f36a7f

35 files changed

Lines changed: 194 additions & 160 deletions

compiler/rustc_attr_parsing/src/attributes/cfg.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::convert::identity;
22

33
use rustc_ast::token::Delimiter;
44
use rustc_ast::tokenstream::DelimSpan;
5-
use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, ast, token};
5+
use rustc_ast::{AttrItem, Attribute, LitKind, ast, token};
66
use rustc_errors::{Applicability, PResult, msg};
77
use rustc_feature::{
88
AttrSuggestionStyle, AttributeTemplate, Features, GatedCfg, find_gated_cfg, template,
@@ -78,7 +78,7 @@ pub fn parse_cfg<S: Stage>(
7878
}
7979
}
8080

81-
adcx.expected_single_argument(list.span);
81+
adcx.expected_single_argument(list.span, list.len());
8282
return None;
8383
};
8484
parse_cfg_entry(cx, single).ok()
@@ -93,7 +93,7 @@ pub fn parse_cfg_entry<S: Stage>(
9393
ArgParser::List(list) => match meta.path().word_sym() {
9494
Some(sym::not) => {
9595
let Some(single) = list.single() else {
96-
return Err(cx.adcx().expected_single_argument(list.span));
96+
return Err(cx.adcx().expected_single_argument(list.span, list.len()));
9797
};
9898
CfgEntry::Not(Box::new(parse_cfg_entry(cx, single)?), list.span)
9999
}
@@ -324,12 +324,13 @@ pub fn parse_cfg_attr(
324324
cfg_attr: &Attribute,
325325
sess: &Session,
326326
features: Option<&Features>,
327+
lint_node_id: ast::NodeId,
327328
) -> Option<(CfgEntry, Vec<(AttrItem, Span)>)> {
328329
match cfg_attr.get_normal_item().args.unparsed_ref().unwrap() {
329330
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, delim, tokens }) if !tokens.is_empty() => {
330331
check_cfg_attr_bad_delim(&sess.psess, *dspan, *delim);
331332
match parse_in(&sess.psess, tokens.clone(), "`cfg_attr` input", |p| {
332-
parse_cfg_attr_internal(p, sess, features, cfg_attr)
333+
parse_cfg_attr_internal(p, sess, features, lint_node_id, cfg_attr)
333334
}) {
334335
Ok(r) => return Some(r),
335336
Err(e) => {
@@ -390,6 +391,7 @@ fn parse_cfg_attr_internal<'a>(
390391
parser: &mut Parser<'a>,
391392
sess: &'a Session,
392393
features: Option<&Features>,
394+
lint_node_id: ast::NodeId,
393395
attribute: &Attribute,
394396
) -> PResult<'a, (CfgEntry, Vec<(ast::AttrItem, Span)>)> {
395397
// Parse cfg predicate
@@ -410,7 +412,7 @@ fn parse_cfg_attr_internal<'a>(
410412
Some(attribute.get_normal_item().unsafety),
411413
ParsedDescription::Attribute,
412414
pred_span,
413-
CRATE_NODE_ID,
415+
lint_node_id,
414416
Target::Crate,
415417
features,
416418
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },

compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs

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

2525
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
26-
let Some(list) = args.list() else {
27-
let attr_span = cx.attr_span;
28-
cx.adcx().expected_list(attr_span, args);
29-
return None;
30-
};
31-
32-
let Some(single) = list.single() else {
33-
cx.adcx().expected_single_argument(list.span);
34-
return None;
35-
};
26+
let single = cx.single_element_list(args, cx.attr_span)?;
3627

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

8677
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
87-
let Some(args) = args.list() else {
88-
let attr_span = cx.attr_span;
89-
cx.adcx().expected_specific_argument_and_list(attr_span, &[sym::on, sym::off]);
90-
return None;
91-
};
92-
93-
let Some(arg) = args.single() else {
94-
cx.adcx().expected_single_argument(args.span);
95-
return None;
96-
};
78+
let arg = cx.single_element_list(args, cx.attr_span)?;
9779

9880
let mut fail_incorrect_argument =
9981
|span| cx.adcx().expected_specific_argument(span, &[sym::on, sym::off]);
10082

10183
let Some(arg) = arg.meta_item() else {
102-
fail_incorrect_argument(args.span);
84+
fail_incorrect_argument(arg.span());
10385
return None;
10486
};
10587

@@ -389,7 +371,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
389371
ArgParser::NoArgs => UsedBy::Default,
390372
ArgParser::List(list) => {
391373
let Some(l) = list.single() else {
392-
cx.adcx().expected_single_argument(list.span);
374+
cx.adcx().expected_single_argument(list.span, list.len());
393375
return;
394376
};
395377

@@ -750,7 +732,7 @@ impl<S: Stage> SingleAttributeParser<S> for PatchableFunctionEntryParser {
750732
let mut entry = None;
751733

752734
if meta_item_list.len() == 0 {
753-
cx.adcx().expected_list(meta_item_list.span, args);
735+
cx.adcx().expected_at_least_one_argument(meta_item_list.span);
754736
return None;
755737
}
756738

compiler/rustc_attr_parsing/src/attributes/debugger.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +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 Some(l) = args.list() else {
24-
let attr_span = cx.attr_span;
25-
cx.adcx().expected_list(attr_span, args);
26-
return None;
27-
};
28-
let Some(single) = l.single() else {
29-
cx.adcx().expected_single_argument(l.span);
30-
return None;
31-
};
23+
let single = cx.single_element_list(args, cx.attr_span)?;
3224
let Some(mi) = single.meta_item() else {
3325
cx.adcx().expected_name_value(single.span(), None);
3426
return None;

compiler/rustc_attr_parsing/src/attributes/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
3838
ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
3939
ArgParser::List(list) => {
4040
let Some(l) = list.single() else {
41-
cx.adcx().expected_single_argument(list.span);
41+
cx.adcx().expected_single_argument(list.span, list.len());
4242
return None;
4343
};
4444

@@ -80,7 +80,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
8080
ArgParser::NoArgs => None,
8181
ArgParser::List(list) => {
8282
let Some(l) = list.single() else {
83-
cx.adcx().expected_single_argument(list.span);
83+
cx.adcx().expected_single_argument(list.span, list.len());
8484
return None;
8585
};
8686

compiler/rustc_attr_parsing/src/attributes/instruction_set.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,7 @@ impl<S: Stage> SingleAttributeParser<S> for InstructionSetParser {
2020
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
2121
const POSSIBLE_SYMBOLS: &[Symbol] = &[sym::arm_a32, sym::arm_t32];
2222
const POSSIBLE_ARM_SYMBOLS: &[Symbol] = &[sym::a32, sym::t32];
23-
let Some(maybe_meta_item) = args.list().and_then(MetaItemListParser::single) else {
24-
let attr_span = cx.attr_span;
25-
cx.adcx().expected_specific_argument(attr_span, POSSIBLE_SYMBOLS);
26-
return None;
27-
};
23+
let maybe_meta_item = cx.single_element_list(args, cx.attr_span)?;
2824

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

compiler/rustc_attr_parsing/src/attributes/link_attrs.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,7 @@ impl LinkParser {
388388
cx.adcx().duplicate_key(item.span(), sym::cfg);
389389
return true;
390390
}
391-
let Some(link_cfg) = item.args().list() else {
392-
cx.adcx().expected_list(item.span(), item.args());
393-
return true;
394-
};
395-
let Some(link_cfg) = link_cfg.single() else {
396-
cx.adcx().expected_single_argument(item.span());
391+
let Some(link_cfg) = cx.single_element_list(item.args(), item.span()) else {
397392
return true;
398393
};
399394
if !features.link_cfg() {

compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,15 +175,7 @@ impl<S: Stage> SingleAttributeParser<S> for CollapseDebugInfoParser {
175175
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::MacroDef)]);
176176

177177
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
178-
let Some(list) = args.list() else {
179-
let attr_span = cx.attr_span;
180-
cx.adcx().expected_list(attr_span, args);
181-
return None;
182-
};
183-
let Some(single) = list.single() else {
184-
cx.adcx().expected_single_argument(list.span);
185-
return None;
186-
};
178+
let single = cx.single_element_list(args, cx.attr_span)?;
187179
let Some(mi) = single.meta_item() else {
188180
cx.adcx().expected_not_literal(single.span());
189181
return None;

compiler/rustc_attr_parsing/src/attributes/prototype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ fn extract_value<S: Stage>(
8282
}
8383

8484
let Some(val) = arg.name_value() else {
85-
cx.adcx().expected_single_argument(arg.span().unwrap_or(span));
85+
cx.adcx().expected_name_value(span, Some(key));
8686
*failed = true;
8787
return;
8888
};

compiler/rustc_attr_parsing/src/attributes/repr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl RustcAlignParser {
297297
}
298298
ArgParser::List(list) => {
299299
let Some(align) = list.single() else {
300-
cx.adcx().expected_single_argument(list.span);
300+
cx.adcx().expected_single_argument(list.span, list.len());
301301
return;
302302
};
303303

compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLintOptDenyFieldAccessParser {
194194
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Field)]);
195195
const TEMPLATE: AttributeTemplate = template!(Word);
196196
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
197-
let Some(arg) = args.list().and_then(MetaItemListParser::single) else {
198-
let attr_span = cx.attr_span;
199-
cx.adcx().expected_single_argument(attr_span);
200-
return None;
201-
};
197+
let arg = cx.single_element_list(args, cx.attr_span)?;
202198

203199
let MetaItemOrLitParser::Lit(MetaItemLit { kind: LitKind::Str(lint_message, _), .. }) = arg
204200
else {
@@ -374,19 +370,10 @@ impl<S: Stage> SingleAttributeParser<S> for RustcDeprecatedSafe2024Parser {
374370
const TEMPLATE: AttributeTemplate = template!(List: &[r#"audit_that = "...""#]);
375371

376372
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
377-
let Some(args) = args.list() else {
378-
let attr_span = cx.attr_span;
379-
cx.adcx().expected_list(attr_span, args);
380-
return None;
381-
};
382-
383-
let Some(single) = args.single() else {
384-
cx.adcx().expected_single_argument(args.span);
385-
return None;
386-
};
373+
let single = cx.single_element_list(args, cx.attr_span)?;
387374

388375
let Some(arg) = single.meta_item() else {
389-
cx.adcx().expected_name_value(args.span, None);
376+
cx.adcx().expected_name_value(single.span(), None);
390377
return None;
391378
};
392379

@@ -955,7 +942,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcIfThisChangedParser {
955942
ArgParser::List(list) => {
956943
let Some(item) = list.single() else {
957944
let attr_span = cx.attr_span;
958-
cx.adcx().expected_single_argument(attr_span);
945+
cx.adcx().expected_single_argument(attr_span, list.len());
959946
return None;
960947
};
961948
let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
@@ -1015,11 +1002,7 @@ impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
10151002
if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
10161003
cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
10171004
}
1018-
let Some(item) = args.list().and_then(|l| l.single()) else {
1019-
let inner_span = cx.inner_span;
1020-
cx.adcx().expected_single_argument(inner_span);
1021-
return None;
1022-
};
1005+
let item = cx.single_element_list(args, cx.attr_span)?;
10231006
let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
10241007
cx.adcx().expected_identifier(item.span());
10251008
return None;

0 commit comments

Comments
 (0)