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

Add a note to the ArbitraryExpressionInPattern error #124488

Merged
merged 2 commits into from
Apr 30, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ast_lowering_abi_specified_multiple_times =

ast_lowering_arbitrary_expression_in_pattern =
arbitrary expressions aren't allowed in patterns
.pattern_from_macro_note = the `expr` fragment specifier forces the metavariable's content to be an expression

ast_lowering_argument = argument

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_ast_lowering/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@ pub struct NeverPatternWithGuard {
pub struct ArbitraryExpressionInPattern {
#[primary_span]
pub span: Span,
#[note(ast_lowering_pattern_from_macro_note)]
pub pattern_from_macro_note: bool,
}

#[derive(Diagnostic)]
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
ExprKind::Path(..) if allow_paths => {}
ExprKind::Unary(UnOp::Neg, inner) if matches!(inner.kind, ExprKind::Lit(_)) => {}
_ => {
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern { span: expr.span });
let pattern_from_macro = expr.is_approximately_pattern();
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern {
span: expr.span,
pattern_from_macro_note: pattern_from_macro,
});
return self.arena.alloc(self.expr_err(expr.span, guar));
}
}
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/issues/issue-43250.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ error: arbitrary expressions aren't allowed in patterns
|
LL | m!(y);
| ^
|
= note: the `expr` fragment specifier forces the metavariable's content to be an expression

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

error: aborting due to 2 previous errors

11 changes: 11 additions & 0 deletions tests/ui/lowering/expr-in-pat-issue-99380.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
macro_rules! foo {
($p:expr) => {
if let $p = Some(42) {
return;
}
};
}

fn main() {
foo!(Some(3)); //~ ERROR arbitrary expressions aren't allowed in patterns
}
10 changes: 10 additions & 0 deletions tests/ui/lowering/expr-in-pat-issue-99380.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: arbitrary expressions aren't allowed in patterns
--> $DIR/expr-in-pat-issue-99380.rs:10: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

1 change: 1 addition & 0 deletions tests/ui/macros/vec-macro-in-pattern.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ error: arbitrary expressions aren't allowed in patterns
LL | Some(vec![43]) => {}
| ^^^^^^^^
|
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 1 previous error
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/match/expr_before_ident_pat.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ error: arbitrary expressions aren't allowed in patterns
|
LL | funny!(a, a);
| ^
|
= note: the `expr` fragment specifier forces the metavariable's content to be an expression

error: aborting due to 2 previous errors

Expand Down
3 changes: 3 additions & 0 deletions tests/ui/pattern/issue-92074-macro-ice.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ LL | () => { force_expr!(Vec::new()) }
LL | assert!(matches!(x, En::A(make_vec!())));
| ----------- in this macro invocation
|
= note: the `expr` fragment specifier forces the metavariable's content to be an expression
= note: this error originates in the macro `make_vec` (in Nightly builds, run with -Z macro-backtrace for more info)

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

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

error: aborting due to 3 previous errors
Expand Down