Skip to content

Commit b7178ef

Browse files
committed
parser: parse_pats -> parse_top_pat{_unpack}.
1 parent 8f6a0cd commit b7178ef

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

src/libsyntax/parse/parser/expr.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,19 +1241,20 @@ impl<'a> Parser<'a> {
12411241
Ok(cond)
12421242
}
12431243

1244-
/// Parses a `let $pats = $expr` pseudo-expression.
1244+
/// Parses a `let $pat = $expr` pseudo-expression.
12451245
/// The `let` token has already been eaten.
12461246
fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
12471247
let lo = self.prev_span;
1248-
let pats = self.parse_pats()?;
1248+
// FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead.
1249+
let pat = self.parse_top_pat_unpack(false)?;
12491250
self.expect(&token::Eq)?;
12501251
let expr = self.with_res(
12511252
Restrictions::NO_STRUCT_LITERAL,
12521253
|this| this.parse_assoc_expr_with(1 + prec_let_scrutinee_needs_par(), None.into())
12531254
)?;
12541255
let span = lo.to(expr.span);
12551256
self.sess.gated_spans.let_chains.borrow_mut().push(span);
1256-
Ok(self.mk_expr(span, ExprKind::Let(pats, expr), attrs))
1257+
Ok(self.mk_expr(span, ExprKind::Let(pat, expr), attrs))
12571258
}
12581259

12591260
/// `else` token already eaten
@@ -1387,7 +1388,8 @@ impl<'a> Parser<'a> {
13871388
crate fn parse_arm(&mut self) -> PResult<'a, Arm> {
13881389
let attrs = self.parse_outer_attributes()?;
13891390
let lo = self.token.span;
1390-
let pats = self.parse_pats()?;
1391+
// FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead.
1392+
let pat = self.parse_top_pat_unpack(false)?;
13911393
let guard = if self.eat_keyword(kw::If) {
13921394
Some(self.parse_expr()?)
13931395
} else {
@@ -1448,7 +1450,7 @@ impl<'a> Parser<'a> {
14481450

14491451
Ok(ast::Arm {
14501452
attrs,
1451-
pats,
1453+
pats: pat, // FIXME(or_patterns, Centril | dlrobertson): this should just be `pat,`.
14521454
guard,
14531455
body: expr,
14541456
span: lo.to(hi),

src/libsyntax/parse/parser/pat.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,25 @@ impl<'a> Parser<'a> {
2020
self.parse_pat_with_range_pat(true, expected)
2121
}
2222

23-
/// Parses patterns, separated by '|' s.
24-
pub(super) fn parse_pats(&mut self) -> PResult<'a, Vec<P<Pat>>> {
25-
// Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
26-
self.eat_or_separator();
27-
28-
let mut pats = Vec::new();
29-
loop {
30-
pats.push(self.parse_top_level_pat()?);
23+
// FIXME(or_patterns, Centril | dlrobertson):
24+
// remove this and use `parse_top_pat` everywhere it is used instead.
25+
pub(super) fn parse_top_pat_unpack(&mut self, gate_or: bool) -> PResult<'a, Vec<P<Pat>>> {
26+
self.parse_top_pat(gate_or)
27+
.map(|pat| pat.and_then(|pat| match pat.node {
28+
PatKind::Or(pats) => pats,
29+
node => vec![self.mk_pat(pat.span, node)],
30+
}))
31+
}
3132

32-
if !self.eat_or_separator() {
33-
return Ok(pats);
34-
}
33+
/// Entry point to the main pattern parser.
34+
/// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
35+
pub(super) fn parse_top_pat(&mut self, gate_or: bool) -> PResult<'a, P<Pat>> {
36+
// Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
37+
if self.eat_or_separator() && gate_or {
38+
self.sess.gated_spans.or_patterns.borrow_mut().push(self.prev_span);
3539
}
40+
41+
self.parse_pat_with_or(None, gate_or, true)
3642
}
3743

3844
pub(super) fn parse_top_level_pat(&mut self) -> PResult<'a, P<Pat>> {

0 commit comments

Comments
 (0)