Rollup merge of #138749 - compiler-errors:closure-recovery, r=fmease
Fix closure recovery for missing block when return type is specified Firstly, fix the `is_array_like_block` condition to make sure we're actually recovering a mistyped *block* rather than some other delimited expression. This fixes #138748. Secondly, split out the recovery of missing braces on a closure body into a separate recovery. Right now, the suggestion `"you might have meant to write this as part of a block"` originates from `suggest_fixes_misparsed_for_loop_head`, which feels kinda brittle and coincidental since AFAICT that recovery wasn't ever really intended to fix this. We also can make this `MachineApplicable` in this case. Fixes #138748 r? `@fmease` or reassign if you're busy/don't wanna review this
This commit is contained in:
commit
0a579d5247
4 changed files with 101 additions and 16 deletions
|
@ -810,16 +810,16 @@ pub(crate) enum WrapInParentheses {
|
||||||
|
|
||||||
#[derive(Diagnostic)]
|
#[derive(Diagnostic)]
|
||||||
#[diag(parse_array_brackets_instead_of_braces)]
|
#[diag(parse_array_brackets_instead_of_braces)]
|
||||||
pub(crate) struct ArrayBracketsInsteadOfSpaces {
|
pub(crate) struct ArrayBracketsInsteadOfBraces {
|
||||||
#[primary_span]
|
#[primary_span]
|
||||||
pub span: Span,
|
pub span: Span,
|
||||||
#[subdiagnostic]
|
#[subdiagnostic]
|
||||||
pub sub: ArrayBracketsInsteadOfSpacesSugg,
|
pub sub: ArrayBracketsInsteadOfBracesSugg,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Subdiagnostic)]
|
#[derive(Subdiagnostic)]
|
||||||
#[multipart_suggestion(parse_suggestion, applicability = "maybe-incorrect")]
|
#[multipart_suggestion(parse_suggestion, applicability = "maybe-incorrect")]
|
||||||
pub(crate) struct ArrayBracketsInsteadOfSpacesSugg {
|
pub(crate) struct ArrayBracketsInsteadOfBracesSugg {
|
||||||
#[suggestion_part(code = "[")]
|
#[suggestion_part(code = "[")]
|
||||||
pub left: Span,
|
pub left: Span,
|
||||||
#[suggestion_part(code = "]")]
|
#[suggestion_part(code = "]")]
|
||||||
|
|
|
@ -2200,7 +2200,9 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_array_like_block(&mut self) -> bool {
|
fn is_array_like_block(&mut self) -> bool {
|
||||||
self.look_ahead(1, |t| matches!(t.kind, TokenKind::Ident(..) | TokenKind::Literal(_)))
|
matches!(self.token.kind, TokenKind::OpenDelim(Delimiter::Brace))
|
||||||
|
&& self
|
||||||
|
.look_ahead(1, |t| matches!(t.kind, TokenKind::Ident(..) | TokenKind::Literal(_)))
|
||||||
&& self.look_ahead(2, |t| t == &token::Comma)
|
&& self.look_ahead(2, |t| t == &token::Comma)
|
||||||
&& self.look_ahead(3, |t| t.can_begin_expr())
|
&& self.look_ahead(3, |t| t.can_begin_expr())
|
||||||
}
|
}
|
||||||
|
@ -2212,9 +2214,9 @@ impl<'a> Parser<'a> {
|
||||||
let mut snapshot = self.create_snapshot_for_diagnostic();
|
let mut snapshot = self.create_snapshot_for_diagnostic();
|
||||||
match snapshot.parse_expr_array_or_repeat(exp!(CloseBrace)) {
|
match snapshot.parse_expr_array_or_repeat(exp!(CloseBrace)) {
|
||||||
Ok(arr) => {
|
Ok(arr) => {
|
||||||
let guar = self.dcx().emit_err(errors::ArrayBracketsInsteadOfSpaces {
|
let guar = self.dcx().emit_err(errors::ArrayBracketsInsteadOfBraces {
|
||||||
span: arr.span,
|
span: arr.span,
|
||||||
sub: errors::ArrayBracketsInsteadOfSpacesSugg {
|
sub: errors::ArrayBracketsInsteadOfBracesSugg {
|
||||||
left: lo,
|
left: lo,
|
||||||
right: snapshot.prev_token.span,
|
right: snapshot.prev_token.span,
|
||||||
},
|
},
|
||||||
|
@ -2337,7 +2339,8 @@ impl<'a> Parser<'a> {
|
||||||
let capture_clause = self.parse_capture_clause()?;
|
let capture_clause = self.parse_capture_clause()?;
|
||||||
let (fn_decl, fn_arg_span) = self.parse_fn_block_decl()?;
|
let (fn_decl, fn_arg_span) = self.parse_fn_block_decl()?;
|
||||||
let decl_hi = self.prev_token.span;
|
let decl_hi = self.prev_token.span;
|
||||||
let mut body = match fn_decl.output {
|
let mut body = match &fn_decl.output {
|
||||||
|
// No return type.
|
||||||
FnRetTy::Default(_) => {
|
FnRetTy::Default(_) => {
|
||||||
let restrictions =
|
let restrictions =
|
||||||
self.restrictions - Restrictions::STMT_EXPR - Restrictions::ALLOW_LET;
|
self.restrictions - Restrictions::STMT_EXPR - Restrictions::ALLOW_LET;
|
||||||
|
@ -2349,11 +2352,8 @@ impl<'a> Parser<'a> {
|
||||||
Err(err) => self.recover_closure_body(err, before, prev, token, lo, decl_hi)?,
|
Err(err) => self.recover_closure_body(err, before, prev, token, lo, decl_hi)?,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
// Explicit return type (`->`) needs block `-> T { }`.
|
||||||
// If an explicit return type is given, require a block to appear (RFC 968).
|
FnRetTy::Ty(ty) => self.parse_closure_block_body(ty.span)?,
|
||||||
let body_lo = self.token.span;
|
|
||||||
self.parse_expr_block(None, body_lo, BlockCheckMode::Default)?
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match coroutine_kind {
|
match coroutine_kind {
|
||||||
|
@ -2405,6 +2405,49 @@ impl<'a> Parser<'a> {
|
||||||
Ok(closure)
|
Ok(closure)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// If an explicit return type is given, require a block to appear (RFC 968).
|
||||||
|
fn parse_closure_block_body(&mut self, ret_span: Span) -> PResult<'a, P<Expr>> {
|
||||||
|
if self.may_recover()
|
||||||
|
&& self.token.can_begin_expr()
|
||||||
|
&& !matches!(self.token.kind, TokenKind::OpenDelim(Delimiter::Brace))
|
||||||
|
&& !self.token.is_whole_block()
|
||||||
|
{
|
||||||
|
let snapshot = self.create_snapshot_for_diagnostic();
|
||||||
|
let restrictions =
|
||||||
|
self.restrictions - Restrictions::STMT_EXPR - Restrictions::ALLOW_LET;
|
||||||
|
let tok = self.token.clone();
|
||||||
|
match self.parse_expr_res(restrictions, AttrWrapper::empty()) {
|
||||||
|
Ok((expr, _)) => {
|
||||||
|
let descr = super::token_descr(&tok);
|
||||||
|
let mut diag = self
|
||||||
|
.dcx()
|
||||||
|
.struct_span_err(tok.span, format!("expected `{{`, found {descr}"));
|
||||||
|
diag.span_label(
|
||||||
|
ret_span,
|
||||||
|
"explicit return type requires closure body to be enclosed in braces",
|
||||||
|
);
|
||||||
|
diag.multipart_suggestion_verbose(
|
||||||
|
"wrap the expression in curly braces",
|
||||||
|
vec![
|
||||||
|
(expr.span.shrink_to_lo(), "{ ".to_string()),
|
||||||
|
(expr.span.shrink_to_hi(), " }".to_string()),
|
||||||
|
],
|
||||||
|
Applicability::MachineApplicable,
|
||||||
|
);
|
||||||
|
diag.emit();
|
||||||
|
return Ok(expr);
|
||||||
|
}
|
||||||
|
Err(diag) => {
|
||||||
|
diag.cancel();
|
||||||
|
self.restore_snapshot(snapshot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let body_lo = self.token.span;
|
||||||
|
self.parse_expr_block(None, body_lo, BlockCheckMode::Default)
|
||||||
|
}
|
||||||
|
|
||||||
/// Parses an optional `move` or `use` prefix to a closure-like construct.
|
/// Parses an optional `move` or `use` prefix to a closure-like construct.
|
||||||
fn parse_capture_clause(&mut self) -> PResult<'a, CaptureBy> {
|
fn parse_capture_clause(&mut self) -> PResult<'a, CaptureBy> {
|
||||||
if self.eat_keyword(exp!(Move)) {
|
if self.eat_keyword(exp!(Move)) {
|
||||||
|
|
|
@ -1,7 +1,21 @@
|
||||||
// Test that we cannot parse a closure with an explicit return type
|
// Test that we cannot parse a closure with an explicit return type
|
||||||
// unless it uses braces.
|
// unless it uses braces.
|
||||||
|
|
||||||
fn main() {
|
fn needs_braces_1() {
|
||||||
let x = || -> i32 22;
|
let x = || -> i32 22;
|
||||||
//~^ ERROR expected `{`, found `22`
|
//~^ ERROR expected `{`, found `22`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check other delimiters too.
|
||||||
|
|
||||||
|
fn needs_braces_2() {
|
||||||
|
let x = || -> (i32, i32) (1, 2);
|
||||||
|
//~^ ERROR expected `{`, found `(`
|
||||||
|
}
|
||||||
|
|
||||||
|
fn needs_braces_3() {
|
||||||
|
let c = || -> [i32; 2] [1, 2];
|
||||||
|
//~^ ERROR expected `{`, found `[`
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {}
|
||||||
|
|
|
@ -2,12 +2,40 @@ error: expected `{`, found `22`
|
||||||
--> $DIR/closure-return-syntax.rs:5:23
|
--> $DIR/closure-return-syntax.rs:5:23
|
||||||
|
|
|
|
||||||
LL | let x = || -> i32 22;
|
LL | let x = || -> i32 22;
|
||||||
| ^^ expected `{`
|
| --- ^^
|
||||||
|
| |
|
||||||
|
| explicit return type requires closure body to be enclosed in braces
|
||||||
|
|
|
|
||||||
help: you might have meant to write this as part of a block
|
help: wrap the expression in curly braces
|
||||||
|
|
|
|
||||||
LL | let x = || -> i32 { 22 };
|
LL | let x = || -> i32 { 22 };
|
||||||
| + +
|
| + +
|
||||||
|
|
||||||
error: aborting due to 1 previous error
|
error: expected `{`, found `(`
|
||||||
|
--> $DIR/closure-return-syntax.rs:12:34
|
||||||
|
|
|
||||||
|
LL | let x = || -> (i32, i32) (1, 2);
|
||||||
|
| ---------- ^
|
||||||
|
| |
|
||||||
|
| explicit return type requires closure body to be enclosed in braces
|
||||||
|
|
|
||||||
|
help: wrap the expression in curly braces
|
||||||
|
|
|
||||||
|
LL | let x = || -> (i32, i32) { (1, 2) };
|
||||||
|
| + +
|
||||||
|
|
||||||
|
error: expected `{`, found `[`
|
||||||
|
--> $DIR/closure-return-syntax.rs:17:32
|
||||||
|
|
|
||||||
|
LL | let c = || -> [i32; 2] [1, 2];
|
||||||
|
| -------- ^
|
||||||
|
| |
|
||||||
|
| explicit return type requires closure body to be enclosed in braces
|
||||||
|
|
|
||||||
|
help: wrap the expression in curly braces
|
||||||
|
|
|
||||||
|
LL | let c = || -> [i32; 2] { [1, 2] };
|
||||||
|
| + +
|
||||||
|
|
||||||
|
error: aborting due to 3 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue