Skip to content

Commit 42ab090

Browse files
authored
Rollup merge of #124488 - est31:arbitrary_expressions_error, r=pnkfelix
Add a note to the ArbitraryExpressionInPattern error The current "arbitrary expressions aren't allowed in patterns" error is confusing, as it fires for code where it *looks* like a pattern but the compiler still treats it as an expression. That this is due to the `:expr` fragment specifier forcing the expression-ness property on the code. In the test suite, the "arbitrary expressions aren't allowed in patterns" error can only be found in combination with macro_rules macros that force expression-ness of their content, namely via `:expr` metavariables. I also can't come up with cases where there would be an expression instead of a pattern, so I think it's always coming from an `:expr`. In order to make the error less confusing, this adds a note explaining the weird `:expr` fragment behaviour. Fixes #99380
2 parents 6ce9708 + c6e946d commit 42ab090

File tree

9 files changed

+39
-1
lines changed

9 files changed

+39
-1
lines changed

compiler/rustc_ast_lowering/messages.ftl

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ast_lowering_abi_specified_multiple_times =
55
66
ast_lowering_arbitrary_expression_in_pattern =
77
arbitrary expressions aren't allowed in patterns
8+
.pattern_from_macro_note = the `expr` fragment specifier forces the metavariable's content to be an expression
89
910
ast_lowering_argument = argument
1011

compiler/rustc_ast_lowering/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ pub struct NeverPatternWithGuard {
368368
pub struct ArbitraryExpressionInPattern {
369369
#[primary_span]
370370
pub span: Span,
371+
#[note(ast_lowering_pattern_from_macro_note)]
372+
pub pattern_from_macro_note: bool,
371373
}
372374

373375
#[derive(Diagnostic)]

compiler/rustc_ast_lowering/src/pat.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
339339
ExprKind::Path(..) if allow_paths => {}
340340
ExprKind::Unary(UnOp::Neg, inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
341341
_ => {
342-
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern { span: expr.span });
342+
let pattern_from_macro = expr.is_approximately_pattern();
343+
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern {
344+
span: expr.span,
345+
pattern_from_macro_note: pattern_from_macro,
346+
});
343347
return self.arena.alloc(self.expr_err(expr.span, guar));
344348
}
345349
}

tests/ui/issues/issue-43250.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ error: arbitrary expressions aren't allowed in patterns
33
|
44
LL | m!(y);
55
| ^
6+
|
7+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
68

79
error: arbitrary expressions aren't allowed in patterns
810
--> $DIR/issue-43250.rs:11:8
911
|
1012
LL | m!(C);
1113
| ^
14+
|
15+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
1216

1317
error: aborting due to 2 previous errors
1418

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
macro_rules! foo {
2+
($p:expr) => {
3+
if let $p = Some(42) {
4+
return;
5+
}
6+
};
7+
}
8+
9+
fn main() {
10+
foo!(Some(3)); //~ ERROR arbitrary expressions aren't allowed in patterns
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: arbitrary expressions aren't allowed in patterns
2+
--> $DIR/expr-in-pat-issue-99380.rs:10:10
3+
|
4+
LL | foo!(Some(3));
5+
| ^^^^^^^
6+
|
7+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
8+
9+
error: aborting due to 1 previous error
10+

tests/ui/macros/vec-macro-in-pattern.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ error: arbitrary expressions aren't allowed in patterns
44
LL | Some(vec![43]) => {}
55
| ^^^^^^^^
66
|
7+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
78
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
89

910
error: aborting due to 1 previous error

tests/ui/match/expr_before_ident_pat.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ error: arbitrary expressions aren't allowed in patterns
99
|
1010
LL | funny!(a, a);
1111
| ^
12+
|
13+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
1214

1315
error: aborting due to 2 previous errors
1416

tests/ui/pattern/issue-92074-macro-ice.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ LL | () => { force_expr!(Vec::new()) }
77
LL | assert!(matches!(x, En::A(make_vec!())));
88
| ----------- in this macro invocation
99
|
10+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
1011
= note: this error originates in the macro `make_vec` (in Nightly builds, run with -Z macro-backtrace for more info)
1112

1213
error: arbitrary expressions aren't allowed in patterns
@@ -18,6 +19,7 @@ LL | () => { force_pat!(get_usize(), get_usize()) }
1819
LL | assert!(matches!(5, make_pat!()));
1920
| ----------- in this macro invocation
2021
|
22+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
2123
= note: this error originates in the macro `make_pat` (in Nightly builds, run with -Z macro-backtrace for more info)
2224

2325
error: arbitrary expressions aren't allowed in patterns
@@ -29,6 +31,7 @@ LL | () => { force_pat!(get_usize(), get_usize()) }
2931
LL | assert!(matches!(5, make_pat!()));
3032
| ----------- in this macro invocation
3133
|
34+
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
3235
= note: this error originates in the macro `make_pat` (in Nightly builds, run with -Z macro-backtrace for more info)
3336

3437
error: aborting due to 3 previous errors

0 commit comments

Comments
 (0)