diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index aece4552dad4d..24bd53088e21d 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -418,7 +418,11 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> { } _ => { let is_const_block = matches!(expr.kind, ExprKind::ConstBlock(_)); - let pattern_from_macro = expr.is_approximately_pattern(); + let pattern_from_macro = expr.is_approximately_pattern() + || matches!( + expr.peel_parens().kind, + ExprKind::Binary(Spanned { node: BinOpKind::BitOr, .. }, ..) + ); let guar = self.dcx().emit_err(ArbitraryExpressionInPattern { span, pattern_from_macro_note: pattern_from_macro, diff --git a/tests/ui/lowering/expr-in-pat-issue-99380.rs b/tests/ui/lowering/expr-in-pat-issue-99380.rs index 1d4a047f717f7..9d1d369dbd69d 100644 --- a/tests/ui/lowering/expr-in-pat-issue-99380.rs +++ b/tests/ui/lowering/expr-in-pat-issue-99380.rs @@ -6,6 +6,17 @@ macro_rules! foo { }; } +macro_rules! custom_matches { + ($e:expr, $p:expr) => { + match $e { + $p => true, + _ => false, + } + }; +} + fn main() { foo!(Some(3)); //~ ERROR arbitrary expressions aren't allowed in patterns + + let _ = custom_matches!(67, 6 | 7); //~ ERROR arbitrary expressions aren't allowed in patterns } diff --git a/tests/ui/lowering/expr-in-pat-issue-99380.stderr b/tests/ui/lowering/expr-in-pat-issue-99380.stderr index 29438c9b06360..8b48e3b0150a2 100644 --- a/tests/ui/lowering/expr-in-pat-issue-99380.stderr +++ b/tests/ui/lowering/expr-in-pat-issue-99380.stderr @@ -1,10 +1,18 @@ error: arbitrary expressions aren't allowed in patterns - --> $DIR/expr-in-pat-issue-99380.rs:10:10 + --> $DIR/expr-in-pat-issue-99380.rs:19:10 | LL | foo!(Some(3)); | ^^^^^^^ | = note: the `expr` fragment specifier forces the metavariable's content to be an expression -error: aborting due to 1 previous error +error: arbitrary expressions aren't allowed in patterns + --> $DIR/expr-in-pat-issue-99380.rs:21:33 + | +LL | let _ = custom_matches!(67, 6 | 7); + | ^^^^^ + | + = note: the `expr` fragment specifier forces the metavariable's content to be an expression + +error: aborting due to 2 previous errors