Skip to content
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

Force parentheses around match expression in binary expression #108542

Merged
merged 3 commits into from
Mar 11, 2023
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
4 changes: 4 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
bwmf2 marked this conversation as resolved.
Show resolved Hide resolved
_ => left_prec,
};

Expand Down
24 changes: 24 additions & 0 deletions tests/ui/macros/issue-98790.rs
Original file line number Diff line number Diff line change
@@ -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 }"
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down