Skip to content

Commit a35189d

Browse files
committed
Remove TrailingToken.
It's used in `Parser::collect_tokens_trailing_token` to decide whether to capture a trailing token. But the callers actually know whether to capture a trailing token, so it's simpler for them to just pass in a bool. Also, the `TrailingToken::Gt` case was weird, because it didn't result in a trailing token being captured. It could have been subsumed by the `TrailingToken::MaybeComma` case, and it effectively is in the new code.
1 parent f8e4ac0 commit a35189d

File tree

7 files changed

+44
-88
lines changed

7 files changed

+44
-88
lines changed

compiler/rustc_parse/src/parser/attr_wrapper.rs

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::{Capturing, FlatToken, ForceCollect, Parser, ReplaceRange, TokenCursor, TrailingToken};
2-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
1+
use super::{Capturing, FlatToken, ForceCollect, Parser, ReplaceRange, TokenCursor};
2+
use rustc_ast::token::{Delimiter, Token, TokenKind};
33
use rustc_ast::tokenstream::{AttrTokenStream, AttrTokenTree, AttrsTarget, DelimSpacing};
44
use rustc_ast::tokenstream::{DelimSpan, LazyAttrTokenStream, Spacing, ToAttrTokenStream};
55
use rustc_ast::{self as ast};
@@ -165,8 +165,10 @@ impl ToAttrTokenStream for LazyAttrTokenStreamImpl {
165165
impl<'a> Parser<'a> {
166166
/// Records all tokens consumed by the provided callback,
167167
/// including the current token. These tokens are collected
168-
/// into a `LazyAttrTokenStream`, and returned along with the result
169-
/// of the callback.
168+
/// into a `LazyAttrTokenStream`, and returned along with the first part of
169+
/// the callback's result. The second (bool) part of the callback's result
170+
/// indicates if an extra token should be captured, e.g. a comma or
171+
/// semicolon.
170172
///
171173
/// The `attrs` passed in are in `AttrWrapper` form, which is opaque. The
172174
/// `AttrVec` within is passed to `f`. See the comment on `AttrWrapper` for
@@ -187,7 +189,7 @@ impl<'a> Parser<'a> {
187189
&mut self,
188190
attrs: AttrWrapper,
189191
force_collect: ForceCollect,
190-
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, TrailingToken)>,
192+
f: impl FnOnce(&mut Self, ast::AttrVec) -> PResult<'a, (R, bool)>,
191193
) -> PResult<'a, R> {
192194
// We only bail out when nothing could possibly observe the collected tokens:
193195
// 1. We cannot be force collecting tokens (since force-collecting requires tokens
@@ -212,7 +214,7 @@ impl<'a> Parser<'a> {
212214
let has_outer_attrs = !attrs.attrs.is_empty();
213215
let replace_ranges_start = self.capture_state.replace_ranges.len();
214216

215-
let (mut ret, trailing) = {
217+
let (mut ret, capture_trailing) = {
216218
let prev_capturing = mem::replace(&mut self.capture_state.capturing, Capturing::Yes);
217219
let ret_and_trailing = f(self, attrs.attrs);
218220
self.capture_state.capturing = prev_capturing;
@@ -266,27 +268,13 @@ impl<'a> Parser<'a> {
266268

267269
let replace_ranges_end = self.capture_state.replace_ranges.len();
268270

269-
// Capture a trailing token if requested by the callback 'f'
270-
let captured_trailing = match trailing {
271-
TrailingToken::None => false,
272-
TrailingToken::Gt => {
273-
assert_eq!(self.token.kind, token::Gt);
274-
false
275-
}
276-
TrailingToken::Semi => {
277-
assert_eq!(self.token.kind, token::Semi);
278-
true
279-
}
280-
TrailingToken::MaybeComma => self.token.kind == token::Comma,
281-
};
282-
283271
assert!(
284-
!(self.break_last_token && captured_trailing),
272+
!(self.break_last_token && capture_trailing),
285273
"Cannot set break_last_token and have trailing token"
286274
);
287275

288276
let end_pos = self.num_bump_calls
289-
+ captured_trailing as u32
277+
+ capture_trailing as u32
290278
// If we 'broke' the last token (e.g. breaking a '>>' token to two '>' tokens), then
291279
// extend the range of captured tokens to include it, since the parser was not actually
292280
// bumped past it. When the `LazyAttrTokenStream` gets converted into an

compiler/rustc_parse/src/parser/expr.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::pat::{CommaRecoveryMode, Expected, RecoverColon, RecoverComma};
55
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
66
use super::{
77
AttrWrapper, BlockMode, ClosureSpans, ForceCollect, Parser, PathStyle, Restrictions,
8-
SemiColonMode, SeqSep, TokenType, Trailing, TrailingToken,
8+
SemiColonMode, SeqSep, TokenType, Trailing,
99
};
1010

1111
use crate::errors;
@@ -2486,7 +2486,7 @@ impl<'a> Parser<'a> {
24862486
id: DUMMY_NODE_ID,
24872487
is_placeholder: false,
24882488
},
2489-
TrailingToken::MaybeComma,
2489+
this.token == token::Comma,
24902490
))
24912491
})
24922492
}
@@ -3262,7 +3262,7 @@ impl<'a> Parser<'a> {
32623262
id: DUMMY_NODE_ID,
32633263
is_placeholder: false,
32643264
},
3265-
TrailingToken::None,
3265+
false,
32663266
))
32673267
})
32683268
}
@@ -3771,7 +3771,7 @@ impl<'a> Parser<'a> {
37713771
id: DUMMY_NODE_ID,
37723772
is_placeholder: false,
37733773
},
3774-
TrailingToken::MaybeComma,
3774+
false,
37753775
))
37763776
})
37773777
}
@@ -3867,18 +3867,12 @@ impl<'a> Parser<'a> {
38673867
) -> PResult<'a, P<Expr>> {
38683868
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
38693869
let res = f(this, attrs)?;
3870-
let trailing = if this.restrictions.contains(Restrictions::STMT_EXPR)
3871-
&& this.token.kind == token::Semi
3872-
{
3873-
TrailingToken::Semi
3874-
} else if this.token.kind == token::Gt {
3875-
TrailingToken::Gt
3876-
} else {
3877-
// FIXME - pass this through from the place where we know
3878-
// we need a comma, rather than assuming that `#[attr] expr,`
3879-
// always captures a trailing comma
3880-
TrailingToken::MaybeComma
3881-
};
3870+
let trailing = (this.restrictions.contains(Restrictions::STMT_EXPR)
3871+
&& this.token.kind == token::Semi)
3872+
// FIXME: pass an additional condition through from the place
3873+
// where we know we need a comma, rather than assuming that
3874+
// `#[attr] expr,` always captures a trailing comma.
3875+
|| this.token.kind == token::Comma;
38823876
Ok((res, trailing))
38833877
})
38843878
}

