Skip to content

Commit 2f46855

Browse files
committed
Fix pattern macro note detection for bit-or expr
Add bit-or detection in lower expr within pattern. Previously `pat1 | pat2` in `$e:expr` failed to trigger `.pattern_from_macro_note` diagnostic bc `ast::Expr::is_approximately_pattern()` did not cover bit-or.
1 parent 8a70352 commit 2f46855

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,11 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
418418
}
419419
_ => {
420420
let is_const_block = matches!(expr.kind, ExprKind::ConstBlock(_));
421-
let pattern_from_macro = expr.is_approximately_pattern();
421+
let pattern_from_macro = expr.is_approximately_pattern()
422+
|| matches!(
423+
expr.peel_parens().kind,
424+
ExprKind::Binary(Spanned { node: BinOpKind::BitOr, .. }, ..)
425+
);
422426
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern {
423427
span,
424428
pattern_from_macro_note: pattern_from_macro,

tests/ui/lowering/expr-in-pat-issue-99380.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ macro_rules! foo {
66
};
77
}
88

9+
macro_rules! custom_matches {
10+
($e:expr, $p:expr) => {
11+
match $e {
12+
$p => true,
13+
_ => false,
14+
}
15+
};
16+
}
17+
918
fn main() {
1019
foo!(Some(3)); //~ ERROR arbitrary expressions aren't allowed in patterns
20+
21+
let _ = custom_matches!(67, 6 | 7); //~ ERROR arbitrary expressions aren't allowed in patterns
1122
}
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
error: arbitrary expressions aren't allowed in patterns
2-
--> $DIR/expr-in-pat-issue-99380.rs:10:10
2+
--> $DIR/expr-in-pat-issue-99380.rs:19:10
33
|
44
LL | foo!(Some(3));
55
| ^^^^^^^
66
|
77
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
88

9-
error: aborting due to 1 previous error
9+
error: arbitrary expressions aren't allowed in patterns
10+
--> $DIR/expr-in-pat-issue-99380.rs:21:33
11+
|
12+
LL | let _ = custom_matches!(67, 6 | 7);
13+
| ^^^^^
14+
|
15+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
16+
17+
error: aborting due to 2 previous errors
1018

0 commit comments

Comments
 (0)