Skip to content

diagnostic: suggest parens when users want logical ops, but get closures #94344

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

Merged
merged 1 commit into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 16 additions & 2 deletions compiler/rustc_parse/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,17 @@ impl<'a> Parser<'a> {
self.sess.ambiguous_block_expr_parse.borrow_mut().insert(sp, lhs.span);
false
}
(true, Some(AssocOp::LAnd)) => {
(true, Some(AssocOp::LAnd)) |
(true, Some(AssocOp::LOr)) |
(true, Some(AssocOp::BitOr)) => {
// `{ 42 } &&x` (#61475) or `{ 42 } && if x { 1 } else { 0 }`. Separated from the
// above due to #74233.
// These cases are ambiguous and can't be identified in the parser alone.
//
// Bitwise AND is left out because guessing intent is hard. We can make
// suggestions based on the assumption that double-refs are rarely intentional,
// and closures are distinct enough that they don't get mixed up with their
// return value.
let sp = self.sess.source_map().start_point(self.token.span);
self.sess.ambiguous_block_expr_parse.borrow_mut().insert(sp, lhs.span);
false
Expand Down Expand Up @@ -1244,7 +1251,14 @@ impl<'a> Parser<'a> {
} else if self.check(&token::OpenDelim(token::Brace)) {
self.parse_block_expr(None, lo, BlockCheckMode::Default, attrs)
} else if self.check(&token::BinOp(token::Or)) || self.check(&token::OrOr) {
self.parse_closure_expr(attrs)
self.parse_closure_expr(attrs).map_err(|mut err| {
// If the input is something like `if a { 1 } else { 2 } | if a { 3 } else { 4 }`
// then suggest parens around the lhs.
if let Some(sp) = self.sess.ambiguous_block_expr_parse.borrow().get(&lo) {
self.sess.expr_parentheses_needed(&mut err, *sp);
}
err
})
} else if self.check(&token::OpenDelim(token::Bracket)) {
self.parse_array_or_repeat_expr(attrs, token::Bracket)
} else if self.check_path() {
Expand Down
27 changes: 27 additions & 0 deletions src/test/ui/parser/expr-as-stmt.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,31 @@ fn qux() -> u32 {
//~^ ERROR mismatched types
}

fn space_cadet() -> bool {
({ true }) | { true } //~ ERROR E0308
//~^ ERROR expected parameter name
}

fn revenge_from_mars() -> bool {
({ true }) && { true } //~ ERROR E0308
//~^ ERROR mismatched types
}

fn attack_from_mars() -> bool {
({ true }) || { true } //~ ERROR E0308
//~^ ERROR mismatched types
}

// This gets corrected by adding a semicolon, instead of parens.
// It's placed here to help keep track of the way this diagnostic
// needs to interact with type checking to avoid MachineApplicable
// suggestions that actually break stuff.
//
// If you're wondering what happens if that `foo()` is a `true` like
// all the ones above use? Nothing. It makes neither suggestion in
// that case.
fn asteroids() -> impl FnOnce() -> bool {
{ foo(); } || { true } //~ ERROR E0308
}

fn main() {}
27 changes: 27 additions & 0 deletions src/test/ui/parser/expr-as-stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,31 @@ fn qux() -> u32 {
//~^ ERROR mismatched types
}

fn space_cadet() -> bool {
{ true } | { true } //~ ERROR E0308
//~^ ERROR expected parameter name
}

fn revenge_from_mars() -> bool {
{ true } && { true } //~ ERROR E0308
//~^ ERROR mismatched types
}

fn attack_from_mars() -> bool {
{ true } || { true } //~ ERROR E0308
//~^ ERROR mismatched types
}

// This gets corrected by adding a semicolon, instead of parens.
// It's placed here to help keep track of the way this diagnostic
// needs to interact with type checking to avoid MachineApplicable
// suggestions that actually break stuff.
//
// If you're wondering what happens if that `foo()` is a `true` like
// all the ones above use? Nothing. It makes neither suggestion in
// that case.
fn asteroids() -> impl FnOnce() -> bool {
{ foo() } || { true } //~ ERROR E0308
}

fn main() {}
82 changes: 81 additions & 1 deletion src/test/ui/parser/expr-as-stmt.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,25 @@ LL | _ => 1,
LL ~ }) > 0
|

error: expected parameter name, found `{`
--> $DIR/expr-as-stmt.rs:41:16
|
LL | { true } | { true }
| ^ expected parameter name
|
help: parentheses are required to parse this as an expression
|
LL | ({ true }) | { true }
| + +

error[E0308]: mismatched types
--> $DIR/expr-as-stmt.rs:64:7
|
LL | { foo() } || { true }
| ^^^^^- help: consider using a semicolon here: `;`
| |
| expected `()`, found `i32`

error[E0308]: mismatched types
--> $DIR/expr-as-stmt.rs:8:6
|
Expand Down Expand Up @@ -121,7 +140,68 @@ help: parentheses are required to parse this as an expression
LL | ({2}) - 2
| + +

error: aborting due to 11 previous errors
error[E0308]: mismatched types
--> $DIR/expr-as-stmt.rs:41:7
|
LL | { true } | { true }
| ^^^^ expected `()`, found `bool`
|
help: you might have meant to return this value
|
LL | { return true; } | { true }
| ++++++ +

error[E0308]: mismatched types
--> $DIR/expr-as-stmt.rs:46:7
|
LL | { true } && { true }
| ^^^^ expected `()`, found `bool`
|
help: you might have meant to return this value
|
LL | { return true; } && { true }
| ++++++ +

error[E0308]: mismatched types
--> $DIR/expr-as-stmt.rs:46:14
|
LL | fn revenge_from_mars() -> bool {
| ---- expected `bool` because of return type
LL | { true } && { true }
| ^^^^^^^^^^^ expected `bool`, found `&&bool`
|
help: parentheses are required to parse this as an expression
|
LL | ({ true }) && { true }
| + +

error[E0308]: mismatched types
--> $DIR/expr-as-stmt.rs:51:7
|
LL | { true } || { true }
| ^^^^ expected `()`, found `bool`
|
help: you might have meant to return this value
|
LL | { return true; } || { true }
| ++++++ +

error[E0308]: mismatched types
--> $DIR/expr-as-stmt.rs:51:14
|
LL | fn attack_from_mars() -> bool {
| ---- expected `bool` because of return type
LL | { true } || { true }
| ^^^^^^^^^^^ expected `bool`, found closure
|
= note: expected type `bool`
found closure `[closure@$DIR/expr-as-stmt.rs:51:14: 51:25]`
help: parentheses are required to parse this as an expression
|
LL | ({ true }) || { true }
| + +

error: aborting due to 18 previous errors

Some errors have detailed explanations: E0308, E0600, E0614.
For more information about an error, try `rustc --explain E0308`.