compiler/rustc_parse/src/parser/generics.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::errors::{
44
WhereClauseBeforeTupleStructBodySugg,
55
};
66

7-
use super::{ForceCollect, Parser, TrailingToken};
7+
use super::{ForceCollect, Parser};
88

99
use ast::token::Delimiter;
1010
use rustc_ast::token;
@@ -229,13 +229,13 @@ impl<'a> Parser<'a> {
229229
span: where_predicate.span(),
230230
});
231231
// FIXME - try to continue parsing other generics?
232-
return Ok((None, TrailingToken::None));
232+
return Ok((None, false));
233233
}
234234
Err(err) => {
235235
err.cancel();
236236
// FIXME - maybe we should overwrite 'self' outside of `collect_tokens`?
237237
this.restore_snapshot(snapshot);
238-
return Ok((None, TrailingToken::None));
238+
return Ok((None, false));
239239
}
240240
}
241241
} else {
@@ -249,14 +249,14 @@ impl<'a> Parser<'a> {
249249
.emit_err(errors::AttrWithoutGenerics { span: attrs[0].span });
250250
}
251251
}
252-
return Ok((None, TrailingToken::None));
252+
return Ok((None, false));
253253
};
254254

255255
if !this.eat(&token::Comma) {
256256
done = true;
257257
}
258-
// We just ate the comma, so no need to use `TrailingToken`
259-
Ok((param, TrailingToken::None))
258+
// We just ate the comma, so no need to capture the trailing token.
259+
Ok((param, false))
260260
})?;
261261

