Recover and suggest to use ; to construct array type by xizheyin · Pull Request #143905 · rust-lang/rust (original) (raw)

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation27 Commits2 Checks11 Files changed

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

@xizheyin

@rustbot rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.

labels

Jul 13, 2025

xizheyin

Comment on lines 15 to 18

help: use `=` if you meant to assign
|
LL - let b: [i32, 5];
LL + let b = [i32, 5];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This suggestion is wrong, but it have nothing with this PR. Because the old diagnostic has the same help message.

Old Diagnostic:

error: expected one of `!`, `(`, `+`, `::`, `;`, `<`, or `]`, found `,`
 --> src/main.rs:3:16
  |
3 |     let b: [i32, 5];
  |          -     ^ expected one of 7 possible tokens
  |          |
  |          while parsing the type for `b`
  |          help: use `=` if you meant to assign

compiler-errors

compiler-errors

@compiler-errors

@rustbot rustbot added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Jul 13, 2025

@rustbot

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@xizheyin

Also, why can't we emit the error and then return TyKind::Array, so we can recover successfully rather than propagating an error?

Yeah, I have tried to recover it to TyKind::Array, but It emit a regression. In this test, it will emit error: expected ; or ], found , immidiately, which is wrong. It just want call parse_ty_common for Some(x), while in this case, it parse it into Array and give a wrong suggestion.

https://github.com/rust-lang/rust/blob/master/tests/ui/issues/issue-50571.rs
https://github.com/rust-lang/rust/blob/master/tests/ui/issues/issue-50571.stderr

@rustbot ready

@rustbot rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Jul 14, 2025

compiler-errors

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally think it's fine to regress #50571, since it's an issue that only has to do with edition 2015 and is a rare enough.

I'd still like to see a better recovery that actually ends up returning Ok(TyKind::Array(...)).

@rustbot rustbot added S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Jul 14, 2025

@xizheyin

Signed-off-by: xizheyin xizheyin@smail.nju.edu.cn

xizheyin

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

help: you might have meant to use `;` as the separator
|
LL | type v = [isize ;* 3];
| +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This regression is somewhat werid, because parse_expr_anon_const take it as a deref of 3. That maybe reasonable for

const L = &3;
type V = [isize * L];

After applying the suggestion, it is correct.

But IMO, it does not matter in this * case, because after applying the suggestion, compiler will report something like you can not deref a integer, which is reasonable.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could perhaps special case both , and * as separators here?

Comment on lines +38 to +48

error: expected `;` or `]`, found `5`
--> $DIR/array-type-no-semi.rs:15:17
|
LL | let e: [i32 5];
| ^ expected `;` or `]`
|
= note: you might have meant to write a slice or array type
help: you might have meant to use `;` as the separator
|
LL | let e: [i32 ;5];
| +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the new case like [i32 5].

@rustbot rustbot added S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

and removed S-waiting-on-author

Status: This is awaiting some action (such as code changes or more information) from the author.

labels

Jul 14, 2025

@rust-log-analyzer

This comment has been minimized.

@rustbot rustbot added A-tidy

Area: The tidy tool

T-bootstrap

Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

labels

Jul 14, 2025

@rustbot

There are changes to the tidy tool.

cc @jieyouxu

@xizheyin xizheyin changed the titleSuggest use ; to construct array type when other token exists Recover and suggest to use ; to construct array type

Jul 14, 2025

jieyouxu

compiler-errors

help: you might have meant to use `;` as the separator
|
LL | type v = [isize ;* 3];
| +

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could perhaps special case both , and * as separators here?

Comment on lines 609 to 614

let length = match self.parse_expr_anon_const() {
Ok(length) => length,
Err(e) => {
e.cancel();
self.bump();
match self.parse_expr_anon_const() {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of parsing once then parsing again, could you do:

let suggestion_span = if self.eat(comma) || self.eat(asterisk) {
    // Consume common erroneous separators.
    self.prev_token.span
} else {
    self.token.span.shrink_to_lo()
};

/// then parse the length.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should other separators be considered, such as :?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think , and * are fine for now

@xizheyin

Signed-off-by: xizheyin xizheyin@smail.nju.edu.cn

@xizheyin

Yeah, I have instead specially judge , and *.

compiler-errors

@compiler-errors

@bors

📌 Commit ed88af2 has been approved by compiler-errors

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

and removed S-waiting-on-review

Status: Awaiting review from the assignee but also interested parties.

labels

Jul 15, 2025

bors added a commit that referenced this pull request

Jul 15, 2025

@bors

Rollup of 13 pull requests

Successful merges:

Failed merges:

r? @ghost @rustbot modify labels: rollup

rust-timer added a commit that referenced this pull request

Jul 15, 2025

@rust-timer

Rollup merge of #143905 - xizheyin:143828, r=compiler-errors

Recover and suggest to use ; to construct array type

Fixes #143828

r? compiler

github-actions bot pushed a commit to rust-lang/rustc-dev-guide that referenced this pull request

Jul 17, 2025

@bors

Muscraft pushed a commit to Muscraft/rust that referenced this pull request

Jul 21, 2025

@samueltardieu @Muscraft

Recover and suggest to use ; to construct array type

Fixes rust-lang#143828

r? compiler

Muscraft pushed a commit to Muscraft/rust that referenced this pull request

Jul 21, 2025

@bors @Muscraft

Zalathar added a commit to Zalathar/rust that referenced this pull request

Nov 9, 2025

@Zalathar

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

rust-timer added a commit that referenced this pull request

Nov 9, 2025

@rust-timer

Rollup merge of #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: #143905


@rustbot label +A-parser +A-array +A-diagnostics +A-suggestion-diagnostics +D-papercut

github-actions bot pushed a commit to rust-lang/miri that referenced this pull request

Nov 10, 2025

@Zalathar

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/rust#143905


@rustbot label +A-parser +A-array +A-diagnostics +A-suggestion-diagnostics +D-papercut

makai410 pushed a commit to makai410/rustc_public that referenced this pull request

Nov 10, 2025

@Zalathar

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/rust#143905


@rustbot label +A-parser +A-array +A-diagnostics +A-suggestion-diagnostics +D-papercut

makai410 pushed a commit to makai410/rust that referenced this pull request

Nov 10, 2025

@Zalathar

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

makai410 pushed a commit to makai410/rustc_public that referenced this pull request

Nov 16, 2025

@Zalathar

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/rust#143905


@rustbot label +A-parser +A-array +A-diagnostics +A-suggestion-diagnostics +D-papercut

Labels

A-tidy

Area: The tidy tool

S-waiting-on-bors

Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

T-bootstrap

Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)

T-compiler

Relevant to the compiler team, which will review and decide on the PR/issue.