Skip to content

Commit f90f43d

Browse files
Fix diagnostic struct typo, make sure is_array_like_block checks that it's a block
1 parent 2947be7 commit f90f43d

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

Diff for: compiler/rustc_parse/src/errors.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -810,16 +810,16 @@ pub(crate) enum WrapInParentheses {
810810

811811
#[derive(Diagnostic)]
812812
#[diag(parse_array_brackets_instead_of_braces)]
813-
pub(crate) struct ArrayBracketsInsteadOfSpaces {
813+
pub(crate) struct ArrayBracketsInsteadOfBraces {
814814
#[primary_span]
815815
pub span: Span,
816816
#[subdiagnostic]
817-
pub sub: ArrayBracketsInsteadOfSpacesSugg,
817+
pub sub: ArrayBracketsInsteadOfBracesSugg,
818818
}
819819

820820
#[derive(Subdiagnostic)]
821821
#[multipart_suggestion(parse_suggestion, applicability = "maybe-incorrect")]
822-
pub(crate) struct ArrayBracketsInsteadOfSpacesSugg {
822+
pub(crate) struct ArrayBracketsInsteadOfBracesSugg {
823823
#[suggestion_part(code = "[")]
824824
pub left: Span,
825825
#[suggestion_part(code = "]")]

Diff for: compiler/rustc_parse/src/parser/expr.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,9 @@ impl<'a> Parser<'a> {
21902190
}
21912191

21922192
fn is_array_like_block(&mut self) -> bool {
2193-
self.look_ahead(1, |t| matches!(t.kind, TokenKind::Ident(..) | TokenKind::Literal(_)))
2193+
matches!(self.token.kind, TokenKind::OpenDelim(Delimiter::Brace))
2194+
&& self
2195+
.look_ahead(1, |t| matches!(t.kind, TokenKind::Ident(..) | TokenKind::Literal(_)))
21942196
&& self.look_ahead(2, |t| t == &token::Comma)
21952197
&& self.look_ahead(3, |t| t.can_begin_expr())
21962198
}
@@ -2202,9 +2204,9 @@ impl<'a> Parser<'a> {
22022204
let mut snapshot = self.create_snapshot_for_diagnostic();
22032205
match snapshot.parse_expr_array_or_repeat(exp!(CloseBrace)) {
22042206
Ok(arr) => {
2205-
let guar = self.dcx().emit_err(errors::ArrayBracketsInsteadOfSpaces {
2207+
let guar = self.dcx().emit_err(errors::ArrayBracketsInsteadOfBraces {
22062208
span: arr.span,
2207-
sub: errors::ArrayBracketsInsteadOfSpacesSugg {
2209+
sub: errors::ArrayBracketsInsteadOfBracesSugg {
22082210
left: lo,
22092211
right: snapshot.prev_token.span,
22102212
},

Diff for: tests/ui/parser/closure-return-syntax.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,21 @@
11
// Test that we cannot parse a closure with an explicit return type
22
// unless it uses braces.
33

4-
fn main() {
4+
fn needs_braces_1() {
55
let x = || -> i32 22;
66
//~^ ERROR expected `{`, found `22`
77
}
8+
9+
// Check other delimiters too.
10+
11+
fn needs_braces_2() {
12+
let x = || -> (i32, i32) (1, 2);
13+
//~^ ERROR expected `{`, found `(`
14+
}
15+
16+
fn needs_braces_3() {
17+
let c = || -> [i32; 2] [1, 2];
18+
//~^ ERROR expected `{`, found `[`
19+
}
20+
21+
fn main() {}

Diff for: tests/ui/parser/closure-return-syntax.stderr

+23-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,27 @@ help: you might have meant to write this as part of a block
99
LL | let x = || -> i32 { 22 };
1010
| + +
1111

12-
error: aborting due to 1 previous error
12+
error: expected `{`, found `(`
13+
--> $DIR/closure-return-syntax.rs:12:34
14+
|
15+
LL | let x = || -> (i32, i32) (1, 2);
16+
| ^ expected `{`
17+
|
18+
help: you might have meant to write this as part of a block
19+
|
20+
LL | let x = || -> (i32, i32) { (1, 2) };
21+
| + +
22+
23+
error: expected `{`, found `[`
24+
--> $DIR/closure-return-syntax.rs:17:32
25+
|
26+
LL | let c = || -> [i32; 2] [1, 2];
27+
| ^ expected `{`
28+
|
29+
help: you might have meant to write this as part of a block
30+
|
31+
LL | let c = || -> [i32; 2] { [1, 2] };
32+
| + +
33+
34+
error: aborting due to 3 previous errors
1335

0 commit comments

Comments
 (0)