262262
if let Some(param) = param {

compiler/rustc_parse/src/parser/item.rs

+10-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use super::diagnostics::{dummy_arg, ConsumeClosingDelim};
22
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
3-
use super::{
4-
AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Trailing, TrailingToken,
5-
};
3+
use super::{AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Trailing};
64
use crate::errors::{self, MacroExpandsToAdtField};
75
use crate::fluent_generated as fluent;
86
use crate::maybe_whole;
@@ -132,7 +130,7 @@ impl<'a> Parser<'a> {
132130
self.collect_tokens_trailing_token(attrs, force_collect, |this: &mut Self, attrs| {
133131
let item =
134132
this.parse_item_common_(attrs, mac_allowed, attrs_allowed, fn_parse_mode);
135-
Ok((item?, TrailingToken::None))
133+
Ok((item?, false))
136134
})?;
137135

138136
Ok(item)
@@ -1570,7 +1568,7 @@ impl<'a> Parser<'a> {
15701568

15711569
let vis = this.parse_visibility(FollowedByType::No)?;
15721570
if !this.recover_nested_adt_item(kw::Enum)? {
1573-
return Ok((None, TrailingToken::None));
1571+
return Ok((None, false));
15741572
}
15751573
let ident = this.parse_field_ident("enum", vlo)?;
15761574

@@ -1582,7 +1580,7 @@ impl<'a> Parser<'a> {
15821580
this.bump();
15831581
this.parse_delim_args()?;
15841582

1585-
return Ok((None, TrailingToken::MaybeComma));
1583+
return Ok((None, this.token == token::Comma));
15861584
}
15871585

15881586
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
@@ -1639,7 +1637,7 @@ impl<'a> Parser<'a> {
16391637
is_placeholder: false,
16401638
};
16411639

1642-
Ok((Some(vr), TrailingToken::MaybeComma))
1640+
Ok((Some(vr), this.token == token::Comma))
16431641
},
16441642
)
16451643
.map_err(|mut err| {
@@ -1831,7 +1829,7 @@ impl<'a> Parser<'a> {
18311829
attrs,
18321830
is_placeholder: false,
18331831
},
1834-
TrailingToken::MaybeComma,
1832+
p.token == token::Comma,
18351833
))
18361834
})
18371835
})
@@ -1846,8 +1844,7 @@ impl<'a> Parser<'a> {
18461844
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
18471845
let lo = this.token.span;
18481846
let vis = this.parse_visibility(FollowedByType::No)?;
1849-
this.parse_single_struct_field(adt_ty, lo, vis, attrs)
1850-
.map(|field| (field, TrailingToken::None))
1847+
this.parse_single_struct_field(adt_ty, lo, vis, attrs).map(|field| (field, false))
18511848
})
18521849
}
18531850

@@ -2746,7 +2743,7 @@ impl<'a> Parser<'a> {
27462743
if let Some(mut param) = this.parse_self_param()? {
27472744
param.attrs = attrs;
27482745
let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
2749-
return Ok((res?, TrailingToken::None));
2746+
return Ok((res?, false));
27502747
}
27512748

27522749
let is_name_required = match this.token.kind {
@@ -2762,7 +2759,7 @@ impl<'a> Parser<'a> {
27622759
this.parameter_without_type(&mut err, pat, is_name_required, first_param)
27632760
{
27642761
let guar = err.emit();
2765-
Ok((dummy_arg(ident, guar), TrailingToken::None))
2762+
Ok((dummy_arg(ident, guar), false))
27662763
} else {
27672764
Err(err)
27682765
};
@@ -2805,7 +2802,7 @@ impl<'a> Parser<'a> {
28052802

28062803
Ok((
28072804
Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
2808-
TrailingToken::None,
2805+
false,
28092806
))
28102807
})
28112808
}

compiler/rustc_parse/src/parser/mod.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,6 @@ pub enum ForceCollect {
9191
No,
9292
}
9393

94-
#[derive(Debug, Eq, PartialEq)]
95-
pub enum TrailingToken {
96-
None,
97-
Semi,
98-
Gt,
99-
/// If the trailing token is a comma, then capture it
100-
/// Otherwise, ignore the trailing token
101-
MaybeComma,
102-
}
103-
10494
/// Like `maybe_whole_expr`, but for things other than expressions.
10595
#[macro_export]
10696
macro_rules! maybe_whole {
@@ -1509,7 +1499,7 @@ impl<'a> Parser<'a> {
15091499
self.collect_tokens_trailing_token(
15101500
AttrWrapper::empty(),
15111501
ForceCollect::Yes,
1512-
|this, _attrs| Ok((f(this)?, TrailingToken::None)),
1502+
|this, _attrs| Ok((f(this)?, false)),
15131503
)
15141504
}
15151505

compiler/rustc_parse/src/parser/pat.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing, TrailingToken};
1+
use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing};
22
use crate::errors::{
33
self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed,
44
DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt,
@@ -1297,9 +1297,8 @@ impl<'a> Parser<'a> {
12971297

12981298
last_non_comma_dotdot_span = Some(this.prev_token.span);
12991299

1300-
// We just ate a comma, so there's no need to use
1301-
// `TrailingToken::Comma`
1302-
Ok((field, TrailingToken::None))
1300+
// We just ate a comma, so there's no need to capture a trailing token.
1301+
Ok((field, false))
13031302
})?;
13041303

