rust-lang / rust-lang/rust

ConstantIndex and Index not flagged as potential aliases

Open
#160,525 11 comments 6 reactions 1 assignee View on GitHub

@beepster4096 is already working on this.

Since Aug 8, 2026.

A-array A-borrow-checker A-NLL C-bug I-unsound P-high T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I tried this code (playpen):

fn main() {
    let mut arr = [[Box::new(42)]];
    let alias = &mut arr[0][{ let [row] = arr; drop(row); 0 }];
    println!("{}", **alias); // use-after-free of arr's dead stack slot
}

I expected to see this happen: Compiler rejects code as unsound.

Instead, this happened: Compiler accepts code and prints a garbage number.

When run under Miri, we see this output:

error: Undefined Behavior: constructing invalid value of type std::boxed::Box<i32>: encountered a dangling box (use-after-free)
 --> src/main.rs:4:20
  |
4 |     println!("{}", **alias); // use-after-free of arr's dead stack slot
  |                    ^^^^^^^ Undefined Behavior occurred here
  |
  = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
  = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

Hypothesis: after a move _1[ConstantIndex{0 of 1}], a subsequent use of _1[Index(_6)] is not flagged as a use-of-partially-moved. The "any subpath moved" check of borrowck treats ConstantIndex as a sibling of a runtime Index projection, rather than a potential alias.

Meta

Nightly channel
Build using the Nightly version: 1.99.0-nightly

(2026-08-03 504869653f510b279c54)

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.