1
Fork 0

Rollup merge of #95293 - compiler-errors:braces, r=davidtwco

suggest wrapping single-expr blocks in square brackets

Suggests a fix in cases like:

```diff
- const A: [i32; 1] = { 1 };

+ const A: [i32; 1] = [ 1 ];
                      ^   ^
```

Also edit the message for the same suggestion in the parser (e.g. `{ 1, 2 }`).

Fixes #95289
This commit is contained in:
Matthias Krüger 2022-04-01 06:59:42 +02:00 committed by GitHub
commit 8f493fd46a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 221 additions and 90 deletions

View file

@ -1919,17 +1919,13 @@ impl<'a> Parser<'a> {
match snapshot.parse_array_or_repeat_expr(attrs, token::Brace) {
Ok(arr) => {
let hi = snapshot.prev_token.span;
self.struct_span_err(
arr.span,
"this code is interpreted as a block expression, not an array",
)
.multipart_suggestion(
"try using [] instead of {}",
vec![(lo, "[".to_owned()), (hi, "]".to_owned())],
Applicability::MaybeIncorrect,
)
.note("to define an array, one would use square brackets instead of curly braces")
.emit();
self.struct_span_err(arr.span, "this is a block expression, not an array")
.multipart_suggestion(
"to make an array, use square brackets instead of curly braces",
vec![(lo, "[".to_owned()), (hi, "]".to_owned())],
Applicability::MaybeIncorrect,
)
.emit();
self.restore_snapshot(snapshot);
Some(self.mk_expr_err(arr.span))