Skip to content

Commit

Permalink
fixup! Add suggestions for expressions in patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
ShE3py committed Jul 24, 2024
1 parent d9d985b commit a604d6f
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 223 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_parse/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -791,19 +791,19 @@ parse_unexpected_expr_in_pat =
.label = arbitrary expressions are not allowed in patterns
parse_unexpected_expr_in_pat_const_sugg = extract the expression into a `const` and refer to it
parse_unexpected_expr_in_pat_const_sugg = consider extracting the expression into a `const`
parse_unexpected_expr_in_pat_create_guard_sugg = check the value in an arm guard
parse_unexpected_expr_in_pat_create_guard_sugg = consider moving the expression to a match arm guard
parse_unexpected_expr_in_pat_inline_const_sugg = wrap the expression in a inline const (requires `{"#"}![feature(inline_const_pat)]`)
parse_unexpected_expr_in_pat_inline_const_sugg = consider wrapping the expression in an inline `const` (requires `{"#"}![feature(inline_const_pat)]`)
parse_unexpected_expr_in_pat_remove_let_sugg =
remove the `let` if you meant to {$has_initializer ->
[true] do an assignment
*[false] evaluate an expression
}
parse_unexpected_expr_in_pat_update_guard_sugg = check the value in the arm guard
parse_unexpected_expr_in_pat_update_guard_sugg = consider moving the expression to the match arm guard
parse_unexpected_if_with_if = unexpected `if` in the condition expression
.suggestion = remove the `if`
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_parse/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2630,7 +2630,8 @@ pub(crate) enum UnexpectedExpressionInPatternSugg {
#[suggestion_part(code = "{ident}")]
ident_span: Span,
/// The end of the match arm's pattern.
#[suggestion_part(code = " if {ident} == {expr}")]
// FIXME: only insert `()` if needed
#[suggestion_part(code = " if {ident} == ({expr})")]
pat_hi: Span,
/// The suggested identifier.
ident: String,
Expand All @@ -2650,7 +2651,8 @@ pub(crate) enum UnexpectedExpressionInPatternSugg {
#[suggestion_part(code = "(")]
guard_lo: Span,
/// The end of the match arm guard's expression.
#[suggestion_part(code = ") && {ident} == {expr}")]
// FIXME: only insert `()` if needed
#[suggestion_part(code = ") && {ident} == ({expr})")]
guard_hi: Span,
/// The suggested identifier.
ident: String,
Expand All @@ -2664,7 +2666,7 @@ pub(crate) enum UnexpectedExpressionInPatternSugg {
)]
Const {
/// The beginning of statement's line.
#[suggestion_part(code = "{indentation}const {ident}: _ = {expr};\n")]
#[suggestion_part(code = "{indentation}const {ident}: /* Type */ = {expr};\n")]
stmt_lo: Span,
/// The span of the `PatKind:Err` to be transformed into a `PatKind::Ident`.
#[suggestion_part(code = "{ident}")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ error: expected a pattern range bound, found an expression
LL | 0..5+1 => errors_only.push(x),
| ^^^ arbitrary expressions are not allowed in patterns
|
help: extract the expression into a `const` and refer to it
help: consider extracting the expression into a `const`
|
LL + const VAL: _ = 5+1;
LL + const VAL: /* Type */ = 5+1;
LL ~ match x as i32 {
LL ~ 0..VAL => errors_only.push(x),
|
help: wrap the expression in a inline const (requires `#![feature(inline_const_pat)]`)
help: consider wrapping the expression in an inline `const` (requires `#![feature(inline_const_pat)]`)
|
LL | 0..const { 5+1 } => errors_only.push(x),
| +++++++ +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ error: expected a pattern range bound, found an expression
LL | 0..=(5+1) => errors_only.push(x),
| ^^^ arbitrary expressions are not allowed in patterns
|
help: extract the expression into a `const` and refer to it
help: consider extracting the expression into a `const`
|
LL + const VAL: _ = 5+1;
LL + const VAL: /* Type */ = 5+1;
LL ~ match x as i32 {
LL ~ 0..=(VAL) => errors_only.push(x),
|
help: wrap the expression in a inline const (requires `#![feature(inline_const_pat)]`)
help: consider wrapping the expression in an inline `const` (requires `#![feature(inline_const_pat)]`)
|
LL | 0..=(const { 5+1 }) => errors_only.push(x),
| +++++++ +
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/parser/issues/issue-24375.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ error: expected a pattern, found an expression
LL | tmp[0] => {}
| ^^^^^^ arbitrary expressions are not allowed in patterns
|
help: check the value in an arm guard
help: consider moving the expression to a match arm guard
|
LL | val if val == tmp[0] => {}
| ~~~ ++++++++++++++++
help: extract the expression into a `const` and refer to it
LL | val if val == (tmp[0]) => {}
| ~~~ ++++++++++++++++++
help: consider extracting the expression into a `const`
|
LL + const VAL: _ = tmp[0];
LL + const VAL: /* Type */ = tmp[0];
LL ~ match z {
LL ~ VAL => {}
|
help: wrap the expression in a inline const (requires `#![feature(inline_const_pat)]`)
help: consider wrapping the expression in an inline `const` (requires `#![feature(inline_const_pat)]`)
|
LL | const { tmp[0] } => {}
| +++++++ +
Expand Down
Loading

0 comments on commit a604d6f

Please sign in to comment.