Skip to content

Commit d81472f

Browse files
committed
Eliminate word_or_empty methods.
To get rid of the `Ident::empty` uses. This requires introducing `PathParser::word_sym`, as an alternative to `PathParser::word`.
1 parent ab62d56 commit d81472f

5 files changed

Lines changed: 55 additions & 61 deletions

File tree

‎compiler/rustc_attr_parsing/src/attributes/allow_unstable.rs‎

Lines changed: 1 addition & 1 deletion
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-
ifletSome(ident) = param.meta_item().and_then(|i| i.word_without_args()){
56+
ifletSome(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‎

Lines changed: 11 additions & 15 deletions
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
usesuper::SingleAttributeParser;
@@ -13,16 +12,13 @@ pub(crate) struct DeprecationParser;
1312

1413
fnget(
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
returnNone;
2723
}
2824
ifletSome(v) = arg.name_value(){
@@ -83,16 +79,16 @@ impl SingleAttributeParser for DeprecationParser {
8379
returnNone;
8480
};
8581

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

88-
matchident.name{
89-
sym::since => {
90-
since = Some(get(cx,ident, param_span,arg,&since)?);
84+
matchident_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‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -96,57 +96,65 @@ 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();
100-
101-
match(ident.name, args){
102-
(sym::align,ArgParser::NoArgs) => {
103-
cx.emit_err(session_diagnostics::InvalidReprAlignNeedArg{span: ident.span});
99+
let ident = param.path_without_args().word();
100+
let ident_span = ident.map_or(rustc_span::DUMMY_SP, |ident| ident.span);
101+
let name = ident.map(|ident| ident.name);
102+
let args = param.args();
103+
104+
match(name, args){
105+
(Some(sym::align),ArgParser::NoArgs) => {
106+
cx.emit_err(session_diagnostics::InvalidReprAlignNeedArg{span: ident_span });
104107
None
105108
}
106-
(sym::align,ArgParser::List(l)) => parse_repr_align(cx, l, param.span(),AlignKind::Align),
109+
(Some(sym::align),ArgParser::List(l)) => {
110+
parse_repr_align(cx, l, param.span(),AlignKind::Align)
111+
}
107112

108-
(sym::packed,ArgParser::NoArgs) => Some(ReprPacked(Align::ONE)),
109-
(sym::packed,ArgParser::List(l)) => {
113+
(Some(sym::packed),ArgParser::NoArgs) => Some(ReprPacked(Align::ONE)),
114+
(Some(sym::packed),ArgParser::List(l)) => {
110115
parse_repr_align(cx, l, param.span(),AlignKind::Packed)
111116
}
112117

113-
(sym::align | sym::packed,ArgParser::NameValue(l)) => {
118+
(Some(sym::align | sym::packed),ArgParser::NameValue(l)) => {
114119
cx.emit_err(session_diagnostics::IncorrectReprFormatGeneric{
115120
span: param.span(),
116121
// FIXME(jdonszelmann) can just be a string in the diag type
117-
repr_arg:&ident.to_string(),
122+
repr_arg:&ident.unwrap().to_string(),
118123
cause:IncorrectReprFormatGenericCause::from_lit_kind(
119124
param.span(),
120125
&l.value_as_lit().kind,
121-
ident.name.as_str(),
126+
ident.unwrap().as_str(),
122127
),
123128
});
124129
None
125130
}
126131

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) => {
132+
(Some(sym::Rust),ArgParser::NoArgs) => Some(ReprRust),
133+
(Some(sym::C),ArgParser::NoArgs) => Some(ReprC),
134+
(Some(sym::simd),ArgParser::NoArgs) => Some(ReprSimd),
135+
(Some(sym::transparent),ArgParser::NoArgs) => Some(ReprTransparent),
136+
(Some(i @ int_pat!()),ArgParser::NoArgs) => {
132137
// int_pat!() should make sure it always parses
133138
Some(ReprInt(int_type_of_word(i).unwrap()))
134139
}
135140

136141
(
137-
sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!(),
142+
Some(sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!()),
138143
ArgParser::NameValue(_),
139144
) => {
140145
cx.emit_err(session_diagnostics::InvalidReprHintNoValue{
141146
span: param.span(),
142-
name: ident.to_string(),
147+
name: ident.unwrap().to_string(),
143148
});
144149
None
145150
}
146-
(sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!(),ArgParser::List(_)) => {
151+
(
152+
Some(sym::Rust | sym::C | sym::simd | sym::transparent | int_pat!()),
153+
ArgParser::List(_),
154+
) => {
147155
cx.emit_err(session_diagnostics::InvalidReprHintNoParen{
148156
span: param.span(),
149-
name: ident.to_string(),
157+
name: ident.unwrap().to_string(),
150158
});
151159
None
152160
}

‎compiler/rustc_attr_parsing/src/attributes/stability.rs‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,9 @@ pub(crate) fn parse_stability(
242242
returnNone;
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
returnNone;
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‎

Lines changed: 2 additions & 13 deletions
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-
pubfnword_or_empty(&self) -> Ident{
82-
self.word().unwrap_or_else(Ident::empty)
81+
pubfnword_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-
pubfnword_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-
pubfnword_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.

0 commit comments

Comments
 (0)