1
Fork 0

parser: bool -> GateOr.

This commit is contained in:
Mazdak Farrokhzad 2019-08-24 21:43:28 +02:00
parent b205055c7b
commit b2966e651d
3 changed files with 16 additions and 10 deletions

View file

@ -1,6 +1,7 @@
use super::{Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle}; use super::{Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle};
use super::{BlockMode, SemiColonMode}; use super::{BlockMode, SemiColonMode};
use super::{SeqSep, TokenExpectType}; use super::{SeqSep, TokenExpectType};
use super::pat::GateOr;
use crate::maybe_recover_from_interpolated_ty_qpath; use crate::maybe_recover_from_interpolated_ty_qpath;
use crate::ptr::P; use crate::ptr::P;
@ -1246,7 +1247,7 @@ impl<'a> Parser<'a> {
fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> { fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
let lo = self.prev_span; let lo = self.prev_span;
// FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead. // FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead.
let pat = self.parse_top_pat_unpack(false)?; let pat = self.parse_top_pat_unpack(GateOr::No)?;
self.expect(&token::Eq)?; self.expect(&token::Eq)?;
let expr = self.with_res( let expr = self.with_res(
Restrictions::NO_STRUCT_LITERAL, Restrictions::NO_STRUCT_LITERAL,
@ -1284,7 +1285,7 @@ impl<'a> Parser<'a> {
_ => None, _ => None,
}; };
let pat = self.parse_top_pat(true)?; let pat = self.parse_top_pat(GateOr::Yes)?;
if !self.eat_keyword(kw::In) { if !self.eat_keyword(kw::In) {
let in_span = self.prev_span.between(self.token.span); let in_span = self.prev_span.between(self.token.span);
self.struct_span_err(in_span, "missing `in` in `for` loop") self.struct_span_err(in_span, "missing `in` in `for` loop")
@ -1389,7 +1390,7 @@ impl<'a> Parser<'a> {
let attrs = self.parse_outer_attributes()?; let attrs = self.parse_outer_attributes()?;
let lo = self.token.span; let lo = self.token.span;
// FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead. // FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead.
let pat = self.parse_top_pat_unpack(false)?; let pat = self.parse_top_pat_unpack(GateOr::No)?;
let guard = if self.eat_keyword(kw::If) { let guard = if self.eat_keyword(kw::If) {
Some(self.parse_expr()?) Some(self.parse_expr()?)
} else { } else {

View file

@ -14,6 +14,10 @@ use errors::{Applicability, DiagnosticBuilder};
type Expected = Option<&'static str>; type Expected = Option<&'static str>;
/// Whether or not an or-pattern should be gated when occurring in the current context.
#[derive(PartialEq)]
pub enum GateOr { Yes, No }
impl<'a> Parser<'a> { impl<'a> Parser<'a> {
/// Parses a pattern. /// Parses a pattern.
/// ///
@ -26,7 +30,7 @@ impl<'a> Parser<'a> {
// FIXME(or_patterns, Centril | dlrobertson): // FIXME(or_patterns, Centril | dlrobertson):
// remove this and use `parse_top_pat` everywhere it is used instead. // remove this and use `parse_top_pat` everywhere it is used instead.
pub(super) fn parse_top_pat_unpack(&mut self, gate_or: bool) -> PResult<'a, Vec<P<Pat>>> { pub(super) fn parse_top_pat_unpack(&mut self, gate_or: GateOr) -> PResult<'a, Vec<P<Pat>>> {
self.parse_top_pat(gate_or) self.parse_top_pat(gate_or)
.map(|pat| pat.and_then(|pat| match pat.node { .map(|pat| pat.and_then(|pat| match pat.node {
PatKind::Or(pats) => pats, PatKind::Or(pats) => pats,
@ -36,9 +40,9 @@ impl<'a> Parser<'a> {
/// Entry point to the main pattern parser. /// Entry point to the main pattern parser.
/// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level. /// Corresponds to `top_pat` in RFC 2535 and allows or-pattern at the top level.
pub(super) fn parse_top_pat(&mut self, gate_or: bool) -> PResult<'a, P<Pat>> { pub(super) fn parse_top_pat(&mut self, gate_or: GateOr) -> PResult<'a, P<Pat>> {
// Allow a '|' before the pats (RFCs 1925, 2530, and 2535). // Allow a '|' before the pats (RFCs 1925, 2530, and 2535).
if self.eat_or_separator() && gate_or { if self.eat_or_separator() && gate_or == GateOr::Yes {
self.sess.gated_spans.or_patterns.borrow_mut().push(self.prev_span); self.sess.gated_spans.or_patterns.borrow_mut().push(self.prev_span);
} }
@ -50,7 +54,7 @@ impl<'a> Parser<'a> {
fn parse_pat_with_or( fn parse_pat_with_or(
&mut self, &mut self,
expected: Expected, expected: Expected,
gate_or: bool, gate_or: GateOr,
top_level: bool top_level: bool
) -> PResult<'a, P<Pat>> { ) -> PResult<'a, P<Pat>> {
// Parse the first pattern. // Parse the first pattern.
@ -73,7 +77,7 @@ impl<'a> Parser<'a> {
let or_pattern_span = lo.to(self.prev_span); let or_pattern_span = lo.to(self.prev_span);
// Feature gate the or-pattern if instructed: // Feature gate the or-pattern if instructed:
if gate_or { if gate_or == GateOr::Yes {
self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span); self.sess.gated_spans.or_patterns.borrow_mut().push(or_pattern_span);
} }
@ -171,7 +175,7 @@ impl<'a> Parser<'a> {
self.bump(); self.bump();
} }
self.parse_pat_with_or(expected, true, false) self.parse_pat_with_or(expected, GateOr::Yes, false)
} }
/// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are /// Parses a pattern, with a setting whether modern range patterns (e.g., `a..=b`, `a..b` are

View file

@ -1,6 +1,7 @@
use super::{Parser, PResult, Restrictions, PrevTokenKind, SemiColonMode, BlockMode}; use super::{Parser, PResult, Restrictions, PrevTokenKind, SemiColonMode, BlockMode};
use super::expr::LhsExpr; use super::expr::LhsExpr;
use super::path::PathStyle; use super::path::PathStyle;
use super::pat::GateOr;
use crate::ptr::P; use crate::ptr::P;
use crate::{maybe_whole, ThinVec}; use crate::{maybe_whole, ThinVec};
@ -207,7 +208,7 @@ impl<'a> Parser<'a> {
/// Parses a local variable declaration. /// Parses a local variable declaration.
fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> { fn parse_local(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Local>> {
let lo = self.prev_span; let lo = self.prev_span;
let pat = self.parse_top_pat(true)?; let pat = self.parse_top_pat(GateOr::Yes)?;
let (err, ty) = if self.eat(&token::Colon) { let (err, ty) = if self.eat(&token::Colon) {
// Save the state of the parser before parsing type normally, in case there is a `:` // Save the state of the parser before parsing type normally, in case there is a `:`