Skip to content

Commit 005c6af

Browse files
committed
Add expr in pat recovery suggestions
1 parent 29fe4b5 commit 005c6af

12 files changed

+95
-10
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,10 @@ parse_unexpected_expr_in_pat =
786786
*[false] arbitrary expressions
787787
} are not allowed in patterns
788788
789+
.help = extract the expression into a `const` and refer to it
790+
791+
parse_unexpected_expr_in_pat_const_pat_sugg = wrap the expression in a inline const (requires `{"#"}![feature(inline_const)]`)
792+
789793
parse_unexpected_if_with_if = unexpected `if` in the condition expression
790794
.suggestion = remove the `if`
791795

compiler/rustc_parse/src/errors.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2433,6 +2433,7 @@ pub(crate) struct ExpectedCommaAfterPatternField {
24332433

24342434
#[derive(Diagnostic)]
24352435
#[diag(parse_unexpected_expr_in_pat)]
2436+
#[help]
24362437
pub(crate) struct UnexpectedExpressionInPattern {
24372438
#[primary_span]
24382439
#[label]
@@ -2443,6 +2444,18 @@ pub(crate) struct UnexpectedExpressionInPattern {
24432444
pub is_method_call: bool,
24442445
}
24452446

2447+
#[derive(Subdiagnostic)]
2448+
#[multipart_suggestion(
2449+
parse_unexpected_expr_in_pat_const_pat_sugg,
2450+
applicability = "machine-applicable"
2451+
)]
2452+
pub(crate) struct UnexpectedExpressionInPatternConstPatSugg {
2453+
#[suggestion_part(code = "const {{ ")]
2454+
pub start_span: Span,
2455+
#[suggestion_part(code = " }}")]
2456+
pub end_span: Span,
2457+
}
2458+
24462459
#[derive(Diagnostic)]
24472460
#[diag(parse_unexpected_paren_in_range_pat)]
24482461
pub(crate) struct UnexpectedParenInRangePat {

compiler/rustc_parse/src/parser/pat.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use crate::errors::{
66
InclusiveRangeExtraEquals, InclusiveRangeMatchArrow, InclusiveRangeNoEnd, InvalidMutInPattern,
77
PatternOnWrongSideOfAt, RemoveLet, RepeatedMutInPattern, SwitchRefBoxOrder,
88
TopLevelOrPatternNotAllowed, TopLevelOrPatternNotAllowedSugg, TrailingVertNotAllowed,
9-
UnexpectedExpressionInPattern, UnexpectedLifetimeInPattern, UnexpectedParenInRangePat,
10-
UnexpectedParenInRangePatSugg, UnexpectedVertVertBeforeFunctionParam,
11-
UnexpectedVertVertInPattern,
9+
UnexpectedExpressionInPattern, UnexpectedExpressionInPatternConstPatSugg,
10+
UnexpectedLifetimeInPattern, UnexpectedParenInRangePat, UnexpectedParenInRangePatSugg,
11+
UnexpectedVertVertBeforeFunctionParam, UnexpectedVertVertInPattern,
1212
};
1313
use crate::parser::expr::{could_be_unclosed_char_literal, DestructuredFloat};
1414
use crate::{maybe_recover_from_interpolated_ty_qpath, maybe_whole};
@@ -431,14 +431,22 @@ impl<'a> Parser<'a> {
431431

432432
let span = expr.span;
433433

434-
return Some((
435-
self.dcx().stash_err(
436-
span,
437-
StashKey::ExprInPat,
438-
UnexpectedExpressionInPattern { span, is_bound, is_method_call },
439-
),
434+
let mut diag = self.dcx().create_err(UnexpectedExpressionInPattern {
440435
span,
441-
));
436+
is_bound,
437+
is_method_call,
438+
});
439+
if self.psess.unstable_features.is_nightly_build() {
440+
diag.subdiagnostic(
441+
&self.dcx(),
442+
UnexpectedExpressionInPatternConstPatSugg {
443+
start_span: span.shrink_to_lo(),
444+
end_span: span.shrink_to_hi(),
445+
},
446+
);
447+
}
448+
449+
return Some((diag.stash(span, StashKey::ExprInPat).unwrap(), span));
442450
}
443451
}
444452

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ error: expected a pattern range bound, found an expression
9898
|
9999
LL | 0..5+1 => errors_only.push(x),
100100
| ^^^ arbitrary expressions are not allowed in patterns
101+
|
102+
= help: extract the expression into a `const` and refer to it
103+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
104+
|
105+
LL | 0..const { 5+1 } => errors_only.push(x),
106+
| +++++++ +
101107

