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

Expect pipe symbol after closure parameter list #44332

Merged
merged 1 commit into from
Sep 10, 2017

Conversation

tirr-c
Copy link
Contributor

@tirr-c tirr-c commented Sep 5, 2017

Fixes #44021.


Originally, the parser just called bump to discard following token after parsing closure parameter list, because it assumes | is following. However, the following code breaks the assumption:

struct MyStruct;
impl MyStruct {
   fn f() {|x, y}
}

Here, the parameter list is x, y and the following token is }. The parser discards }, and then we have a curly bracket mismatch.

Indeed, this code has a syntax error. On current nightly, the compiler emits an syntax error, but with incorrect message and span, followed by an ICE.

error: expected expression, found `}`
 --> 44021.rs:4:1
  |
4 | }
  | ^

error: internal compiler error: unexpected panic

Even worse, on current stable(1.20.0), the compiler falls into an infinite loop.

This pull request fixes this problem. Now the compiler emits correct error message and span, and does not ICE.

error: expected one of `:`, `@`, or `|`, found `}`
 --> 44021.rs:3:20
  |
3 |     fn foo() {|x, y}
  |                    ^ expected one of `:`, `@`, or `|` here

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pnkfelix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@arielb1
Copy link
Contributor

arielb1 commented Sep 5, 2017

r? @petrochenkov

@arielb1
Copy link
Contributor

arielb1 commented Sep 5, 2017

Does your patch fix the ICE even if there is a curly bracket mismatch:

struct MyStruct;
impl MyStruct {
   fn f() {|x, y}

@tirr-c
Copy link
Contributor Author

tirr-c commented Sep 5, 2017

It doesn't ICE. It seems that the ICE was caused by the untracked consuming of CloseDelim.

error: this file contains an un-closed delimiter
 --> 44021.rs:3:19
  |
3 |    fn f() {|x, y}
  |                   ^
  |
help: did you mean to close this delimiter?
 --> 44021.rs:2:15
  |
2 | impl MyStruct {
  |               ^

error: expected one of `:`, `@`, or `|`, found `}`
 --> 44021.rs:3:17
  |
3 |    fn f() {|x, y}
  |                 ^ expected one of `:`, `@`, or `|` here

@arielb1 arielb1 added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 5, 2017
@arielb1 arielb1 assigned petrochenkov and unassigned pnkfelix Sep 5, 2017
@@ -4699,7 +4699,7 @@ impl<'a> Parser<'a> {
SeqSep::trailing_allowed(token::Comma),
|p| p.parse_fn_block_arg()
);
self.bump();
self.expect(&token::BinOp(token::Or))?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm, token::OrOr should be possible here as well in theory, e.g.

#![allow(unused)]

fn main() {
    let x = |x, y| |a, b| 0; // Legal
    x(1, 2)(3, 4);
}

, but |x, y||a, b| 0 (without space) doesn't parse on stable/beta/nightly already.
Worth fixing as well!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm trying to fix this. Should I work on this double-closure parsing bug in a new PR?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this can be done separately to avoid blocking this PR.

@petrochenkov
Copy link
Contributor

petrochenkov commented Sep 6, 2017

@tirr-c
r=me unless you want to fix #44332 (comment)

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Sep 7, 2017

📌 Commit 258ec30 has been approved by petrochenkov

@Mark-Simulacrum
Copy link
Member

@bors rollup

frewsxcv added a commit to frewsxcv/rust that referenced this pull request Sep 9, 2017
Expect pipe symbol after closure parameter list

Fixes rust-lang#44021.

---

Originally, the parser just called `bump` to discard following token after parsing closure parameter list, because it assumes `|` is following. However, the following code breaks the assumption:

```rust
struct MyStruct;
impl MyStruct {
   fn f() {|x, y}
}
```

Here, the parameter list is `x, y` and the following token is `}`. The parser discards `}`, and then we have a curly bracket mismatch.

Indeed, this code has a syntax error. On current nightly, the compiler emits an syntax error, but with incorrect message and span, followed by an ICE.

```
error: expected expression, found `}`
 --> 44021.rs:4:1
  |
4 | }
  | ^

error: internal compiler error: unexpected panic
```

Even worse, on current stable(1.20.0), the compiler falls into an infinite loop.

This pull request fixes this problem. Now the compiler emits correct error message and span, and does not ICE.

```
error: expected one of `:`, `@`, or `|`, found `}`
 --> 44021.rs:3:20
  |
3 |     fn foo() {|x, y}
  |                    ^ expected one of `:`, `@`, or `|` here
```
frewsxcv added a commit to frewsxcv/rust that referenced this pull request Sep 9, 2017
Expect pipe symbol after closure parameter list

Fixes rust-lang#44021.

---

Originally, the parser just called `bump` to discard following token after parsing closure parameter list, because it assumes `|` is following. However, the following code breaks the assumption:

```rust
struct MyStruct;
impl MyStruct {
   fn f() {|x, y}
}
```

Here, the parameter list is `x, y` and the following token is `}`. The parser discards `}`, and then we have a curly bracket mismatch.

Indeed, this code has a syntax error. On current nightly, the compiler emits an syntax error, but with incorrect message and span, followed by an ICE.

```
error: expected expression, found `}`
 --> 44021.rs:4:1
  |
4 | }
  | ^

error: internal compiler error: unexpected panic
```

Even worse, on current stable(1.20.0), the compiler falls into an infinite loop.

This pull request fixes this problem. Now the compiler emits correct error message and span, and does not ICE.

```
error: expected one of `:`, `@`, or `|`, found `}`
 --> 44021.rs:3:20
  |
3 |     fn foo() {|x, y}
  |                    ^ expected one of `:`, `@`, or `|` here
```
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Sep 10, 2017
Expect pipe symbol after closure parameter list

Fixes rust-lang#44021.

---

Originally, the parser just called `bump` to discard following token after parsing closure parameter list, because it assumes `|` is following. However, the following code breaks the assumption:

```rust
struct MyStruct;
impl MyStruct {
   fn f() {|x, y}
}
```

Here, the parameter list is `x, y` and the following token is `}`. The parser discards `}`, and then we have a curly bracket mismatch.

Indeed, this code has a syntax error. On current nightly, the compiler emits an syntax error, but with incorrect message and span, followed by an ICE.

```
error: expected expression, found `}`
 --> 44021.rs:4:1
  |
4 | }
  | ^

error: internal compiler error: unexpected panic
```

Even worse, on current stable(1.20.0), the compiler falls into an infinite loop.

This pull request fixes this problem. Now the compiler emits correct error message and span, and does not ICE.

```
error: expected one of `:`, `@`, or `|`, found `}`
 --> 44021.rs:3:20
  |
3 |     fn foo() {|x, y}
  |                    ^ expected one of `:`, `@`, or `|` here
```
bors added a commit that referenced this pull request Sep 10, 2017
Rollup of 13 pull requests

- Successful merges: #44262, #44329, #44332, #44347, #44372, #44384, #44387, #44396, #44449, #44451, #44457, #44464, #44467
- Failed merges:
@bors bors merged commit 258ec30 into rust-lang:master Sep 10, 2017
bors added a commit that referenced this pull request Sep 14, 2017
Parse nested closure with two consecutive parameter lists properly

This is a followup of #44332.

---

Currently, in nightly, this does not compile:

```rust
fn main() {
    let f = |_||x, y| x+y;
    println!("{}", f(())(1, 2)); // should print 3
}
```

`|_||x, y| x+y` should be parsed as `|_| (|x, y| x+y)`, but the parser didn't accept `||` between `_` and `x`. This patch fixes the problem.

r? @petrochenkov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
7 participants