Skip to content

Commit 784e91d

Browse files
authored
Rollup merge of rust-lang#148680 - ShE3py:array-colon, r=JonathanBrouwer
Recover `[T: N]` as `[T; N]` `;` is similar and (keyboard-wise) next to `:`, so a verbose suggestion may help to see the difference. Parent PR: rust-lang#143905 --- `@rustbot` label +A-parser +A-array +A-diagnostics +A-suggestion-diagnostics +D-papercut
2 parents 9fe2f4c + d1052e4 commit 784e91d

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,6 @@ impl<'a> Parser<'a> {
686686
let mut err =
687687
self.dcx().struct_span_err(span, format!("expected `;` or `]`, found {}", token_descr));
688688
err.span_label(span, "expected `;` or `]`");
689-
err.note("you might have meant to write a slice or array type");
690689

691690
// If we cannot recover, return the error immediately.
692691
if !self.may_recover() {
@@ -695,12 +694,10 @@ impl<'a> Parser<'a> {
695694

696695
let snapshot = self.create_snapshot_for_diagnostic();
697696

698-
let suggestion_span = if self.eat(exp!(Comma)) || self.eat(exp!(Star)) {
699-
// Consume common erroneous separators.
700-
self.prev_token.span
701-
} else {
702-
self.token.span.shrink_to_lo()
703-
};
697+
// Consume common erroneous separators.
698+
let hi = self.prev_token.span.hi();
699+
_ = self.eat(exp!(Comma)) || self.eat(exp!(Colon)) || self.eat(exp!(Star));
700+
let suggestion_span = self.prev_token.span.with_lo(hi);
704701

705702
// we first try to parse pattern like `[u8 5]`
706703
let length = match self.parse_expr_anon_const() {

tests/ui/parser/better-expected.stderr

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ error: expected `;` or `]`, found `3`
44
LL | let x: [isize 3];
55
| ^ expected `;` or `]`
66
|
7-
= note: you might have meant to write a slice or array type
87
help: you might have meant to use `;` as the separator
98
|
10-
LL | let x: [isize ;3];
11-
| +
9+
LL | let x: [isize; 3];
10+
| +
1211

1312
error: aborting due to 1 previous error
1413

tests/ui/parser/issues/error-pattern-issue-50571.stderr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error: expected `;` or `]`, found `,`
44
LL | fn foo([a, b]: [i32; 2]) {}
55
| ^ expected `;` or `]`
66
|
7-
= note: you might have meant to write a slice or array type
87
help: you might have meant to use `;` as the separator
98
|
109
LL - fn foo([a, b]: [i32; 2]) {}

tests/ui/parser/recover/array-type-no-semi.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ fn main() {
1414
//~| ERROR attempt to use a non-constant value in a constant [E0435]
1515
let e: [i32 5];
1616
//~^ ERROR expected `;` or `]`, found `5`
17+
let f: [i32: 1 - 1];
18+
//~^ ERROR expected `;` or `]`, found `:`
1719
}

tests/ui/parser/recover/array-type-no-semi.stderr

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ error: expected `;` or `]`, found `,`
44
LL | let b: [i32, 5];
55
| ^ expected `;` or `]`
66
|
7-
= note: you might have meant to write a slice or array type
87
help: you might have meant to use `;` as the separator
98
|
109
LL - let b: [i32, 5];
@@ -19,16 +18,13 @@ LL | let a: [i32, ];
1918
| |
2019
| while parsing the type for `a`
2120
| help: use `=` if you meant to assign
22-
|
23-
= note: you might have meant to write a slice or array type
2421

2522
error: expected `;` or `]`, found `,`
2623
--> $DIR/array-type-no-semi.rs:12:16
2724
|
2825
LL | let c: [i32, x];
2926
| ^ expected `;` or `]`
3027
|
31-
= note: you might have meant to write a slice or array type
3228
help: you might have meant to use `;` as the separator
3329
|
3430
LL - let c: [i32, x];
@@ -41,11 +37,22 @@ error: expected `;` or `]`, found `5`
4137
LL | let e: [i32 5];
4238
| ^ expected `;` or `]`
4339
|
44-
= note: you might have meant to write a slice or array type
4540
help: you might have meant to use `;` as the separator
4641
|
47-
LL | let e: [i32 ;5];
48-
| +
42+
LL | let e: [i32; 5];
43+
| +
44+
45+
error: expected `;` or `]`, found `:`
46+
--> $DIR/array-type-no-semi.rs:17:16
47+
|
48+
LL | let f: [i32: 1 - 1];
49+
| ^ expected `;` or `]`
50+
|
51+
help: you might have meant to use `;` as the separator
52+
|
53+
LL - let f: [i32: 1 - 1];
54+
LL + let f: [i32; 1 - 1];
55+
|
4956

5057
error[E0435]: attempt to use a non-constant value in a constant
5158
--> $DIR/array-type-no-semi.rs:12:18
@@ -65,7 +72,7 @@ error[E0423]: expected value, found builtin type `i32`
6572
LL | let a: [i32, ];
6673
| ^^^ not a value
6774

68-
error: aborting due to 6 previous errors
75+
error: aborting due to 7 previous errors
6976

7077
Some errors have detailed explanations: E0423, E0435.
7178
For more information about an error, try `rustc --explain E0423`.

tests/ui/parser/removed-syntax/removed-syntax-fixed-vec.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ error: expected `;` or `]`, found `*`
44
LL | type v = [isize * 3];
55
| ^ expected `;` or `]`
66
|
7-
= note: you might have meant to write a slice or array type
87
help: you might have meant to use `;` as the separator
98
|
109
LL - type v = [isize * 3];
11-
LL + type v = [isize ; 3];
10+
LL + type v = [isize; 3];
1211
|
1312

1413
warning: type `v` should have an upper camel case name

0 commit comments

Comments
 (0)