102108
error: aborting due to 10 previous errors
103109

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ error: expected a pattern range bound, found an expression
6969
|
7070
LL | 0..=(5+1) => errors_only.push(x),
7171
| ^^^ arbitrary expressions are not allowed in patterns
72+
|
73+
= help: extract the expression into a `const` and refer to it
74+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
75+
|
76+
LL | 0..=(const { 5+1 }) => errors_only.push(x),
77+
| +++++++ +
7278

7379
error: aborting due to 7 previous errors
7480

tests/ui/parser/bad-name.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ error: expected a pattern, found an expression
1515
|
1616
LL | let x.y::<isize>.z foo;
1717
| ^^^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
18+
|
19+
= help: extract the expression into a `const` and refer to it
20+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
21+
|
22+
LL | let const { x.y::<isize>.z } foo;
23+
| +++++++ +
1824

1925
error: aborting due to 3 previous errors
2026

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error: expected a pattern, found an expression
33
|
44
LL | let buf[0] = 0;
55
| ^^^^^^ arbitrary expressions are not allowed in patterns
6+
|
7+
= help: extract the expression into a `const` and refer to it
8+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
9+
|
10+
LL | let const { buf[0] } = 0;
11+
| +++++++ +
612

713
error: aborting due to 1 previous error
814

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ error: expected a pattern, found an expression
33
|
44
LL | tmp[0] => {}
55
| ^^^^^^ arbitrary expressions are not allowed in patterns
6+
|
7+
= help: extract the expression into a `const` and refer to it
8+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
9+
|
10+
LL | const { tmp[0] } => {}
11+
| +++++++ +
612

713
error: aborting due to 1 previous error
814

tests/ui/parser/pat-lt-bracket-5.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ error: expected a pattern, found an expression
99
|
1010
LL | let v[0] = v[1];
1111
| ^^^^ arbitrary expressions are not allowed in patterns
12+
|
13+
= help: extract the expression into a `const` and refer to it
14+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
15+
|
16+
LL | let const { v[0] } = v[1];
17+
| +++++++ +
1218

1319
error: aborting due to 2 previous errors
1420

tests/ui/parser/pat-lt-bracket-6.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ error: expected a pattern, found an expression
2323
|
2424
LL | let Test(&desc[..]) = x;
2525
| ^^^^^^^^ arbitrary expressions are not allowed in patterns
26+
|
27+
= help: extract the expression into a `const` and refer to it
28+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
29+
|
30+
LL | let Test(&const { desc[..] }) = x;
31+
| +++++++ +
2632

2733
error: aborting due to 3 previous errors
2834

tests/ui/parser/pat-ranges-3.stderr

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ error: expected a pattern range bound, found an expression
33
|
44
LL | let 10 ..= 10 + 3 = 12;
55
| ^^^^^^ arbitrary expressions are not allowed in patterns
6+
|
7+
= help: extract the expression into a `const` and refer to it
8+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
9+
|
10+
LL | let 10 ..= const { 10 + 3 } = 12;
11+
| +++++++ +
612

713
error: expected a pattern range bound, found an expression
814
--> $DIR/pat-ranges-3.rs:7:9
915
|
1016
LL | let 10 - 3 ..= 10 = 8;
1117
| ^^^^^^ arbitrary expressions are not allowed in patterns
18+
|
19+
= help: extract the expression into a `const` and refer to it
20+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
21+
|
22+
LL | let const { 10 - 3 } ..= 10 = 8;
23+
| +++++++ +
1224

1325
error: aborting due to 2 previous errors
1426

tests/ui/sized/ensure-overriding-bindings-in-pattern-with-ty-err-doesnt-ice.stderr

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ error: expected a pattern, found an expression
3131
|
3232
LL | let str::<{fn str() { let str::T>>::as_bytes; }}, T>::as_bytes;
3333
| ^^^^^^^^^^^^^^^^^^ arbitrary expressions are not allowed in patterns
34+
|
35+
= help: extract the expression into a `const` and refer to it
36+
help: wrap the expression in a inline const (requires `#![feature(inline_const)]`)
37+
|
38+
LL | let str::<{fn str() { let const { str::T>>::as_bytes }; }}, T>::as_bytes;
39+
| +++++++ +
3440

3541
error: aborting due to 4 previous errors
3642

0 commit comments

Comments
 (0)