-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Detect ruby-style closure in parser #116645
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
tests/ui/expr/malformed_closure/missing_block_in_fn_call.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// run-rustfix | ||
fn main() { | ||
let _ = vec![1, 2, 3].into_iter().map(|x| { | ||
let y = x; //~ ERROR expected expression, found `let` statement | ||
y | ||
}); | ||
let _: () = foo(); //~ ERROR mismatched types | ||
} | ||
|
||
fn foo() {} |
10 changes: 10 additions & 0 deletions
10
tests/ui/expr/malformed_closure/missing_block_in_fn_call.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// run-rustfix | ||
fn main() { | ||
let _ = vec![1, 2, 3].into_iter().map(|x| | ||
let y = x; //~ ERROR expected expression, found `let` statement | ||
y | ||
); | ||
let _: () = foo; //~ ERROR mismatched types | ||
} | ||
|
||
fn foo() {} |
38 changes: 38 additions & 0 deletions
38
tests/ui/expr/malformed_closure/missing_block_in_fn_call.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
error: expected expression, found `let` statement | ||
--> $DIR/missing_block_in_fn_call.rs:4:9 | ||
| | ||
LL | let _ = vec![1, 2, 3].into_iter().map(|x| | ||
| --- while parsing the body of this closure | ||
LL | let y = x; | ||
| ^^^ | ||
| | ||
= note: only supported directly in conditions of `if` and `while` expressions | ||
help: you might have meant to open the body of the closure | ||
| | ||
LL ~ let _ = vec![1, 2, 3].into_iter().map(|x| { | ||
LL | let y = x; | ||
LL | y | ||
LL ~ }); | ||
| | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/missing_block_in_fn_call.rs:7:17 | ||
| | ||
LL | let _: () = foo; | ||
| -- ^^^ expected `()`, found fn item | ||
| | | ||
| expected due to this | ||
... | ||
LL | fn foo() {} | ||
| -------- function `foo` defined here | ||
| | ||
= note: expected unit type `()` | ||
found fn item `fn() {foo}` | ||
help: use parentheses to call this function | ||
| | ||
LL | let _: () = foo(); | ||
| ++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
6 changes: 6 additions & 0 deletions
6
tests/ui/expr/malformed_closure/missing_block_in_let_binding.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
fn main() { | ||
let x = |x| | ||
let y = x; //~ ERROR expected expression, found `let` statement | ||
let _ = () + (); | ||
y | ||
} |
16 changes: 16 additions & 0 deletions
16
tests/ui/expr/malformed_closure/missing_block_in_let_binding.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error: expected expression, found `let` statement | ||
--> $DIR/missing_block_in_let_binding.rs:3:9 | ||
| | ||
LL | let x = |x| | ||
| --- while parsing the body of this closure | ||
LL | let y = x; | ||
| ^^^ | ||
| | ||
= note: only supported directly in conditions of `if` and `while` expressions | ||
help: you might have meant to open the body of the closure | ||
| | ||
LL | let x = |x| { | ||
| + | ||
|
||
error: aborting due to previous error | ||
|
9 changes: 9 additions & 0 deletions
9
tests/ui/expr/malformed_closure/ruby_style_closure_parse_error.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
fn main() { | ||
let _ = vec![1, 2, 3].into_iter().map(|x| { | ||
let y = x; //~ ERROR expected expression, found `let` statement | ||
y | ||
}); | ||
let _: () = foo(); //~ ERROR mismatched types | ||
} | ||
fn foo() {} |
9 changes: 9 additions & 0 deletions
9
tests/ui/expr/malformed_closure/ruby_style_closure_parse_error.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// run-rustfix | ||
fn main() { | ||
let _ = vec![1, 2, 3].into_iter().map({|x| | ||
let y = x; //~ ERROR expected expression, found `let` statement | ||
y | ||
}); | ||
let _: () = foo; //~ ERROR mismatched types | ||
} | ||
fn foo() {} |
36 changes: 36 additions & 0 deletions
36
tests/ui/expr/malformed_closure/ruby_style_closure_parse_error.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
error: expected expression, found `let` statement | ||
--> $DIR/ruby_style_closure_parse_error.rs:4:9 | ||
| | ||
LL | let _ = vec![1, 2, 3].into_iter().map({|x| | ||
| --- while parsing the body of this closure | ||
LL | let y = x; | ||
| ^^^ | ||
| | ||
= note: only supported directly in conditions of `if` and `while` expressions | ||
help: you might have meant to open the body of the closure, instead of enclosing the closure in a block | ||
| | ||
LL - let _ = vec![1, 2, 3].into_iter().map({|x| | ||
LL + let _ = vec![1, 2, 3].into_iter().map(|x| { | ||
| | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/ruby_style_closure_parse_error.rs:7:17 | ||
| | ||
LL | let _: () = foo; | ||
| -- ^^^ expected `()`, found fn item | ||
| | | ||
| expected due to this | ||
LL | } | ||
LL | fn foo() {} | ||
| -------- function `foo` defined here | ||
| | ||
= note: expected unit type `()` | ||
found fn item `fn() {foo}` | ||
help: use parentheses to call this function | ||
| | ||
LL | let _: () = foo(); | ||
| ++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub fn test() { | ||
foo(|_|) //~ ERROR expected expression, found `)` | ||
//~^ ERROR cannot find function `foo` in this scope | ||
} | ||
|
||
fn main() { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a false positive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I saw this, but I have no clue how we're meant to even start to try to parse such code in practice 😅
I feel it's fine, it still gives people some sense of what's happening.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea... idk. I just feel like the suggestion doesn't help. But maybe it's better than without it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can always special case it, but the previous error wasn't much better, tbqh