Skip to content

Commit d02ed0a

Browse files
authored
Rollup merge of #108542 - bwmf2:expanded, r=wesleywiser
Force parentheses around `match` expression in binary expression This attempts to solve #98790.
2 parents 4bb0b2b + 219195f commit d02ed0a

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

compiler/rustc_ast_pretty/src/pprust/state/expr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,10 @@ impl<'a> State<'a> {
244244
(&ast::ExprKind::Let { .. }, _) if !parser::needs_par_as_let_scrutinee(prec) => {
245245
parser::PREC_FORCE_PAREN
246246
}
247+
// For a binary expression like `(match () { _ => a }) OP b`, the parens are required
248+
// otherwise the parser would interpret `match () { _ => a }` as a statement,
249+
// with the remaining `OP b` not making sense. So we force parens.
250+
(&ast::ExprKind::Match(..), _) => parser::PREC_FORCE_PAREN,
247251
_ => left_prec,
248252
};
249253

tests/ui/macros/issue-98790.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// run-pass
2+
3+
macro_rules! stringify_item {
4+
($item:item) => {
5+
stringify!($item)
6+
};
7+
}
8+
9+
macro_rules! repro {
10+
($expr:expr) => {
11+
stringify_item! {
12+
pub fn repro() -> bool {
13+
$expr
14+
}
15+
}
16+
};
17+
}
18+
19+
fn main() {
20+
assert_eq!(
21+
repro!(match () { () => true } | true),
22+
"pub fn repro() -> bool { (match () { () => true, }) | true }"
23+
);
24+
}

tests/ui/macros/rfc-2011-nicer-assert-messages/all-expr-kinds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ fn main() {
164164
// mac call
165165

166166
// match
167-
[ match elem { _ => elem } == 3 ] => "Assertion failed: match elem { _ => elem, } == 3"
167+
[ match elem { _ => elem } == 3 ] => "Assertion failed: (match elem { _ => elem, }) == 3"
168168

169169
// ret
170170
[ (|| { return elem; })() == 3 ] => "Assertion failed: (|| { return elem; })() == 3"

0 commit comments

Comments
 (0)