rust-lang / rust-lang/rust

Different codegen between index and iterator

Open
#149,672 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-codegen needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

They should produce the same codegen.
https://godbolt.org/z/W5hKTedd3

#[inline(never)]
pub fn f_index(data: &mut [u32]) {
    for i in 0..data.len() {
        data[i] += 1;
    }
}
#[inline(never)]
pub fn f_iter(data: &mut [u32]) {
    for v in data.iter_mut() {
        *v += 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 by reproducing the comparison in the Godbolt link using the two functions, f_index and f_iter, and inspect their generated code. Trace the relevant Rust compiler codegen behavior to determine why indexed and iterator loops differ. Done means both examples produce equivalent codegen, with a regression test covering the behavior.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.