Repository navigation
Closed
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationArea: Code generationC-bugCategory: This is a bug.Category: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: 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.Call 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.Issue: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
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.
Metadata
Metadata
Assignees
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationArea: Code generationC-bugCategory: This is a bug.Category: This is a bug.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: 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.Call 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.Issue: 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.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
LingMan commentedon Jan 28, 2026
Optimizes as expected since Rust 1.73.
nikic commentedon Jan 28, 2026
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.
el-ev commentedon May 10, 2026
It no longer seems to be an issue. The two functions are compiled to identical code in rustc >= 1.73.0
Rollup merge of rust-lang#158902 - amirHdev:add-codegen-test-range-le…
Rollup merge of rust-lang#158902 - amirHdev:add-codegen-test-range-le…
Rollup merge of rust-lang#158902 - amirHdev:add-codegen-test-range-le…
Rollup merge of rust-lang#158902 - amirHdev:add-codegen-test-range-le…
Unrolled build for #158902
Rollup merge of #158902 - amirHdev:add-codegen-test-range-len-try-fro…
Rollup merge of #158902 - amirHdev:add-codegen-test-range-len-try-fro…