1
Fork 0

Fix diagnostic struct typo, make sure is_array_like_block checks that it's a block

This commit is contained in:
Michael Goulet 2025-03-20 15:46:19 +00:00
parent 2947be7af8
commit f90f43d62b
4 changed files with 46 additions and 8 deletions

View file

@ -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 = "]")]

View file

@ -2190,7 +2190,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())
} }
@ -2202,9 +2204,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,
}, },

View file

@ -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() {}

View file

@ -9,5 +9,27 @@ help: you might have meant to write this as part of a block
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);
| ^ expected `{`
|
help: you might have meant to write this as part of a block
|
LL | let x = || -> (i32, i32) { (1, 2) };
| + +
error: expected `{`, found `[`
--> $DIR/closure-return-syntax.rs:17:32
|
LL | let c = || -> [i32; 2] [1, 2];
| ^ expected `{`
|
help: you might have meant to write this as part of a block
|
LL | let c = || -> [i32; 2] { [1, 2] };
| + +
error: aborting due to 3 previous errors