Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fully implement or-pattern parsing #63693

Merged
merged 34 commits into from
Aug 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
5299d8a
parser: extract `ban_unexpected_or_or`.
Centril Aug 18, 2019
1ba7550
parser: type alias `type Expected = Option<&'static str>;`.
Centril Aug 18, 2019
0bbea47
parser: refactor `parse_pat_with_or` + use it in [p0, p1, ..] pats.
Centril Aug 18, 2019
30b841d
parser: improve or-patterns recovery.
Centril Aug 18, 2019
d34ee76
parser: move `multiple-pattern-typo` -> `or-patterns` directory.
Centril Aug 18, 2019
5f57fee
parser: `multiple-pattern-typo`: cover more or-pattern places.
Centril Aug 18, 2019
f852c7c
parser: simplify `parse_pat_with_or`.
Centril Aug 18, 2019
21d9b85
parser: extract `maybe_recover_unexpected_comma`.
Centril Aug 18, 2019
a4a34ab
parser: integrate `maybe_recover_unexpected_comma` in `parse_pat_with…
Centril Aug 18, 2019
7b59b4f
parser: extract `eat_or_separator`.
Centril Aug 18, 2019
dc5bbaf
parser: improve `parse_pat_with_or` docs.
Centril Aug 18, 2019
6498959
parser: use `eat_or_separator` for leading vert.
Centril Aug 18, 2019
39f5e5b
parser: move `maybe_recover_unexpected_comma` to a more appropriate p…
Centril Aug 18, 2019
8f6a0cd
parser: document `ban_unexpected_or_or`.
Centril Aug 18, 2019
b7178ef
parser: `parse_pats` -> `parse_top_pat{_unpack}`.
Centril Aug 18, 2019
92d66a1
parser: document `parse_pat`.
Centril Aug 18, 2019
95792b4
parser: `let` stmts & `for` exprs: allow or-patterns.
Centril Aug 18, 2019
5f6bec8
parser: drive-by: simplify `parse_arg_general`.
Centril Aug 18, 2019
a9af18b
move `ui/or-pattern-mismatch` -> `ui/or-patterns/`.
Centril Aug 18, 2019
f35432e
or-patterns: add syntactic tests.
Centril Aug 18, 2019
d3b3bce
move `feature-gate-or_patterns.*` -> `ui/or-patterns/`
Centril Aug 18, 2019
1ffea18
or-patterns: harden feature gating tests.
Centril Aug 18, 2019
b205055
parser: better recovery for || in inner pats.
Centril Aug 19, 2019
b2966e6
parser: bool -> GateOr.
Centril Aug 24, 2019
a9ef859
parser: bool -> TopLevel.
Centril Aug 24, 2019
3a40542
parse_top_pat: silence leading vert gating sometimes.
Centril Aug 24, 2019
e374772
parser: extract recover_inner_leading_vert.
Centril Aug 24, 2019
0ab8430
parser: reword || recovery.
Centril Aug 24, 2019
1202cb0
parser: simplify parse_pat_with_or_{inner}
Centril Aug 24, 2019
083963e
parser: 'while parsing this or-pattern...'
Centril Aug 24, 2019
1caaa40
parser: gracefully handle `fn foo(A | B: type)`.
Centril Aug 25, 2019
6a73199
or_patterns: add run-rustfix tests.
Centril Aug 25, 2019
acb1130
parser: TopLevel -> RecoverComma.
Centril Aug 25, 2019
2bd27fb
parser: fix span for leading vert.
Centril Aug 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,15 +971,12 @@ impl<'a> Parser<'a> {
/// Skips unexpected attributes and doc comments in this position and emits an appropriate
/// error.
/// This version of parse arg doesn't necessarily require identifier names.
fn parse_arg_general<F>(
fn parse_arg_general(
&mut self,
is_trait_item: bool,
allow_c_variadic: bool,
is_name_required: F,
) -> PResult<'a, Arg>
where
F: Fn(&token::Token) -> bool
{
is_name_required: impl Fn(&token::Token) -> bool,
) -> PResult<'a, Arg> {
let lo = self.token.span;
let attrs = self.parse_arg_attributes()?;
if let Some(mut arg) = self.parse_self_arg()? {
Expand All @@ -991,7 +988,7 @@ impl<'a> Parser<'a> {
let (pat, ty) = if is_name_required || self.is_named_argument() {
debug!("parse_arg_general parse_pat (is_name_required:{})", is_name_required);

let pat = self.parse_pat(Some("argument name"))?;
let pat = self.parse_fn_param_pat()?;
if let Err(mut err) = self.expect(&token::Colon) {
if let Some(ident) = self.argument_without_type(
&mut err,
Expand Down
17 changes: 10 additions & 7 deletions src/libsyntax/parse/parser/expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{Parser, PResult, Restrictions, PrevTokenKind, TokenType, PathStyle};
use super::{BlockMode, SemiColonMode};
use super::{SeqSep, TokenExpectType};
use super::pat::{GateOr, PARAM_EXPECTED};

use crate::maybe_recover_from_interpolated_ty_qpath;
use crate::ptr::P;
Expand Down Expand Up @@ -1175,7 +1176,7 @@ impl<'a> Parser<'a> {
fn parse_fn_block_arg(&mut self) -> PResult<'a, Arg> {
let lo = self.token.span;
let attrs = self.parse_arg_attributes()?;
let pat = self.parse_pat(Some("argument name"))?;
let pat = self.parse_pat(PARAM_EXPECTED)?;
let t = if self.eat(&token::Colon) {
self.parse_ty()?
} else {
Expand Down Expand Up @@ -1241,19 +1242,20 @@ impl<'a> Parser<'a> {
Ok(cond)
}

/// Parses a `let $pats = $expr` pseudo-expression.
/// Parses a `let $pat = $expr` pseudo-expression.
/// The `let` token has already been eaten.
fn parse_let_expr(&mut self, attrs: ThinVec<Attribute>) -> PResult<'a, P<Expr>> {
let lo = self.prev_span;
let pats = self.parse_pats()?;
// FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead.
let pat = self.parse_top_pat_unpack(GateOr::No)?;
self.expect(&token::Eq)?;
let expr = self.with_res(
Restrictions::NO_STRUCT_LITERAL,
|this| this.parse_assoc_expr_with(1 + prec_let_scrutinee_needs_par(), None.into())
)?;
let span = lo.to(expr.span);
self.sess.gated_spans.let_chains.borrow_mut().push(span);
Ok(self.mk_expr(span, ExprKind::Let(pats, expr), attrs))
Ok(self.mk_expr(span, ExprKind::Let(pat, expr), attrs))
}

/// `else` token already eaten
Expand Down Expand Up @@ -1283,7 +1285,7 @@ impl<'a> Parser<'a> {
_ => None,
};

let pat = self.parse_top_level_pat()?;
let pat = self.parse_top_pat(GateOr::Yes)?;
if !self.eat_keyword(kw::In) {
let in_span = self.prev_span.between(self.token.span);
self.struct_span_err(in_span, "missing `in` in `for` loop")
Expand Down Expand Up @@ -1387,7 +1389,8 @@ impl<'a> Parser<'a> {
crate fn parse_arm(&mut self) -> PResult<'a, Arm> {
let attrs = self.parse_outer_attributes()?;
let lo = self.token.span;
let pats = self.parse_pats()?;
// FIXME(or_patterns, Centril | dlrobertson): use `parse_top_pat` instead.
let pat = self.parse_top_pat_unpack(GateOr::No)?;
let guard = if self.eat_keyword(kw::If) {
Some(self.parse_expr()?)
} else {
Expand Down Expand Up @@ -1448,7 +1451,7 @@ impl<'a> Parser<'a> {

Ok(ast::Arm {
attrs,
pats,
pats: pat, // FIXME(or_patterns, Centril | dlrobertson): this should just be `pat,`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this just be a singular pat? Foo(A | B) will now be a single pattern that contains an or-pattern, but woudn't we still want to parse Foo(A) | Bar(B) as multiple patterns?

Copy link
Contributor Author

@Centril Centril Aug 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having arm.pat: P<Pat> instead of arm.pats: Vec<P<Pat>> simplifies the overall structure of the compiler by using a uniform mechanism for or-patterns instead of having a distinction between or-patterns at the top-level and in nested positions. As an example, you can see that the parser pat.rs parser has, in this PR, a hack to unpack an or-pattern at the top level. There are many other places (e.g. in resolve, save-analysis, etc.) in which it would simplify (and reduce duplication of logic) to have one uniform mechanism. So we should represent p1 | p2 at the top of a match arm simply as PatKind::Or.

Edit: As a further consideration, using Vec<P<Pat>> pessimizes what I think is the most common case of an arm with just Foo => . Moreover, let now permits or-patterns so there's no good reason to have a top-level distinction sometimes and sometimes not. In terms of preventing bugs, it is also better to have one mechanism since you can ensure that way that the semantics at the top and inner levels do not diverge.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! This makes a lot of sense and I'd love to see this happen.The lowering in MIR will require quite a bit of work and thought in order to get to this point, but AFAIK unifying the two could also greatly simplify librustc_mir/build/matches.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh absolutely. I think basically we want to work from both sides and meet in the middle. That is, we fix the AST and add a hack to lowering. Then we fix lowering + HIR and add a hack to HAIR. Eventually we get down to MIR and the hack is no more.

guard,
body: expr,
span: lo.to(hi),
Expand Down
Loading