Skip to content

Commit 12c7a3f

Browse files
authored
Unrolled build for #154369
Rollup merge of #154369 - ver-nyan:fix-bit_or-pat_macro-msg, r=JohnTitor Fix `pattern_from_macro_note` for bit-or expr This is a continuation of issue #99380 (and pr #124488) but covers bit-or pattern. Essentially a `pat1 | pat2`, or any bit-or pattern used in a `$e:expr` was not firing the `.pattern_from_macro_note` diagnostic bc `ast::Expr::is_approximately_pattern()` did not cover bit-or. The cover for bit-or is only added in `lower_expr_within_pat()`, otherwise doing it in `is_approximately_pattern()` would result in the suggestion `if (i + 2) = 2 => if let (i + 2) = 2` from `suggest_pattern_match_with_let()` (which is the only other place `ast::Expr::is_approximately_pattern()` is used from what i see). resolves #99380 refs #124488
2 parents 8317fef + 2f46855 commit 12c7a3f

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

‎compiler/rustc_ast_lowering/src/pat.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
421421
}
422422
_ => {
423423
let is_const_block = matches!(expr.kind, ExprKind::ConstBlock(_));
424-
let pattern_from_macro = expr.is_approximately_pattern();
424+
let pattern_from_macro = expr.is_approximately_pattern()
425+
|| matches!(
426+
expr.peel_parens().kind,
427+
ExprKind::Binary(Spanned { node: BinOpKind::BitOr, .. }, ..)
428+
);
425429
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern {
426430
span,
427431
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)