rust-lang / rust-lang/rust

Comma in indexing expression produces incorrect error location and suggestion to add a semicolon

Open
#161,147 3 comments 0 reactions 1 assignee View on GitHub

@hu-jeff is already working on this.

Since Aug 15, 2026.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
fn example() {
    (foo[
        bar
        .baz(),
    ])
}
Current output
error: expected `;`, found `[`
 --> src/lib.rs:2:9
  |
2 |     (foo[
  |         ^
  |
help: consider adding `;` here
  |
2 |     (foo;[
  |         +
Desired output
error: expected `]`, found `,`
  |
4 |         .baz(),
  |               ^
Rationale and extra context

The existing error message points to the wrong line, and suggests that a semicolon could help, when in fact it would make things worse.

Other cases

This problem is sensitive to line breaks — it does not happen if the extra comma is on the same line as foo.

Rust Version
rustc 1.97.1 (8bab26f4f 2026-07-14)
binary: rustc
commit-hash: 8bab26f4f68e0e26f0bb7960be334d5b520ea452
commit-date: 2026-07-14
host: aarch64-apple-darwin
release: 1.97.1
LLVM version: 22.1.6

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.