-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect possibly non-Rust closure syntax during parse
Other languages have a similar, but not quite the same to Rust, syntax for closures: ``` {|<args>| <stmts> } ``` Even worse, the above code *is* valid Rust if and only if there's only one statement, as the block would resolve to the single statement closure within it. This means that seemingly correct code will fail when adding a new statement, making modifying something that looks unrelated cause "value not found in this scope" and "trait bound not satisfied" errors. Because of this, make the parser detect the specific case where the code is incorrect *and* failing, and emit a warning explaining the correct syntax.
- Loading branch information
Showing
3 changed files
with
155 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// #27300 | ||
|
||
fn main() { | ||
let _p = Some(45).and_then({|x| | ||
//~^ ERROR the trait bound `std::option::Option<_>: std::ops::FnOnce<({integer},)>` | ||
//~| NOTE the trait `std::ops::FnOnce<({integer},)>` is not implemented for | ||
println!("hi"); | ||
Some(x * 2) //~ NOTE ...implicitly returns this | ||
}); | ||
//~^^^^^^ WARN a closure's body is not determined by its enclosing block | ||
//~| NOTE this closure's body is not determined by its enclosing block | ||
//~| NOTE this is the closure's block... | ||
//~| NOTE ...while this enclosing block... | ||
//~| HELP you should open the block *after* the closure's argument list | ||
//~^^^^^^^ ERROR cannot find value `x` in this scope | ||
//~| NOTE not found in this scope | ||
|
||
// Don't alert on the folloing case, even though it is likely that the user is confused in the | ||
// same way as the first test case, as 1) clippy will warn about this and 2) if they ever | ||
// change it, the appropriate warning will trigger. | ||
let _y = Some(45).and_then({|x| | ||
Some(x * 2) | ||
}); | ||
} |
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,50 @@ | ||
warning: a closure's body is not determined by its enclosing block | ||
--> $DIR/block-enclosed-closure.rs:14:33 | ||
| | ||
14 | let _p = Some(45).and_then({|x| | ||
| ^^^ this closure's body is not determined by its enclosing block | ||
| | ||
note: this is the closure's block... | ||
--> $DIR/block-enclosed-closure.rs:14:33 | ||
| | ||
14 | let _p = Some(45).and_then({|x| | ||
| _________________________________^ | ||
15 | | //~^ ERROR the trait bound `std::option::Option<_>: std::ops::FnOnce<({integer},)>` | ||
16 | | //~| NOTE the trait `std::ops::FnOnce<({integer},)>` is not implemented for | ||
17 | | println!("hi"); | ||
| |______________________^ | ||
note: ...while this enclosing block... | ||
--> $DIR/block-enclosed-closure.rs:14:32 | ||
| | ||
14 | let _p = Some(45).and_then({|x| | ||
| ________________________________^ | ||
15 | | //~^ ERROR the trait bound `std::option::Option<_>: std::ops::FnOnce<({integer},)>` | ||
16 | | //~| NOTE the trait `std::ops::FnOnce<({integer},)>` is not implemented for | ||
17 | | println!("hi"); | ||
18 | | Some(x * 2) //~ NOTE ...implicitly returns this | ||
19 | | }); | ||
| |_____^ | ||
note: ...implicitly returns this | ||
--> $DIR/block-enclosed-closure.rs:18:9 | ||
| | ||
18 | Some(x * 2) //~ NOTE ...implicitly returns this | ||
| ^^^^^^^^^^^ | ||
help: you should open the block *after* the closure's argument list | ||
| | ||
14 | let _p = Some(45).and_then(|x| { | ||
| ^^^^^ | ||
|
||
error[E0425]: cannot find value `x` in this scope | ||
--> $DIR/block-enclosed-closure.rs:18:14 | ||
| | ||
18 | Some(x * 2) //~ NOTE ...implicitly returns this | ||
| ^ not found in this scope | ||
|
||
error[E0277]: the trait bound `std::option::Option<_>: std::ops::FnOnce<({integer},)>` is not satisfied | ||
--> $DIR/block-enclosed-closure.rs:14:23 | ||
| | ||
14 | let _p = Some(45).and_then({|x| | ||
| ^^^^^^^^ the trait `std::ops::FnOnce<({integer},)>` is not implemented for `std::option::Option<_>` | ||
|
||
error: aborting due to 2 previous errors | ||
|