Skip to content

Commit 1e9bad4

Browse files
committed
Streamline attr parsing APIs.
- Remove `MetaItemParser::{path,deconstruct}` and just get the two parsers one at a time. - Rename `MetaItemParser::path_without_args` as `MetaItemParser::path`, and avoid the clone. - Remove `MetaItemParser::{word,word_without_args,path_is}`, which are unused. - Remove `MetaItemListParser::all_{word,path}_list`, which are unused.
1 parent 399aa5c commit 1e9bad4

File tree

6 files changed

+19
-65
lines changed

6 files changed

+19
-65
lines changed

compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn parse_unstable<'a>(
5353

5454
for param in list.mixed() {
5555
let param_span = param.span();
56-
if let Some(ident) = param.meta_item().and_then(|i| i.path_without_args().word()) {
56+
if let Some(ident) = param.meta_item().and_then(|i| i.path().word()) {
5757
res.push(ident.name);
5858
} else {
5959
cx.emit_err(session_diagnostics::ExpectsFeatures {

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl SingleAttributeParser for DeprecationParser {
7979
return None;
8080
};
8181

82-
let ident_name = param.path_without_args().word_sym();
82+
let ident_name = param.path().word_sym();
8383

8484
match ident_name {
8585
Some(name @ sym::since) => {
@@ -102,7 +102,7 @@ impl SingleAttributeParser for DeprecationParser {
102102
_ => {
103103
cx.emit_err(session_diagnostics::UnknownMetaItem {
104104
span: param_span,
105-
item: param.path_without_args().to_string(),
105+
item: param.path().to_string(),
106106
expected: if features.deprecated_suggestion() {
107107
&["since", "note", "suggestion"]
108108
} else {

compiler/rustc_attr_parsing/src/attributes/repr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ fn parse_repr(cx: &AcceptContext<'_>, param: &MetaItemParser<'_>) -> Option<Repr
9696

9797
// FIXME(jdonszelmann): invert the parsing here to match on the word first and then the
9898
// structure.
99-
let ident = param.path_without_args().word();
99+
let ident = param.path().word();
100100
let ident_span = ident.map_or(rustc_span::DUMMY_SP, |ident| ident.span);
101101
let name = ident.map(|ident| ident.name);
102102
let args = param.args();

compiler/rustc_attr_parsing/src/attributes/stability.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn insert_value_into_option_or_error(
204204
if item.is_some() {
205205
cx.emit_err(session_diagnostics::MultipleItem {
206206
span: param.span(),
207-
item: param.path_without_args().to_string(),
207+
item: param.path().to_string(),
208208
});
209209
None
210210
} else if let Some(v) = param.args().name_value()
@@ -242,13 +242,13 @@ pub(crate) fn parse_stability(
242242
return None;
243243
};
244244

245-
match param.path_without_args().word_sym() {
245+
match param.path().word_sym() {
246246
Some(sym::feature) => insert_value_into_option_or_error(cx, &param, &mut feature)?,
247247
Some(sym::since) => insert_value_into_option_or_error(cx, &param, &mut since)?,
248248
_ => {
249249
cx.emit_err(session_diagnostics::UnknownMetaItem {
250250
span: param_span,
251-
item: param.path_without_args().to_string(),
251+
item: param.path().to_string(),
252252
expected: &["feature", "since"],
253253
});
254254
return None;
@@ -310,7 +310,7 @@ pub(crate) fn parse_unstability(
310310
return None;
311311
};
312312

313-
match param.path_without_args().word_sym() {
313+
match param.path().word_sym() {
314314
Some(sym::feature) => insert_value_into_option_or_error(cx, &param, &mut feature)?,
315315
Some(sym::reason) => insert_value_into_option_or_error(cx, &param, &mut reason)?,
316316
Some(sym::issue) => {
@@ -349,7 +349,7 @@ pub(crate) fn parse_unstability(
349349
_ => {
350350
cx.emit_err(session_diagnostics::UnknownMetaItem {
351351
span: param.span(),
352-
item: param.path_without_args().to_string(),
352+
item: param.path().to_string(),
353353
expected: &["feature", "reason", "issue", "soft", "implied_by"],
354354
});
355355
return None;

compiler/rustc_attr_parsing/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl<'sess> AttributeParser<'sess> {
260260
// }
261261
ast::AttrKind::Normal(n) => {
262262
let parser = MetaItemParser::from_attr(n, self.dcx());
263-
let path = parser.path_without_args();
263+
let path = parser.path();
264264
let args = parser.args();
265265
let parts = path.segments().map(|i| i.name).collect::<Vec<_>>();
266266

compiler/rustc_attr_parsing/src/parser.rs

+9-55
Original file line numberDiff line numberDiff line change
@@ -253,60 +253,28 @@ impl<'a> MetaItemParser<'a> {
253253
}
254254
}
255255

256-
/// Gets just the path, without the args.
257-
pub fn path_without_args(&self) -> PathParser<'a> {
258-
self.path.clone()
259-
}
260-
261-
/// Gets just the args parser, without caring about the path.
262-
pub fn args(&self) -> &ArgParser<'a> {
263-
&self.args
264-
}
265-
266-
pub fn deconstruct(&self) -> (PathParser<'a>, &ArgParser<'a>) {
267-
(self.path_without_args(), self.args())
268-
}
269-
270-
/// Asserts that this MetaItem starts with a path. Some examples:
256+
/// Gets a `PathParser` for a path. Some examples:
271257
///
272258
/// - `#[rustfmt::skip]`: `rustfmt::skip` is a path
273259
/// - `#[allow(clippy::complexity)]`: `clippy::complexity` is a path
274260
/// - `#[inline]`: `inline` is a single segment path
275-
pub fn path(&self) -> (PathParser<'a>, &ArgParser<'a>) {
276-
self.deconstruct()
261+
pub fn path(&self) -> &PathParser<'a> {
262+
&self.path
277263
}
278264

279-
/// Asserts that this MetaItem starts with a word, or single segment path.
280-
/// Doesn't return the args parser.
281-
///
282-
/// For examples. see [`Self::word`]
283-
pub fn word_without_args(&self) -> Option<Ident> {
284-
Some(self.word()?.0)
265+
/// Gets just the args parser, without caring about the path.
266+
pub fn args(&self) -> &ArgParser<'a> {
267+
&self.args
285268
}
286269

287-
/// Asserts that this MetaItem starts with a word, or single segment path.
270+
/// Asserts that this MetaItem starts with some specific word.
288271
///
289272
/// Some examples:
290273
/// - `#[inline]`: `inline` is a word
291274
/// - `#[rustfmt::skip]`: `rustfmt::skip` is a path,
292275
/// and not a word and should instead be parsed using [`path`](Self::path)
293-
pub fn word(&self) -> Option<(Ident, &ArgParser<'a>)> {
294-
let (path, args) = self.deconstruct();
295-
Some((path.word()?, args))
296-
}
297-
298-
/// Asserts that this MetaItem starts with some specific word.
299-
///
300-
/// See [`word`](Self::word) for examples of what a word is.
301276
pub fn word_is(&self, sym: Symbol) -> Option<&ArgParser<'a>> {
302-
self.path_without_args().word_is(sym).then(|| self.args())
303-
}
304-
305-
/// Asserts that this MetaItem starts with some specific path.
306-
///
307-
/// See [`word`](Self::path) for examples of what a word is.
308-
pub fn path_is(&self, segments: &[Symbol]) -> Option<&ArgParser<'a>> {
309-
self.path_without_args().segments_is(segments).then(|| self.args())
277+
self.path().word_is(sym).then(|| self.args())
310278
}
311279
}
312280

@@ -549,7 +517,7 @@ impl<'a> MetaItemListParser<'a> {
549517
}
550518

551519
/// Lets you pick and choose as what you want to parse each element in the list
552-
pub fn mixed<'s>(&'s self) -> impl Iterator<Item = &'s MetaItemOrLitParser<'a>> + 's {
520+
pub fn mixed(&self) -> impl Iterator<Item = &MetaItemOrLitParser<'a>> + '_ {
553521
self.sub_parsers.iter()
554522
}
555523

@@ -561,20 +529,6 @@ impl<'a> MetaItemListParser<'a> {
561529
self.len() == 0
562530
}
563531

564-
/// Asserts that every item in the list is another list starting with a word.
565-
///
566-
/// See [`MetaItemParser::word`] for examples of words.
567-
pub fn all_word_list<'s>(&'s self) -> Option<Vec<(Ident, &'s ArgParser<'a>)>> {
568-
self.mixed().map(|i| i.meta_item()?.word()).collect()
569-
}
570-
571-
/// Asserts that every item in the list is another list starting with a full path.
572-
///
573-
/// See [`MetaItemParser::path`] for examples of paths.
574-
pub fn all_path_list<'s>(&'s self) -> Option<Vec<(PathParser<'a>, &'s ArgParser<'a>)>> {
575-
self.mixed().map(|i| Some(i.meta_item()?.path())).collect()
576-
}
577-
578532
/// Returns Some if the list contains only a single element.
579533
///
580534
/// Inside the Some is the parser to parse this single element.

0 commit comments

Comments
 (0)