Skip to content

Commit

Permalink
Add newtype for trailing in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Feb 20, 2024
1 parent 4850ae8 commit acb2cee
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::pat::{CommaRecoveryMode, Expected, RecoverColon, RecoverComma};
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
use super::{
AttrWrapper, BlockMode, ClosureSpans, ForceCollect, Parser, PathStyle, Recovered, Restrictions,
SemiColonMode, SeqSep, TokenExpectType, TokenType, TrailingToken,
SemiColonMode, SeqSep, TokenExpectType, TokenType, Trailing, TrailingToken,
};

use crate::errors;
Expand Down Expand Up @@ -1561,7 +1561,7 @@ impl<'a> Parser<'a> {
return Ok(self.recover_seq_parse_error(Delimiter::Parenthesis, lo, err));
}
};
let kind = if es.len() == 1 && !trailing_comma {
let kind = if es.len() == 1 && matches!(trailing_comma, Trailing::No) {
// `(e)` is parenthesized `e`.
ExprKind::Paren(es.into_iter().next().unwrap())
} else {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use super::diagnostics::{dummy_arg, ConsumeClosingDelim};
use super::ty::{AllowPlus, RecoverQPath, RecoverReturnSign};
use super::{
AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Recovered, TrailingToken,
AttrWrapper, FollowedByType, ForceCollect, Parser, PathStyle, Recovered, Trailing,
TrailingToken,
};
use crate::errors::{self, MacroExpandsToAdtField};
use crate::fluent_generated as fluent;
Expand Down Expand Up @@ -1459,7 +1460,7 @@ impl<'a> Parser<'a> {
let (variants, _) = if self.token == TokenKind::Semi {
self.dcx().emit_err(errors::UseEmptyBlockNotSemi { span: self.token.span });
self.bump();
(thin_vec![], false)
(thin_vec![], Trailing::No)
} else {
self.parse_delim_comma_seq(Delimiter::Brace, |p| p.parse_enum_variant(id.span))
.map_err(|mut err| {
Expand Down
22 changes: 14 additions & 8 deletions compiler/rustc_parse/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ impl From<Recovered> for bool {
}
}

#[derive(Copy, Clone, Debug)]
pub enum Trailing {
No,
Yes,
}

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum TokenDescription {
ReservedIdentifier,
Expand Down Expand Up @@ -797,10 +803,10 @@ impl<'a> Parser<'a> {
sep: SeqSep,
expect: TokenExpectType,
mut f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */, Recovered)> {
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
let mut first = true;
let mut recovered = Recovered::No;
let mut trailing = false;
let mut trailing = Trailing::No;
let mut v = ThinVec::new();

while !self.expect_any_with_type(kets, expect) {
Expand Down Expand Up @@ -914,7 +920,7 @@ impl<'a> Parser<'a> {
}
}
if sep.trailing_sep_allowed && self.expect_any_with_type(kets, expect) {
trailing = true;
trailing = Trailing::Yes;
break;
}

Expand Down Expand Up @@ -992,7 +998,7 @@ impl<'a> Parser<'a> {
ket: &TokenKind,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */, Recovered)> {
) -> PResult<'a, (ThinVec<T>, Trailing, Recovered)> {
self.parse_seq_to_before_tokens(&[ket], sep, TokenExpectType::Expect, f)
}

Expand All @@ -1004,7 +1010,7 @@ impl<'a> Parser<'a> {
ket: &TokenKind,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
) -> PResult<'a, (ThinVec<T>, Trailing)> {
let (val, trailing, recovered) = self.parse_seq_to_before_end(ket, sep, f)?;
if matches!(recovered, Recovered::No) {
self.eat(ket);
Expand All @@ -1021,7 +1027,7 @@ impl<'a> Parser<'a> {
ket: &TokenKind,
sep: SeqSep,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
) -> PResult<'a, (ThinVec<T>, Trailing)> {
self.expect(bra)?;
self.parse_seq_to_end(ket, sep, f)
}
Expand All @@ -1033,7 +1039,7 @@ impl<'a> Parser<'a> {
&mut self,
delim: Delimiter,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
) -> PResult<'a, (ThinVec<T>, Trailing)> {
self.parse_unspanned_seq(
&token::OpenDelim(delim),
&token::CloseDelim(delim),
Expand All @@ -1048,7 +1054,7 @@ impl<'a> Parser<'a> {
fn parse_paren_comma_seq<T>(
&mut self,
f: impl FnMut(&mut Parser<'a>) -> PResult<'a, T>,
) -> PResult<'a, (ThinVec<T>, bool /* trailing */)> {
) -> PResult<'a, (ThinVec<T>, Trailing)> {
self.parse_delim_comma_seq(Delimiter::Parenthesis, f)
}

Expand Down
16 changes: 9 additions & 7 deletions compiler/rustc_parse/src/parser/pat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{ForceCollect, Parser, PathStyle, Restrictions, TrailingToken};
use super::{ForceCollect, Parser, PathStyle, Restrictions, Trailing, TrailingToken};
use crate::errors::{
self, AmbiguousRangePattern, DotDotDotForRemainingFields, DotDotDotRangeToPatternNotAllowed,
DotDotDotRestPattern, EnumPatternInsteadOfIdentifier, ExpectedBindingLeftOfAt,
Expand Down Expand Up @@ -696,7 +696,9 @@ impl<'a> Parser<'a> {

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

Expand All @@ -714,7 +716,7 @@ impl<'a> Parser<'a> {
},
});

self.parse_pat_range_begin_with(begin.clone(), form)?
self.parse_pat_range_begin_with(begin.clone(), form)
}
// recover ranges with parentheses around the `(start)..`
PatKind::Err(_)
Expand All @@ -729,15 +731,15 @@ impl<'a> Parser<'a> {
},
});

self.parse_pat_range_begin_with(self.mk_expr(pat.span, ExprKind::Err), form)?
self.parse_pat_range_begin_with(self.mk_expr(pat.span, ExprKind::Err), form)
}

// (pat) with optional parentheses
_ => PatKind::Paren(pat),
_ => Ok(PatKind::Paren(pat)),
}
} else {
PatKind::Tuple(fields)
})
Ok(PatKind::Tuple(fields))
}
}

/// Parse a mutable binding with the `mut` token already eaten.
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Parser, PathStyle, TokenType};
use super::{Parser, PathStyle, TokenType, Trailing};

use crate::errors::{
self, DynAfterMut, ExpectedFnPathFoundFnKeyword, ExpectedMutOrConstInRawPointerType,
Expand Down Expand Up @@ -415,7 +415,7 @@ impl<'a> Parser<'a> {
Ok(ty)
})?;

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

0 comments on commit acb2cee

Please sign in to comment.