Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions compiler/rustc_attr_parsing/src/attributes/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::identity;

use rustc_ast::token::Delimiter;
use rustc_ast::tokenstream::DelimSpan;
use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, ast, token};
use rustc_ast::{AttrItem, Attribute, LitKind, ast, token};
use rustc_errors::{Applicability, PResult, msg};
use rustc_feature::{
AttrSuggestionStyle, AttributeTemplate, Features, GatedCfg, find_gated_cfg, template,
Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn parse_cfg<S: Stage>(
}
}

adcx.expected_single_argument(list.span);
adcx.expected_single_argument(list.span, list.len());
return None;
};
parse_cfg_entry(cx, single).ok()
Expand All @@ -93,7 +93,7 @@ pub fn parse_cfg_entry<S: Stage>(
ArgParser::List(list) => match meta.path().word_sym() {
Some(sym::not) => {
let Some(single) = list.single() else {
return Err(cx.adcx().expected_single_argument(list.span));
return Err(cx.adcx().expected_single_argument(list.span, list.len()));
};
CfgEntry::Not(Box::new(parse_cfg_entry(cx, single)?), list.span)
}
Expand Down Expand Up @@ -324,12 +324,13 @@ pub fn parse_cfg_attr(
cfg_attr: &Attribute,
sess: &Session,
features: Option<&Features>,
lint_node_id: ast::NodeId,
) -> Option<(CfgEntry, Vec<(AttrItem, Span)>)> {
match cfg_attr.get_normal_item().args.unparsed_ref().unwrap() {
ast::AttrArgs::Delimited(ast::DelimArgs { dspan, delim, tokens }) if !tokens.is_empty() => {
check_cfg_attr_bad_delim(&sess.psess, *dspan, *delim);
match parse_in(&sess.psess, tokens.clone(), "`cfg_attr` input", |p| {
parse_cfg_attr_internal(p, sess, features, cfg_attr)
parse_cfg_attr_internal(p, sess, features, lint_node_id, cfg_attr)
}) {
Ok(r) => return Some(r),
Err(e) => {
Expand Down Expand Up @@ -390,6 +391,7 @@ fn parse_cfg_attr_internal<'a>(
parser: &mut Parser<'a>,
sess: &'a Session,
features: Option<&Features>,
lint_node_id: ast::NodeId,
attribute: &Attribute,
) -> PResult<'a, (CfgEntry, Vec<(ast::AttrItem, Span)>)> {
// Parse cfg predicate
Expand All @@ -410,7 +412,7 @@ fn parse_cfg_attr_internal<'a>(
Some(attribute.get_normal_item().unsafety),
ParsedDescription::Attribute,
pred_span,
CRATE_NODE_ID,
lint_node_id,
Target::Crate,
features,
ShouldEmit::ErrorsAndLints { recovery: Recovery::Allowed },
Expand Down
28 changes: 5 additions & 23 deletions compiler/rustc_attr_parsing/src/attributes/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,7 @@ impl<S: Stage> SingleAttributeParser<S> for OptimizeParser {
const TEMPLATE: AttributeTemplate = template!(List: &["size", "speed", "none"]);

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
let Some(list) = args.list() else {
let attr_span = cx.attr_span;
cx.adcx().expected_list(attr_span, args);
return None;
};

let Some(single) = list.single() else {
cx.adcx().expected_single_argument(list.span);
return None;
};
let single = cx.single_element_list(args, cx.attr_span)?;

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

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
let Some(args) = args.list() else {
let attr_span = cx.attr_span;
cx.adcx().expected_specific_argument_and_list(attr_span, &[sym::on, sym::off]);
return None;
};

let Some(arg) = args.single() else {
cx.adcx().expected_single_argument(args.span);
return None;
};
let arg = cx.single_element_list(args, cx.attr_span)?;

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

let Some(arg) = arg.meta_item() else {
fail_incorrect_argument(args.span);
fail_incorrect_argument(arg.span());
return None;
};

Expand Down Expand Up @@ -389,7 +371,7 @@ impl<S: Stage> AttributeParser<S> for UsedParser {
ArgParser::NoArgs => UsedBy::Default,
ArgParser::List(list) => {
let Some(l) = list.single() else {
cx.adcx().expected_single_argument(list.span);
cx.adcx().expected_single_argument(list.span, list.len());
return;
};

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

if meta_item_list.len() == 0 {
cx.adcx().expected_list(meta_item_list.span, args);
cx.adcx().expected_at_least_one_argument(meta_item_list.span);
return None;
}

Expand Down
10 changes: 1 addition & 9 deletions compiler/rustc_attr_parsing/src/attributes/debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,7 @@ impl<S: Stage> CombineAttributeParser<S> for DebuggerViualizerParser {
cx: &mut AcceptContext<'_, '_, S>,
args: &ArgParser,
) -> impl IntoIterator<Item = Self::Item> {
let Some(l) = args.list() else {
let attr_span = cx.attr_span;
cx.adcx().expected_list(attr_span, args);
return None;
};
let Some(single) = l.single() else {
cx.adcx().expected_single_argument(l.span);
return None;
};
let single = cx.single_element_list(args, cx.attr_span)?;
let Some(mi) = single.meta_item() else {
cx.adcx().expected_name_value(single.span(), None);
return None;
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_attr_parsing/src/attributes/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl<S: Stage> SingleAttributeParser<S> for InlineParser {
ArgParser::NoArgs => Some(AttributeKind::Inline(InlineAttr::Hint, cx.attr_span)),
ArgParser::List(list) => {
let Some(l) = list.single() else {
cx.adcx().expected_single_argument(list.span);
cx.adcx().expected_single_argument(list.span, list.len());
return None;
};

Expand Down Expand Up @@ -80,7 +80,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcForceInlineParser {
ArgParser::NoArgs => None,
ArgParser::List(list) => {
let Some(l) = list.single() else {
cx.adcx().expected_single_argument(list.span);
cx.adcx().expected_single_argument(list.span, list.len());
return None;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ impl<S: Stage> SingleAttributeParser<S> for InstructionSetParser {
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
const POSSIBLE_SYMBOLS: &[Symbol] = &[sym::arm_a32, sym::arm_t32];
const POSSIBLE_ARM_SYMBOLS: &[Symbol] = &[sym::a32, sym::t32];
let Some(maybe_meta_item) = args.list().and_then(MetaItemListParser::single) else {
let attr_span = cx.attr_span;
cx.adcx().expected_specific_argument(attr_span, POSSIBLE_SYMBOLS);
return None;
};
let maybe_meta_item = cx.single_element_list(args, cx.attr_span)?;

let Some(meta_item) = maybe_meta_item.meta_item() else {
cx.adcx().expected_specific_argument(maybe_meta_item.span(), POSSIBLE_SYMBOLS);
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_attr_parsing/src/attributes/link_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,7 @@ impl LinkParser {
cx.adcx().duplicate_key(item.span(), sym::cfg);
return true;
}
let Some(link_cfg) = item.args().list() else {
cx.adcx().expected_list(item.span(), item.args());
return true;
};
let Some(link_cfg) = link_cfg.single() else {
cx.adcx().expected_single_argument(item.span());
let Some(link_cfg) = cx.single_element_list(item.args(), item.span()) else {
return true;
};
if !features.link_cfg() {
Expand Down
10 changes: 1 addition & 9 deletions compiler/rustc_attr_parsing/src/attributes/macro_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,7 @@ impl<S: Stage> SingleAttributeParser<S> for CollapseDebugInfoParser {
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::MacroDef)]);

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
let Some(list) = args.list() else {
let attr_span = cx.attr_span;
cx.adcx().expected_list(attr_span, args);
return None;
};
let Some(single) = list.single() else {
cx.adcx().expected_single_argument(list.span);
return None;
};
let single = cx.single_element_list(args, cx.attr_span)?;
let Some(mi) = single.meta_item() else {
cx.adcx().expected_not_literal(single.span());
return None;
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr_parsing/src/attributes/prototype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn extract_value<S: Stage>(
}

let Some(val) = arg.name_value() else {
cx.adcx().expected_single_argument(arg.span().unwrap_or(span));
cx.adcx().expected_name_value(span, Some(key));
*failed = true;
return;
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_attr_parsing/src/attributes/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl RustcAlignParser {
}
ArgParser::List(list) => {
let Some(align) = list.single() else {
cx.adcx().expected_single_argument(list.span);
cx.adcx().expected_single_argument(list.span, list.len());
return;
};

Expand Down
27 changes: 5 additions & 22 deletions compiler/rustc_attr_parsing/src/attributes/rustc_internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcLintOptDenyFieldAccessParser {
const ALLOWED_TARGETS: AllowedTargets = AllowedTargets::AllowList(&[Allow(Target::Field)]);
const TEMPLATE: AttributeTemplate = template!(Word);
fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
let Some(arg) = args.list().and_then(MetaItemListParser::single) else {
let attr_span = cx.attr_span;
cx.adcx().expected_single_argument(attr_span);
return None;
};
let arg = cx.single_element_list(args, cx.attr_span)?;

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

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
let Some(args) = args.list() else {
let attr_span = cx.attr_span;
cx.adcx().expected_list(attr_span, args);
return None;
};

let Some(single) = args.single() else {
cx.adcx().expected_single_argument(args.span);
return None;
};
let single = cx.single_element_list(args, cx.attr_span)?;

let Some(arg) = single.meta_item() else {
cx.adcx().expected_name_value(args.span, None);
cx.adcx().expected_name_value(single.span(), None);
return None;
};

Expand Down Expand Up @@ -955,7 +942,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcIfThisChangedParser {
ArgParser::List(list) => {
let Some(item) = list.single() else {
let attr_span = cx.attr_span;
cx.adcx().expected_single_argument(attr_span);
cx.adcx().expected_single_argument(attr_span, list.len());
return None;
};
let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
Expand Down Expand Up @@ -1015,11 +1002,7 @@ impl<S: Stage> CombineAttributeParser<S> for RustcThenThisWouldNeedParser {
if !cx.cx.sess.opts.unstable_opts.query_dep_graph {
cx.emit_err(AttributeRequiresOpt { span: cx.attr_span, opt: "-Z query-dep-graph" });
}
let Some(item) = args.list().and_then(|l| l.single()) else {
let inner_span = cx.inner_span;
cx.adcx().expected_single_argument(inner_span);
return None;
};
let item = cx.single_element_list(args, cx.attr_span)?;
let Some(ident) = item.meta_item().and_then(|item| item.ident()) else {
cx.adcx().expected_identifier(item.span());
return None;
Expand Down
15 changes: 3 additions & 12 deletions compiler/rustc_attr_parsing/src/attributes/test_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<S: Stage> SingleAttributeParser<S> for ShouldPanicParser {
}
ArgParser::List(list) => {
let Some(single) = list.single() else {
cx.adcx().expected_single_argument(list.span);
cx.adcx().expected_single_argument(list.span, list.len());
return None;
};
let Some(single) = single.meta_item() else {
Expand Down Expand Up @@ -150,7 +150,7 @@ impl<S: Stage> SingleAttributeParser<S> for RustcAbiParser {

let Some(arg) = args.single() else {
let attr_span = cx.attr_span;
cx.adcx().expected_single_argument(attr_span);
cx.adcx().expected_single_argument(attr_span, args.len());
return None;
};

Expand Down Expand Up @@ -208,16 +208,7 @@ impl<S: Stage> SingleAttributeParser<S> for TestRunnerParser {
const TEMPLATE: AttributeTemplate = template!(List: &["path"]);

fn convert(cx: &mut AcceptContext<'_, '_, S>, args: &ArgParser) -> Option<AttributeKind> {
let Some(list) = args.list() else {
let attr_span = cx.attr_span;
cx.adcx().expected_list(attr_span, args);
return None;
};

let Some(single) = list.single() else {
cx.adcx().expected_single_argument(list.span);
return None;
};
let single = cx.single_element_list(args, cx.attr_span)?;

let Some(meta) = single.meta_item() else {
cx.adcx().expected_not_literal(single.span());
Expand Down
10 changes: 1 addition & 9 deletions compiler/rustc_attr_parsing/src/attributes/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,7 @@ pub(crate) fn parse_single_integer<S: Stage>(
cx: &mut AcceptContext<'_, '_, S>,
args: &ArgParser,
) -> Option<u128> {
let Some(list) = args.list() else {
let attr_span = cx.attr_span;
cx.adcx().expected_list(attr_span, args);
return None;
};
let Some(single) = list.single() else {
cx.adcx().expected_single_argument(list.span);
return None;
};
let single = cx.single_element_list(args, cx.attr_span)?;
let Some(lit) = single.lit() else {
cx.adcx().expected_integer_literal(single.span());
return None;
Expand Down
Loading
Loading