rust-lang / rust-lang/rust

Missed bounds check elimination

Open
#127,553 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen A-LLVM C-bug C-optimization I-slow T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

On Rust 1.79.0.

pub const fn missing_bce(v: &[u32]) -> u32 {
    if v.is_empty() {
        return 0;
    }
    let mut sum = 0;
    let mut i = v.len() - 1;
    while i > 0 {
        sum += v[i];
        i -= 1;
    }
    sum
}

https://godbolt.org/z/xxjKd5anW

The compiler should be able to elide the bounds check for v[i] since i is always in [0, v.len()-1].

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 Rust 1.79.0 reproducer in the issue and inspect its generated assembly on the linked Godbolt example. Confirm where the bounds check remains and compare behavior across compiler versions or optimization settings. Done means the check is eliminated for this pattern, with a regression test covering the example.

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
Stale
Clarity
Mostly clear
Newbie friendliness
40/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.