13051304
fields.push(field)

compiler/rustc_parse/src/parser/stmt.rs

+4-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use super::diagnostics::AttemptLocalParseRecovery;
33
use super::expr::LhsExpr;
44
use super::pat::{PatternLocation, RecoverComma};
55
use super::path::PathStyle;
6-
use super::TrailingToken;
76
use super::{
87
AttrWrapper, BlockMode, FnParseMode, ForceCollect, Parser, Restrictions, SemiColonMode,
98
};
@@ -149,11 +148,7 @@ impl<'a> Parser<'a> {
149148

150149
if this.eat(&token::Not) {
151150
let stmt_mac = this.parse_stmt_mac(lo, attrs, path)?;
152-
if this.token == token::Semi {
153-
return Ok((stmt_mac, TrailingToken::Semi));
154-
} else {
155-
return Ok((stmt_mac, TrailingToken::None));
156-
}
151+
return Ok((stmt_mac, this.token == token::Semi));
157152
}
158153

159154
let expr = if this.eat(&token::OpenDelim(Delimiter::Brace)) {
@@ -167,7 +162,7 @@ impl<'a> Parser<'a> {
167162
this.parse_expr_dot_or_call_with(expr, lo, attrs)
168163
})?;
169164
// `DUMMY_SP` will get overwritten later in this function
170-
Ok((this.mk_stmt(rustc_span::DUMMY_SP, StmtKind::Expr(expr)), TrailingToken::None))
165+
Ok((this.mk_stmt(rustc_span::DUMMY_SP, StmtKind::Expr(expr)), false))
171166
})?;
172167

173168
if let StmtKind::Expr(expr) = stmt.kind {
@@ -241,10 +236,7 @@ impl<'a> Parser<'a> {
241236
self.collect_tokens_trailing_token(attrs, ForceCollect::Yes, |this, attrs| {
242237
let local = this.parse_local(attrs)?;
243238
// FIXME - maybe capture semicolon in recovery?
244-
Ok((
245-
this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)),
246-
TrailingToken::None,
247-
))
239+
Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), false))
248240
})?;
249241
self.dcx()
250242
.emit_err(errors::InvalidVariableDeclaration { span: lo, sub: subdiagnostic(lo) });
@@ -261,11 +253,7 @@ impl<'a> Parser<'a> {
261253
self.collect_tokens_trailing_token(attrs, force_collect, |this, attrs| {
262254
this.expect_keyword(kw::Let)?;
263255
let local = this.parse_local(attrs)?;
264-
let trailing = if capture_semi && this.token.kind == token::Semi {
265-
TrailingToken::Semi
266-
} else {
267-
TrailingToken::None
268-
};
256+
let trailing = capture_semi && this.token.kind == token::Semi;
269257
Ok((this.mk_stmt(lo.to(this.prev_token.span), StmtKind::Let(local)), trailing))
270258
})
271259
}

0 commit comments

Comments
 (0)