Skip to content

Commit acb2cee

Browse files
committed
Add newtype for trailing in parser
1 parent 4850ae8 commit acb2cee

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::pat::{CommaRecoveryMode, Expected, RecoverColon, RecoverComma};
44
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
55
use super::{
66
AttrWrapper, BlockMode, ClosureSpans, ForceCollect, Parser, PathStyle, Recovered, Restrictions,
7-
SemiColonMode, SeqSep, TokenExpectType, TokenType, TrailingToken,
7+
SemiColonMode, SeqSep, TokenExpectType, TokenType, Trailing, TrailingToken,
88
};
99

1010
use crate::errors;
@@ -1561,7 +1561,7 @@ impl<'a> Parser<'a> {
15611561
return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, err));
15621562
}
15631563
};
1564-
let kind = if es.len() == 1 && !trailing_comma {
1564+
let kind = if es.len() == 1 && matches!(trailing_comma, Trailing::No) {
15651565
// `(e)` is parenthesized `e`.
15661566
ExprKind::Paren(es.into_iter().next().unwrap())
15671567
} else {

compiler/rustc_parse/src/parser/item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use super::diagnostics::{dummy_arg, ConsumeClosingDelim};
22
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
33
use super::{
4-
AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Recovered, TrailingToken,
4+
AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Recovered, Trailing,
5+
TrailingToken,
56
};
67
use crate::errors::{self, MacroExpandsToAdtField};
78
use crate::fluent_generated as fluent;
@@ -1459,7 +1460,7 @@ impl<'a> Parser<'a> {
14591460
let (variants, _) = if self.token == TokenKind::Semi {
14601461
self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
14611462
self.bump();
1462-
(thin_vec![], false)
1463+
(thin_vec![], Trailing::No)
14631464
} else {
14641465
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant(id.span))
14651466
.map_err(|mut err| {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,12 @@ impl From<Recovered> for bool {
371371
}
372372
}
373373

374+
#[derive(Copy, Clone, Debug)]
375+
pub enum Trailing {
376+
No,
377+
Yes,
378+
}
379+
374380
#[derive(Clone, Copy, PartialEq, Eq)]
375381
pub enum TokenDescription {
376382
ReservedIdentifier,
@@ -797,10 +803,10 @@ impl<'a> Parser<'a> {
797803
sep: SeqSep,
798804
expect: TokenExpectType,
799805
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
800-
) -> PResult<'a, (ThinVec<T>, bool /* trailing */, Recovered)> {
806+
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
801807
let mut first = true;
802808
let mut recovered = Recovered::No;
803-
let mut trailing = false;
809+
let mut trailing = Trailing::No;
804810
let mut v = ThinVec::new();
805811

806812
while !self.expect_any_with_type(kets, expect) {
@@ -914,7 +920,7 @@ impl<'a> Parser<'a> {
914920
}
915921
}
916922
if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
917-
trailing = true;
923+
trailing = Trailing::Yes;
918924
break;
919925
}
920926

@@ -992,7 +998,7 @@ impl<'a> Parser<'a> {
992998
ket: &TokenKind,
993999
sep: SeqSep,
9941000
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
995-
) -> PResult<'a, (ThinVec<T>, bool /* trailing */, Recovered)> {
1001+
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
9961002
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
9971003
}
9981004

@@ -1004,7 +1010,7 @@ impl<'a> Parser<'a> {
10041010
ket: &TokenKind,
10051011
sep: SeqSep,
10061012
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1007-
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
1013+
) -> PResult<'a, (ThinVec<T>, Trailing)> {
10081014
let (val, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
10091015
if matches!(recovered, Recovered::No) {
10101016
self.eat(ket);
@@ -1021,7 +1027,7 @@ impl<'a> Parser<'a> {
10211027
ket: &TokenKind,
10221028
sep: SeqSep,
10231029
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1024-
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
1030+
) -> PResult<'a, (ThinVec<T>, Trailing)> {
10251031
self.expect(bra)?;
10261032
self.parse_seq_to_end(ket, sep, f)
10271033
}
@@ -1033,7 +1039,7 @@ impl<'a> Parser<'a> {
10331039
&mut self,
10341040
delim: Delimiter,
10351041
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1036-
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
1042+
) -> PResult<'a, (ThinVec<T>, Trailing)> {
10371043
self.parse_unspanned_seq(
10381044
&token::OpenDelim(delim),
10391045
&token::CloseDelim(delim),
@@ -1048,7 +1054,7 @@ impl<'a> Parser<'a> {
10481054
fn parse_paren_comma_seq<T>(
10491055
&mut self,
10501056
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
1051-
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
1057+
) -> PResult<'a, (ThinVec<T>, Trailing)> {
10521058
self.parse_delim_comma_seq(Delimiter::Parenthesis, f)
10531059
}
10541060

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{ForceCollect, Parser, PathStyle, Restrictions, TrailingToken};
1+
use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing, TrailingToken};
22
use crate::errors::{
33
self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed,
44
DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt,
@@ -696,7 +696,9 @@ impl<'a> Parser<'a> {
696696

697697
// Here, `(pat,)` is a tuple pattern.
698698
// For backward compatibility, `(..)` is a tuple pattern as well.
699-
Ok(if fields.len() == 1 && !(trailing_comma || fields[0].is_rest()) {
699+
let paren_pattern =
700+
fields.len() == 1 && !(matches!(trailing_comma, Trailing::Yes) || fields[0].is_rest());
701+
if paren_pattern {
700702
let pat = fields.into_iter().next().unwrap();
701703
let close_paren = self.prev_token.span;
702704

@@ -714,7 +716,7 @@ impl<'a> Parser<'a> {
714716
},
715717
});
716718

717-
self.parse_pat_range_begin_with(begin.clone(), form)?
719+
self.parse_pat_range_begin_with(begin.clone(), form)
718720
}
719721
// recover ranges with parentheses around the `(start)..`
720722
PatKind::Err(_)
@@ -729,15 +731,15 @@ impl<'a> Parser<'a> {
729731
},
730732
});
731733

732-
self.parse_pat_range_begin_with(self.mk_expr(pat.span, ExprKind::Err), form)?
734+
self.parse_pat_range_begin_with(self.mk_expr(pat.span, ExprKind::Err), form)
733735
}
734736

735737
// (pat) with optional parentheses
736-
_ => PatKind::Paren(pat),
738+
_ => Ok(PatKind::Paren(pat)),
737739
}
738740
} else {
739-
PatKind::Tuple(fields)
740-
})
741+
Ok(PatKind::Tuple(fields))
742+
}
741743
}
742744

743745
/// Parse a mutable binding with the `mut` token already eaten.

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use super::{Parser, PathStyle, TokenType};
1+
use super::{Parser, PathStyle, TokenType, Trailing};
22

33
use crate::errors::{
44
self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
@@ -415,7 +415,7 @@ impl<'a> Parser<'a> {
415415
Ok(ty)
416416
})?;
417417

418-
if ts.len() == 1 && !trailing {
418+
if ts.len() == 1 && matches!(trailing, Trailing::No) {
419419
let ty = ts.into_iter().next().unwrap().into_inner();
420420
let maybe_bounds = allow_plus == AllowPlus::Yes && self.token.is_like_plus();
421421
match ty.kind {

0 commit comments

Comments
 (0)