Skip to content

Failed optimization: missing range/enumerate bijection suppresses length fact propagation #114242

Closed

Description

@SUPERCILEX
pub fn score_round_opt(candidates: &[bool]) {
    u32::try_from(candidates.len()).unwrap();

    for (i, _) in candidates.iter().enumerate() {
        u32::try_from(i).unwrap();
        std::hint::black_box(42);
    }
}

pub fn score_round_fail(candidates: &[bool]) {
    u32::try_from(candidates.len()).unwrap();

    for i in 0..candidates.len() {
        u32::try_from(i).unwrap();
        std::hint::black_box(42);
    }
}

https://rust.godbolt.org/z/8a6jd8acG

The unwrap in the loop fails to get optimized out when using 0..candidates.len() even though this is equivalent to enumerate.

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jul 30, 2023
  2. added
    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
    I-slowIssue: Problems and improvements with respect to performance of generated code.
    A-codegenArea: Code generation
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-bugCategory: This is a bug.
    and removed
    C-bugCategory: This is a bug.
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Jul 30, 2023
  3. added
    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing such
    on Oct 8, 2023
  4. LingMan commented on Jan 28, 2026

    @LingMan
    Contributor

    Optimizes as expected since Rust 1.73.

  5. nikic commented on Jan 28, 2026

    @nikic
    Contributor

    Closing this per previous comment. I expect that the root cause here is the same as #92174, so adding a test for that one should be sufficient.

    Oops, I misread this (enumerate was working, manual iteration wasn't). Keeping this open with E-needs-test.

  6. reopened this on Jan 28, 2026
  7. added
    E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
    on Jan 28, 2026
  8. el-ev commented on May 10, 2026

    @el-ev
    Contributor

    It no longer seems to be an issue. The two functions are compiled to identical code in rustc >= 1.73.0

  9. added a commit that references this issue on Jul 8, 2026
    a3b4470
  10. added a commit that references this issue on Jul 8, 2026
    e99d267
  11. added 2 commits that reference this issue on Jul 9, 2026
    c8256d6
    5f6ad3f
  12. added a commit that references this issue on Jul 9, 2026
    5dc7a51
  13. added a commit that references this issue on Jul 10, 2026
    7db06ae
  14. added a commit that references this issue on Jul 16, 2026
    df9fbc5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-bugCategory: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchE-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions