rust-lang / rust-lang/rust-analyzer

MIR borrow checker panics on a field projection from a slice

Open
#22,757 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: rust-analyzer 1.99.0-nightly (af3d9558 2026-07-09)

rustc version: rustc 1.99.0-nightly (af3d95584 2026-07-09)

editor or extension: VsCode extension. rust-analyzer v0.3.2963. Observed through LSP textDocument/diagnostic and textDocument/codeAction requests. The behavior should not specific to an editor.

relevant settings: N/A

repository link (if public, optional): Overseer

code snippet to reproduce:

#[derive(Clone, Copy)]
enum Row {
    A { x: usize },
    B { x: usize, y: usize, z: bool, w: usize },
}

struct S {
    rows: Vec<Row>,
}

impl S {
    fn f(&mut self, idx: Option<usize>) -> bool {
        let rows = &self.rows;
        let Some(row) = idx.and_then(|i| rows.get(i)).copied() else {
            return false;
        };

        match row {
            Row::B { y, .. } => {
                let _ = y;
                false
            }
            Row::A { x } => {
                let _ = x;
                false
            }
        }
    }
}

The reduced standalone source does not preserve every aspect of the original inference context. The panic itself can be reproduced directly in the hir-ty test harness by applying a MIR Field projection to a slice-typed PlaceTy.

The panic is:

text thread 'Worker3' panicked at crates/hir-ty/src/mir.rs:1277:22: can't project out of [Row] ​

borrowck_query reaches PlaceTy::field_ty with a ProjectionElem::Field, no downcast variant, and a slice base type. field_ty only accepts non-enum ADTs, closures, and tuples in this case.

A field projection does not contain the element index needed to project through an array or slice, so it cannot derive a meaningful element-field type. This malformed MIR should not crash diagnostic or code-action requests. Propagating an error type allows MIR diagnostic recovery to stop safely.

This is rust-analyzer's MIR-based diagnostic borrow checker, not rustc's borrow checker, so the panic does not prevent cargo build.

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.

Research direction

Start in crates/hir-ty/src/mir.rs at PlaceTy::field_ty and inspect how borrowck_query handles ProjectionElem::Field on slice-typed places. Use the MIR test harness to reproduce the slice Field projection, then exercise the diagnostic and code-action paths. Done means the malformed projection recovers without a panic and the existing valid projection cases still pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.