rust-lang / rust-lang/rust-analyzer

Crash on MIR projection through slice reference (autoderef)

Open
#22,760 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I've been trying to investigate a crash with slice references. I've successfully minimised a repro and written a unit test:

#[test]
fn index_assignment_through_reference() {
    check_borrowck(
        r#"
pub fn f(xs: &mut [i32]) {
    xs[0usize] = 1;
}
    "#,
    );
}
$ RUST_BACKTRACE=1 cargo t -p hir-ty index_assign
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.54s
     Running unittests src/lib.rs (target/debug/deps/hir_ty-1dfab1f1e1155558)

running 1 test
test mir::lower::tests::index_assignment_through_reference ... FAILED

failures:

---- mir::lower::tests::index_assignment_through_reference stdout ----

thread 'mir::lower::tests::index_assignment_through_reference' (66052050) panicked at crates/hir-ty/src/mir.rs:1335:82:
called `Option::unwrap()` on a `None` value
stack backtrace:
   0: __rustc::rust_begin_unwind
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/std/src/panicking.rs:689:5
   1: core::panicking::panic_fmt
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/panicking.rs:80:14
   2: core::panicking::panic
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/panicking.rs:150:5
   3: core::option::unwrap_failed
             at /rustc/59807616e1fa2540724bfbac14d7976d7e4a3860/library/core/src/option.rs:2236:5
   4: core::option::Option<T>::unwrap
             at /Users/wilfred/.rustup/toolchains/stable-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/option.rs:1016:21
   5: hir_ty::mir::PlaceTy::projection_ty_core
             at ./src/mir.rs:1335:82
   6: hir_ty::mir::PlaceTy::projection_ty
             at ./src/mir.rs:1289:14
   7: hir_ty::mir::borrowck::place_case
             at ./src/mir/borrowck.rs:441:17
   8: hir_ty::mir::borrowck::mutability_of_locals
             at ./src/mir/borrowck.rs:582:27

It's definitely a bug, and I think it's an issue with autoderef. I'm not sure what the correct fix is.

I could check for None instead of unwrapping here:

https://github.com/rust-lang/rust-analyzer/blob/9482996a746236c56c3333ef1bf796bd2d5f7272/crates/hir-ty/src/mir.rs#L1334-L1336

but that feels wrong -- shouldn't MIR have autoderef logic already?

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 with the reproducer in mir::lower::tests::index_assignment_through_reference and run the hir-ty index_assign test. Read PlaceTy::projection_ty_core in crates/hir-ty/src/mir.rs around line 1335, then follow its use from crates/hir-ty/src/mir/borrowck.rs. Done means the test no longer panics and index assignment through a mutable slice reference has correct MIR autoderef handling.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.