Skip to content

code less optimized when accessing a specific array element, only on nightly #111508

@antonilol

Description

@antonilol

I tried this code on rustc nightly (rustc 1.71.0-nightly (c4190f2d3 2023-05-07)) on godbolt and the assembly code contains some unnecessary checks, but not when changing the value of N to anything different than 24 (but not out of bounds for a) or changing the version to 1.69.0 or beta

const N: usize = 24;

pub fn example(a: Vec<u8>) -> u8 {
    if a.len() != 32 {
        return 0;
    }

    let a: [u8; 32] = a.try_into().unwrap();

    a[15] + a[N]
}

https://godbolt.org/z/WMWv5s57o

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-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.S-has-mcveStatus: A Minimal Complete and Verifiable Example has been found for this issueT-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