Skip to content

Commit ece9f99

Browse files
authored
Rollup merge of rust-lang#140095 - nnethercote:rm-word_or_empty, r=jdonszelmann
Eliminate `word_and_empty` methods. To remove the last remaining `Ident::empty` uses. r? ``@jdonszelmann``
2 parents fe348cd + 603766c commit ece9f99

File tree

6 files changed

+88
-85
lines changed

6 files changed

+88
-85
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.word_without_args()) {
56+
if let Some(ident) = param.meta_item().and_then(|i| i.path_without_args().word()) {
5757
res.push(ident.name);
5858
} else {
5959
cx.emit_err(session_diagnostics::ExpectsFeatures {

compiler/rustc_attr_parsing/src/attributes/deprecation.rs

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_attr_data_structures::{AttributeKind, DeprecatedSince, Deprecation};
2-
use rustc_span::symbol::Ident;
32
use rustc_span::{Span, Symbol, sym};
43

54
use super::SingleAttributeParser;
@@ -13,16 +12,13 @@ pub(crate) struct DeprecationParser;
1312

1413
fn get(
1514
cx: &AcceptContext<'_>,
16-
ident: Ident,
15+
name: Symbol,
1716
param_span: Span,
1817
arg: &ArgParser<'_>,
1918
item: &Option<Symbol>,
2019
) -> Option<Symbol> {
2120
if item.is_some() {
22-
cx.emit_err(session_diagnostics::MultipleItem {
23-
span: param_span,
24-
item: ident.to_string(),
25-
});
21+
cx.emit_err(session_diagnostics::MultipleItem { span: param_span, item: name.to_string() });
2622
return None;
2723
}
2824
if let Some(v) = arg.name_value() {
@@ -83,16 +79,16 @@ impl SingleAttributeParser for DeprecationParser {
8379
return None;
8480
};
8581

86-
let (ident, arg) = param.word_or_empty();
82+
let ident_name = param.path_without_args().word_sym();
8783

88-
match ident.name {
89-
sym::since => {
90-
since = Some(get(cx, ident, param_span, arg, &since)?);
84+
match ident_name {
85+
Some(name @ sym::since) => {
86+
since = Some(get(cx, name, param_span, param.args(), &since)?);
9187
}
92-
sym::note => {
93-
note = Some(get(cx, ident, param_span, arg, &note)?);
88+
Some(name @ sym::note) => {
89+
note = Some(get(cx, name, param_span, param.args(), &note)?);
9490
}
95-
sym::suggestion => {
91+
Some(name @ sym::suggestion) => {
9692
if !features.deprecated_suggestion() {
9793
cx.emit_err(session_diagnostics::DeprecatedItemSuggestion {
9894
span: param_span,
@@ -101,12 +97,12 @@ impl SingleAttributeParser for DeprecationParser {
10197
});
10298
}
10399

104-
suggestion = Some(get(cx, ident, param_span, arg, &suggestion)?);
100+
suggestion = Some(get(cx, name, param_span, param.args(), &suggestion)?);
105101
}
106102
_ => {
107103
cx.emit_err(session_diagnostics::UnknownMetaItem {
108104
span: param_span,
109-
item: ident.to_string(),
105+
item: param.path_without_args().to_string(),
110106
expected: if features.deprecated_suggestion() {
111107
&["since", "note", "suggestion"]
112108
} else {

compiler/rustc_attr_parsing/src/attributes/repr.rs

+43-26
Original file line numberDiff line numberDiff line change
@@ -96,58 +96,75 @@ 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, args) = param.word_or_empty();
99+
let (name, ident_span) = if let Some(ident) = param.path_without_args().word() {
100+
(Some(ident.name), ident.span)
101+
} else {
102+
(None, rustc_span::DUMMY_SP)
103+
};
104+
105+
let args = param.args();
100106

101-
match (ident.name, args) {
102-
(sym::align, ArgParser::NoArgs) => {
103-
cx.emit_err(session_diagnostics::InvalidReprAlignNeedArg { span: ident.span });
107+
match (name, args) {
108+
(Some(sym::align), ArgParser::NoArgs) => {
109+
cx.emit_err(session_diagnostics::InvalidReprAlignNeedArg { span: ident_span });
104110
None
105111
}
106-
(sym::align, ArgParser::List(l)) => parse_repr_align(cx, l, param.span(), AlignKind::Align),
112+
(Some(sym::align), ArgParser::List(l)) => {
113+
parse_repr_align(cx, l, param.span(), AlignKind::Align)
114+
}
107115

108-
(sym::packed, ArgParser::NoArgs) => Some(ReprPacked(Align::ONE)),
109-
(sym::packed, ArgParser::List(l)) => {
116+
(Some(sym::packed), ArgParser::NoArgs) => Some(ReprPacked(Align::ONE)),
117+
(Some(sym::packed), ArgParser::List(l)) => {
110118
parse_repr_align(cx, l, param.span(), AlignKind::Packed)
111119
}
112120

113-
(sym::align | sym::packed, ArgParser::NameValue(l)) => {
121+
(Some(name @ sym::align | name @ sym::packed), ArgParser::NameValue(l)) => {
114122
cx.emit_err(session_diagnostics::IncorrectReprFormatGeneric {
115123
span: param.span(),
116124
// FIXME(jdonszelmann) can just be a string in the diag type
117-
repr_arg: &ident.to_string(),
125+
repr_arg: name,
118126
cause: IncorrectReprFormatGenericCause::from_lit_kind(
119127
param.span(),
120128
&l.value_as_lit().kind,
121-
ident.name.as_str(),
129+
name,
122130
),
123131
});
124132
None
125133
}
126134

127-
(sym::Rust, ArgParser::NoArgs) => Some(ReprRust),
128-
(sym::C, ArgParser::NoArgs) => Some(ReprC),
129-
(sym::simd, ArgParser::NoArgs) => Some(ReprSimd),
130-
(sym::transparent, ArgParser::NoArgs) => Some(ReprTransparent),
131-
(i @ int_pat!(), ArgParser::NoArgs) => {
135+
(Some(sym::Rust), ArgParser::NoArgs) => Some(ReprRust),
136+
(Some(sym::C), ArgParser::NoArgs) => Some(ReprC),
137+
(Some(sym::simd), ArgParser::NoArgs) => Some(ReprSimd),
138+
(Some(sym::transparent), ArgParser::NoArgs) => Some(ReprTransparent),
139+
(Some(name @ int_pat!()), ArgParser::NoArgs) => {
132140
// int_pat!() should make sure it always parses
133-
Some(ReprInt(int_type_of_word(i).unwrap()))
141+
Some(ReprInt(int_type_of_word(name).unwrap()))
134142
}
135143

136144
(
137-
sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!(),
145+
Some(
146+
name @ sym::Rust
147+
| name @ sym::C
148+
| name @ sym::simd
149+
| name @ sym::transparent
150+
| name @ int_pat!(),
151+
),
138152
ArgParser::NameValue(_),
139153
) => {
140-
cx.emit_err(session_diagnostics::InvalidReprHintNoValue {
141-
span: param.span(),
142-
name: ident.to_string(),
143-
});
154+
cx.emit_err(session_diagnostics::InvalidReprHintNoValue { span: param.span(), name });
144155
None
145156
}
146-
(sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!(), ArgParser::List(_)) => {
147-
cx.emit_err(session_diagnostics::InvalidReprHintNoParen {
148-
span: param.span(),
149-
name: ident.to_string(),
150-
});
157+
(
158+
Some(
159+
name @ sym::Rust
160+
| name @ sym::C
161+
| name @ sym::simd
162+
| name @ sym::transparent
163+
| name @ int_pat!(),
164+
),
165+
ArgParser::List(_),
166+
) => {
167+
cx.emit_err(session_diagnostics::InvalidReprHintNoParen { span: param.span(), name });
151168
None
152169
}
153170

compiler/rustc_attr_parsing/src/attributes/stability.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ pub(crate) fn parse_stability(
242242
return None;
243243
};
244244

245-
match param.word_or_empty_without_args().name {
246-
sym::feature => insert_value_into_option_or_error(cx, &param, &mut feature)?,
247-
sym::since => insert_value_into_option_or_error(cx, &param, &mut since)?,
245+
match param.path_without_args().word_sym() {
246+
Some(sym::feature) => insert_value_into_option_or_error(cx, &param, &mut feature)?,
247+
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,
@@ -310,11 +310,10 @@ pub(crate) fn parse_unstability(
310310
return None;
311311
};
312312

313-
let (word, args) = param.word_or_empty();
314-
match word.name {
315-
sym::feature => insert_value_into_option_or_error(cx, &param, &mut feature)?,
316-
sym::reason => insert_value_into_option_or_error(cx, &param, &mut reason)?,
317-
sym::issue => {
313+
match param.path_without_args().word_sym() {
314+
Some(sym::feature) => insert_value_into_option_or_error(cx, &param, &mut feature)?,
315+
Some(sym::reason) => insert_value_into_option_or_error(cx, &param, &mut reason)?,
316+
Some(sym::issue) => {
318317
insert_value_into_option_or_error(cx, &param, &mut issue)?;
319318

320319
// These unwraps are safe because `insert_value_into_option_or_error` ensures the meta item
@@ -328,7 +327,7 @@ pub(crate) fn parse_unstability(
328327
session_diagnostics::InvalidIssueString {
329328
span: param.span(),
330329
cause: session_diagnostics::InvalidIssueStringCause::from_int_error_kind(
331-
args.name_value().unwrap().value_span,
330+
param.args().name_value().unwrap().value_span,
332331
err.kind(),
333332
),
334333
},
@@ -338,13 +337,15 @@ pub(crate) fn parse_unstability(
338337
},
339338
};
340339
}
341-
sym::soft => {
342-
if !args.no_args() {
340+
Some(sym::soft) => {
341+
if !param.args().no_args() {
343342
cx.emit_err(session_diagnostics::SoftNoArgs { span: param.span() });
344343
}
345344
is_soft = true;
346345
}
347-
sym::implied_by => insert_value_into_option_or_error(cx, &param, &mut implied_by)?,
346+
Some(sym::implied_by) => {
347+
insert_value_into_option_or_error(cx, &param, &mut implied_by)?
348+
}
348349
_ => {
349350
cx.emit_err(session_diagnostics::UnknownMetaItem {
350351
span: param.span(),

compiler/rustc_attr_parsing/src/parser.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ impl<'a> PathParser<'a> {
7878
(self.len() == 1).then(|| **self.segments().next().as_ref().unwrap())
7979
}
8080

81-
pub fn word_or_empty(&self) -> Ident {
82-
self.word().unwrap_or_else(Ident::empty)
81+
pub fn word_sym(&self) -> Option<Symbol> {
82+
self.word().map(|ident| ident.name)
8383
}
8484

8585
/// Asserts that this MetaItem is some specific word.
@@ -284,11 +284,6 @@ impl<'a> MetaItemParser<'a> {
284284
Some(self.word()?.0)
285285
}
286286

287-
/// Like [`word`](Self::word), but returns an empty symbol instead of None
288-
pub fn word_or_empty_without_args(&self) -> Ident {
289-
self.word_or_empty().0
290-
}
291-
292287
/// Asserts that this MetaItem starts with a word, or single segment path.
293288
///
294289
/// Some examples:
@@ -300,12 +295,6 @@ impl<'a> MetaItemParser<'a> {
300295
Some((path.word()?, args))
301296
}
302297

303-
/// Like [`word`](Self::word), but returns an empty symbol instead of None
304-
pub fn word_or_empty(&self) -> (Ident, &ArgParser<'a>) {
305-
let (path, args) = self.deconstruct();
306-
(path.word().unwrap_or(Ident::empty()), args)
307-
}
308-
309298
/// Asserts that this MetaItem starts with some specific word.
310299
///
311300
/// See [`word`](Self::word) for examples of what a word is.

compiler/rustc_attr_parsing/src/session_diagnostics.rs

+18-18
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub(crate) struct InvalidReprHintNoParen {
204204
#[primary_span]
205205
pub span: Span,
206206

207-
pub name: String,
207+
pub name: Symbol,
208208
}
209209

210210
#[derive(Diagnostic)]
@@ -213,7 +213,7 @@ pub(crate) struct InvalidReprHintNoValue {
213213
#[primary_span]
214214
pub span: Span,
215215

216-
pub name: String,
216+
pub name: Symbol,
217217
}
218218

219219
/// Error code: E0565
@@ -295,58 +295,58 @@ pub(crate) struct IncorrectReprFormatExpectInteger {
295295

296296
#[derive(Diagnostic)]
297297
#[diag(attr_parsing_incorrect_repr_format_generic, code = E0693)]
298-
pub(crate) struct IncorrectReprFormatGeneric<'a> {
298+
pub(crate) struct IncorrectReprFormatGeneric {
299299
#[primary_span]
300300
pub span: Span,
301301

302-
pub repr_arg: &'a str,
302+
pub repr_arg: Symbol,
303303

304304
#[subdiagnostic]
305-
pub cause: Option<IncorrectReprFormatGenericCause<'a>>,
305+
pub cause: Option<IncorrectReprFormatGenericCause>,
306306
}
307307

308308
#[derive(Subdiagnostic)]
309-
pub(crate) enum IncorrectReprFormatGenericCause<'a> {
309+
pub(crate) enum IncorrectReprFormatGenericCause {
310310
#[suggestion(
311311
attr_parsing_suggestion,
312-
code = "{name}({int})",
312+
code = "{name}({value})",
313313
applicability = "machine-applicable"
314314
)]
315315
Int {
316316
#[primary_span]
317317
span: Span,
318318

319319
#[skip_arg]
320-
name: &'a str,
320+
name: Symbol,
321321

322322
#[skip_arg]
323-
int: u128,
323+
value: u128,
324324
},
325325

326326
#[suggestion(
327327
attr_parsing_suggestion,
328-
code = "{name}({symbol})",
328+
code = "{name}({value})",
329329
applicability = "machine-applicable"
330330
)]
331331
Symbol {
332332
#[primary_span]
333333
span: Span,
334334

335335
#[skip_arg]
336-
name: &'a str,
336+
name: Symbol,
337337

338338
#[skip_arg]
339-
symbol: Symbol,
339+
value: Symbol,
340340
},
341341
}
342342

343-
impl<'a> IncorrectReprFormatGenericCause<'a> {
344-
pub(crate) fn from_lit_kind(span: Span, kind: &ast::LitKind, name: &'a str) -> Option<Self> {
345-
match kind {
346-
ast::LitKind::Int(int, ast::LitIntType::Unsuffixed) => {
347-
Some(Self::Int { span, name, int: int.get() })
343+
impl IncorrectReprFormatGenericCause {
344+
pub(crate) fn from_lit_kind(span: Span, kind: &ast::LitKind, name: Symbol) -> Option<Self> {
345+
match *kind {
346+
ast::LitKind::Int(value, ast::LitIntType::Unsuffixed) => {
347+
Some(Self::Int { span, name, value: value.get() })
348348
}
349-
ast::LitKind::Str(symbol, _) => Some(Self::Symbol { span, name, symbol: *symbol }),
349+
ast::LitKind::Str(value, _) => Some(Self::Symbol { span, name, value }),
350350
_ => None,
351351
}
352352
}

0 commit comments

Comments
 (0)