Skip to content

Commit 487802d

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 4bb2f27 commit 487802d

File tree

7 files changed

+45
-89
lines changed

7 files changed

+45
-89
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;
@@ -2474,7 +2474,7 @@ impl<'a> Parser<'a> {
24742474
id: DUMMY_NODE_ID,
24752475
is_placeholder: false,
24762476
},
2477-
TrailingToken::MaybeComma,
2477+
this.token == token::Comma,
24782478
))
24792479
})
24802480
}
@@ -3257,7 +3257,7 @@ impl<'a> Parser<'a> {
32573257
id: DUMMY_NODE_ID,
32583258
is_placeholder: false,
32593259
},
3260-
TrailingToken::None,
3260+
false,
32613261
))
32623262
})
32633263
}
@@ -3766,7 +3766,7 @@ impl<'a> Parser<'a> {
37663766
id: DUMMY_NODE_ID,
37673767
is_placeholder: false,
37683768
},
3769-
TrailingToken::MaybeComma,
3769+
this.token == token::Comma,
37703770
))
37713771
})
37723772
}
@@ -3862,18 +3862,12 @@ impl<'a> Parser<'a> {
38623862
) -> PResult<'a, P<Expr>> {
38633863
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
38643864
let res = f(this, attrs)?;
3865-
let trailing = if this.restrictions.contains(Restrictions::STMT_EXPR)
3866-
&& this.token.kind == token::Semi
3867-
{
3868-
TrailingToken::Semi
3869-
} else if this.token.kind == token::Gt {
3870-
TrailingToken::Gt
3871-
} else {
3872-
// FIXME - pass this through from the place where we know
3873-
// we need a comma, rather than assuming that `#[attr] expr,`
3874-
// always captures a trailing comma
3875-
TrailingToken::MaybeComma
3876-
};
3865+
let trailing = (this.restrictions.contains(Restrictions::STMT_EXPR)
3866+
&& this.token.kind == token::Semi)
3867+
// FIXME: pass an additional condition through from the place
3868+
// where we know we need a comma, rather than assuming that
3869+
// `#[attr] expr,` always captures a trailing comma.
3870+
|| this.token.kind == token::Comma;
38773871
Ok((res, trailing))
38783872
})
38793873
}

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

+11-14
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;
@@ -146,7 +144,7 @@ impl<'a> Parser<'a> {
146144
let span = lo.to(this.prev_token.span);
147145
let id = DUMMY_NODE_ID;
148146
let item = Item { ident, attrs, id, kind, vis, span, tokens: None };
149-
return Ok((Some(item), TrailingToken::None));
147+
return Ok((Some(item), false));
150148
}
151149

152150
// At this point, we have failed to parse an item.
@@ -161,7 +159,7 @@ impl<'a> Parser<'a> {
161159
if !attrs_allowed {
162160
this.recover_attrs_no_item(&attrs)?;
163161
}
164-
Ok((None, TrailingToken::None))
162+
Ok((None, false))
165163
})
166164
}
167165

@@ -1555,7 +1553,7 @@ impl<'a> Parser<'a> {
15551553

15561554
let vis = this.parse_visibility(FollowedByType::No)?;
15571555
if !this.recover_nested_adt_item(kw::Enum)? {
1558-
return Ok((None, TrailingToken::None));
1556+
return Ok((None, false));
15591557
}
15601558
let ident = this.parse_field_ident("enum", vlo)?;
15611559

@@ -1567,7 +1565,7 @@ impl<'a> Parser<'a> {
15671565
this.bump();
15681566
this.parse_delim_args()?;
15691567

1570-
return Ok((None, TrailingToken::MaybeComma));
1568+
return Ok((None, this.token == token::Comma));
15711569
}
15721570

15731571
let struct_def = if this.check(&token::OpenDelim(Delimiter::Brace)) {
@@ -1624,7 +1622,7 @@ impl<'a> Parser<'a> {
16241622
is_placeholder: false,
16251623
};
16261624

1627-
Ok((Some(vr), TrailingToken::MaybeComma))
1625+
Ok((Some(vr), this.token == token::Comma))
16281626
},
16291627
)
16301628
.map_err(|mut err| {
@@ -1816,7 +1814,7 @@ impl<'a> Parser<'a> {
18161814
attrs,
18171815
is_placeholder: false,
18181816
},
1819-
TrailingToken::MaybeComma,
1817+
p.token == token::Comma,
18201818
))
18211819
})
18221820
})
@@ -1831,8 +1829,7 @@ impl<'a> Parser<'a> {
18311829
self.collect_tokens_trailing_token(attrs, ForceCollect::No, |this, attrs| {
18321830
let lo = this.token.span;
18331831
let vis = this.parse_visibility(FollowedByType::No)?;
1834-
this.parse_single_struct_field(adt_ty, lo, vis, attrs)
1835-
.map(|field| (field, TrailingToken::None))
1832+
this.parse_single_struct_field(adt_ty, lo, vis, attrs).map(|field| (field, false))
18361833
})
18371834
}
18381835

@@ -2735,7 +2732,7 @@ impl<'a> Parser<'a> {
27352732
if let Some(mut param) = this.parse_self_param()? {
27362733
param.attrs = attrs;
27372734
let res = if first_param { Ok(param) } else { this.recover_bad_self_param(param) };
2738-
return Ok((res?, TrailingToken::None));
2735+
return Ok((res?, false));
27392736
}
27402737

27412738
let is_name_required = match this.token.kind {
@@ -2751,7 +2748,7 @@ impl<'a> Parser<'a> {
27512748
this.parameter_without_type(&mut err, pat, is_name_required, first_param)
27522749
{
27532750
let guar = err.emit();
2754-
Ok((dummy_arg(ident, guar), TrailingToken::None))
2751+
Ok((dummy_arg(ident, guar), false))
27552752
} else {
27562753
Err(err)
27572754
};
@@ -2794,7 +2791,7 @@ impl<'a> Parser<'a> {
27942791

27952792
Ok((
27962793
Param { attrs, id: ast::DUMMY_NODE_ID, is_placeholder: false, pat, span, ty },
2797-
TrailingToken::None,
2794+
false,
27982795
))
27992796
})
28002797
}

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
#[macro_export]
10595
macro_rules! maybe_whole {
10696
($p:expr, $constructor:ident, |$x:ident| $e:expr) => {
@@ -1508,7 +1498,7 @@ impl<'a> Parser<'a> {
15081498
self.collect_tokens_trailing_token(
15091499
AttrWrapper::empty(),
15101500
ForceCollect::Yes,
1511-
|this, _attrs| Ok((f(this)?, TrailingToken::None)),
1501+
|this, _attrs| Ok((f(this)?, false)),
15121502
)
15131503
}
15141504

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,
@@ -1315,9 +1315,8 @@ impl<'a> Parser<'a> {
13151315

13161316
last_non_comma_dotdot_span = Some(this.prev_token.span);
13171317

1318-
// We just ate a comma, so there's no need to use
1319-
// `TrailingToken::Comma`
1320-
Ok((field, TrailingToken::None))
1318+
// We just ate a comma, so there's no need to capture a trailing token.
1319+
Ok((field, false))
13211320
})?;
13221321

13231322
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(attrs, expr, lo)
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)