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

syntax: add parser recovery for intersection- / and-patterns `p1 @ p2` #65410

Merged
merged 5 commits into from Oct 15, 2019

Conversation

@Centril
Copy link
Member

Centril commented Oct 14, 2019

Fixes #65400.

The recovery comes in two flavors:

  1. We know that p2 is a binding so we can invert as p2 @ p1:
error: pattern on wrong side of `@`
  --> $DIR/intersection-patterns.rs:13:9
   |
LL |         Some(x) @ y => {}
   |         -------^^^-
   |         |         |
   |         |         binding on the right, should be to the left
   |         pattern on the left, should be to the right
   |         help: switch the order: `y @ Some(x)`
  1. Otherwise we emit a generic diagnostic for the lack of support for intersection patterns:
error: left-hand side of `@` must be a binding
  --> $DIR/intersection-patterns.rs:23:9
   |
LL |         Some(x) @ Some(y) => {}
   |         -------^^^-------
   |         |         |
   |         |         also a pattern
   |         interpreted as a pattern, not a binding
   |
   = note: bindings are `x`, `mut x`, `ref x`, and `ref mut x`

For more on and-patterns, see e.g. https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching#and-pattern.

r? @davidtwco
cc @varkor @lzutao

Copy link
Member

davidtwco left a comment

LGTM, r=me w/ @petrochenkov's comment and passing CI.

src/libsyntax/parse/parser/pat.rs Outdated Show resolved Hide resolved
src/test/ui/parser/intersection-patterns.stderr Outdated Show resolved Hide resolved
src/libsyntax/parse/parser/pat.rs Show resolved Hide resolved
@Centril

This comment has been minimized.

Copy link
Member Author

Centril commented Oct 14, 2019

@davidtwco Threw in two additional commits to address @varkor's review. Please take a look. :)

@varkor

This comment has been minimized.

Copy link
Member

varkor commented Oct 14, 2019

@bors r=davidtwco,varkor

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Oct 14, 2019

📌 Commit 16266a5 has been approved by davidtwco,varkor

tmandry added a commit to tmandry/rust that referenced this pull request Oct 15, 2019
…=davidtwco,varkor

syntax: add parser recovery for intersection- / and-patterns `p1 @ p2`

Fixes rust-lang#65400.

The recovery comes in two flavors:

1. We know that `p2` is a binding so we can invert as `p2 @ p1`:

```rust
error: pattern on wrong side of `@`
  --> $DIR/intersection-patterns.rs:13:9
   |
LL |         Some(x) @ y => {}
   |         -------^^^-
   |         |         |
   |         |         binding on the right, should be to the left
   |         pattern on the left, should be to the right
   |         help: switch the order: `y @ Some(x)`
```

2. Otherwise we emit a generic diagnostic for the lack of support for intersection patterns:

```rust
error: left-hand side of `@` must be a binding
  --> $DIR/intersection-patterns.rs:23:9
   |
LL |         Some(x) @ Some(y) => {}
   |         -------^^^-------
   |         |         |
   |         |         also a pattern
   |         interpreted as a pattern, not a binding
   |
   = note: bindings are `x`, `mut x`, `ref x`, and `ref mut x`
```

For more on and-patterns, see e.g. https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching#and-pattern.

r? @davidtwco
cc @varkor @lzutao
bors added a commit that referenced this pull request Oct 15, 2019
Rollup of 10 pull requests

Successful merges:

 - #65170 (rustc_metadata: Privatize private code and remove dead code)
 - #65260 (Optimize `LexicalResolve::expansion`.)
 - #65261 (Remove `Option` from `TokenStream`)
 - #65332 (std::fmt: reorder docs)
 - #65340 (Several changes to the codegen backend organization)
 - #65365 (Include const generic arguments in metadata)
 - #65398 (Bring attention to suggestions when the only difference is capitalization)
 - #65410 (syntax: add parser recovery for intersection- / and-patterns `p1 @ p2`)
 - #65415 (Remove an outdated test output file)
 - #65416 (Minor sync changes)

Failed merges:

r? @ghost
@bors bors merged commit 16266a5 into rust-lang:master Oct 15, 2019
4 checks passed
4 checks passed
pr Build #20191014.37 succeeded
Details
pr (Linux mingw-check) Linux mingw-check succeeded
Details
pr (Linux x86_64-gnu-llvm-6.0) Linux x86_64-gnu-llvm-6.0 succeeded
Details
pr (LinuxTools) LinuxTools succeeded
Details
@Centril Centril deleted the Centril:intersection-pat-recover branch Oct 15, 2019
// The user inverted the order, so help them fix that.
let mut applicability = Applicability::MachineApplicable;
lhs.walk(&mut |p| match p.kind {
// `check_match` is unhappy if the subpattern has a binding anywhere.

This comment has been minimized.

Copy link
@Centril

Centril Oct 19, 2019

Author Member

@pnkfelix This code should be removed when #65490 is implemented / stabilized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.