From 832b33e6b1e0359130b67f0c83f2def0420a6832 Mon Sep 17 00:00:00 2001 From: bwmf2 Date: Sun, 26 Feb 2023 23:22:50 +0100 Subject: [PATCH 1/3] Force parentheses around `match` expression in binary expression --- compiler/rustc_ast_pretty/src/pprust/state/expr.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs index cacfe9eb2f107..a4d91a9566273 100644 --- a/compiler/rustc_ast_pretty/src/pprust/state/expr.rs +++ b/compiler/rustc_ast_pretty/src/pprust/state/expr.rs @@ -244,6 +244,10 @@ impl<'a> State<'a> { (&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(prec) => { parser::PREC_FORCE_PAREN } + // For a binary expression like `(match () { _ => a }) OP b`, the parens are required + // otherwise the parser would interpret `match () { _ => a }` as a statement, + // with the remaining `OP b` not making sense. So we force parens. + (&ast::ExprKind::Match(..), _) => parser::PREC_FORCE_PAREN, _ => left_prec, }; From cdeb0e3e02cc70ca4efb41fa67b55b5722a81121 Mon Sep 17 00:00:00 2001 From: bwmf2 Date: Tue, 28 Feb 2023 06:04:56 +0100 Subject: [PATCH 2/3] Fix UI test --- .../ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs b/tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs index b8b6f0846bb44..39ba1714cc70b 100644 --- a/tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs +++ b/tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs @@ -164,7 +164,7 @@ fn main() { // mac call // match - [ match elem { _ => elem } == 3 ] => "Assertion failed: match elem { _ => elem, } == 3" + [ match elem { _ => elem } == 3 ] => "Assertion failed: (match elem { _ => elem, }) == 3" // ret [ (|| { return elem; })() == 3 ] => "Assertion failed: (|| { return elem; })() == 3" From 219195fc4c98649785154e2c66075be716bc0c01 Mon Sep 17 00:00:00 2001 From: bwmf2 Date: Tue, 28 Feb 2023 06:05:45 +0100 Subject: [PATCH 3/3] Add UI test --- tests/ui/macros/issue-98790.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 tests/ui/macros/issue-98790.rs diff --git a/tests/ui/macros/issue-98790.rs b/tests/ui/macros/issue-98790.rs new file mode 100644 index 0000000000000..8fe6fc41d10b7 --- /dev/null +++ b/tests/ui/macros/issue-98790.rs @@ -0,0 +1,24 @@ +// run-pass + +macro_rules! stringify_item { + ($item:item) => { + stringify!($item) + }; +} + +macro_rules! repro { + ($expr:expr) => { + stringify_item! { + pub fn repro() -> bool { + $expr + } + } + }; +} + +fn main() { + assert_eq!( + repro!(match () { () => true } | true), + "pub fn repro() -> bool { (match () { () => true, }) | true }" + ); +}