Skip to content

Commit a604d6f

Browse files
committed
fixup! Add suggestions for expressions in patterns
1 parent d9d985b commit a604d6f

10 files changed

+225
-223
lines changed

compiler/rustc_parse/messages.ftl

+4-4
Original file line numberDiff line numberDiff line change
@@ -791,19 +791,19 @@ parse_unexpected_expr_in_pat =
791791
792792
.label = arbitrary expressions are not allowed in patterns
793793
794-
parse_unexpected_expr_in_pat_const_sugg = extract the expression into a `const` and refer to it
794+
parse_unexpected_expr_in_pat_const_sugg = consider extracting the expression into a `const`
795795
796-
parse_unexpected_expr_in_pat_create_guard_sugg = check the value in an arm guard
796+
parse_unexpected_expr_in_pat_create_guard_sugg = consider moving the expression to a match arm guard
797797
798-
parse_unexpected_expr_in_pat_inline_const_sugg = wrap the expression in a inline const (requires `{"#"}![feature(inline_const_pat)]`)
798+
parse_unexpected_expr_in_pat_inline_const_sugg = consider wrapping the expression in an inline `const` (requires `{"#"}![feature(inline_const_pat)]`)
799799
800800
parse_unexpected_expr_in_pat_remove_let_sugg =
801801
remove the `let` if you meant to {$has_initializer ->
802802
[true] do an assignment
803803
*[false] evaluate an expression
804804
}
805805
806-
parse_unexpected_expr_in_pat_update_guard_sugg = check the value in the arm guard
806+
parse_unexpected_expr_in_pat_update_guard_sugg = consider moving the expression to the match arm guard
807807
808808
parse_unexpected_if_with_if = unexpected `if` in the condition expression
809809
.suggestion = remove the `if`

compiler/rustc_parse/src/errors.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2630,7 +2630,8 @@ pub(crate) enum UnexpectedExpressionInPatternSugg {
26302630
#[suggestion_part(code = "{ident}")]
26312631
ident_span: Span,
26322632
/// The end of the match arm's pattern.
2633-
#[suggestion_part(code = " if {ident} == {expr}")]
2633+
// FIXME: only insert `()` if needed
2634+
#[suggestion_part(code = " if {ident} == ({expr})")]
26342635
pat_hi: Span,
26352636
/// The suggested identifier.
26362637
ident: String,
@@ -2650,7 +2651,8 @@ pub(crate) enum UnexpectedExpressionInPatternSugg {
26502651
#[suggestion_part(code = "(")]
26512652
guard_lo: Span,
26522653
/// The end of the match arm guard's expression.
2653-
#[suggestion_part(code = ") && {ident} == {expr}")]
2654+
// FIXME: only insert `()` if needed
2655+
#[suggestion_part(code = ") && {ident} == ({expr})")]
26542656
guard_hi: Span,
26552657
/// The suggested identifier.
26562658
ident: String,
@@ -2664,7 +2666,7 @@ pub(crate) enum UnexpectedExpressionInPatternSugg {
26642666
)]
26652667
Const {
26662668
/// The beginning of statement's line.
2667-
#[suggestion_part(code = "{indentation}const {ident}: _ = {expr};\n")]
2669+
#[suggestion_part(code = "{indentation}const {ident}: /* Type */ = {expr};\n")]
26682670
stmt_lo: Span,
26692671
/// The span of the `PatKind:Err` to be transformed into a `PatKind::Ident`.
26702672
#[suggestion_part(code = "{ident}")]

tests/ui/half-open-range-patterns/range_pat_interactions1.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ error: expected a pattern range bound, found an expression
44
LL | 0..5+1 => errors_only.push(x),
55
| ^^^ arbitrary expressions are not allowed in patterns
66
|
7-
help: extract the expression into a `const` and refer to it
7+
help: consider extracting the expression into a `const`
88
|
9-
LL + const VAL: _ = 5+1;
9+
LL + const VAL: /* Type */ = 5+1;
1010
LL ~ match x as i32 {
1111
LL ~ 0..VAL => errors_only.push(x),
1212
|
13-
help: wrap the expression in a inline const (requires `#![feature(inline_const_pat)]`)
13+
help: consider wrapping the expression in an inline `const` (requires `#![feature(inline_const_pat)]`)
1414
|
1515
LL | 0..const { 5+1 } => errors_only.push(x),
1616
| +++++++ +

tests/ui/half-open-range-patterns/range_pat_interactions2.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ error: expected a pattern range bound, found an expression
1616
LL | 0..=(5+1) => errors_only.push(x),
1717
| ^^^ arbitrary expressions are not allowed in patterns
1818
|
19-
help: extract the expression into a `const` and refer to it
19+
help: consider extracting the expression into a `const`
2020
|
21-
LL + const VAL: _ = 5+1;
21+
LL + const VAL: /* Type */ = 5+1;
2222
LL ~ match x as i32 {
2323
LL ~ 0..=(VAL) => errors_only.push(x),
2424
|
25-
help: wrap the expression in a inline const (requires `#![feature(inline_const_pat)]`)
25+
help: consider wrapping the expression in an inline `const` (requires `#![feature(inline_const_pat)]`)
2626
|
2727
LL | 0..=(const { 5+1 }) => errors_only.push(x),
2828
| +++++++ +

tests/ui/parser/issues/issue-24375.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ error: expected a pattern, found an expression
44
LL | tmp[0] => {}
55
| ^^^^^^ arbitrary expressions are not allowed in patterns
66
|
7-
help: check the value in an arm guard
7+
help: consider moving the expression to a match arm guard
88
|
9-
LL | val if val == tmp[0] => {}
10-
| ~~~ ++++++++++++++++
11-
help: extract the expression into a `const` and refer to it
9+
LL | val if val == (tmp[0]) => {}
10+
| ~~~ ++++++++++++++++++
11+
help: consider extracting the expression into a `const`
1212
|
13-
LL + const VAL: _ = tmp[0];
13+
LL + const VAL: /* Type */ = tmp[0];
1414
LL ~ match z {
1515
LL ~ VAL => {}
1616
|
17-
help: wrap the expression in a inline const (requires `#![feature(inline_const_pat)]`)
17+
help: consider wrapping the expression in an inline `const` (requires `#![feature(inline_const_pat)]`)
1818
|
1919
LL | const { tmp[0] } => {}
2020
| +++++++ +

0 commit comments

Comments
 (0)