diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-02-25 14:14:40 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-25 14:14:40 +0100 |
| commit | 731cd3fbeb0e08a44d52c14039fe942c74491f0d (patch) | |
| tree | 603794c02d501f40e703078f1c3335bd54e5706d /compiler/rustc_parse/src/parser | |
| parent | cf3bb098881da40eed6ce7ad913a7e5d904663e2 (diff) | |
| parent | fd35770e8d092b79eef16a7061e202307f730c90 (diff) | |
| download | rust-731cd3fbeb0e08a44d52c14039fe942c74491f0d.tar.gz rust-731cd3fbeb0e08a44d52c14039fe942c74491f0d.zip | |
Rollup merge of #94344 - notriddle:notriddle/suggest-parens-more, r=oli-obk
diagnostic: suggest parens when users want logical ops, but get closures Fixes #93536
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index c6919779ffd..a11cb3f5677 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -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 @@ -1247,7 +1254,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() { |
