Skip to content

Rust fails to optimize const slice #147163

@TroyKomodo

Description

@TroyKomodo

I tried this code:

const fn test() -> &'static [&'static str] { const ITEMS: &[&str] = const { std::slice::from_ref(&[ "hello", "world", ][0]) }; ITEMS } #[inline(never)] pub fn main() { std::hint::black_box(test()); }

I expected to see this happen:

The string world to be pruned from the final binary.

Instead, this happened:

https://godbolt.org/z/WTM5jz987

The array was not pruned at all.

Meta

rustc --version --verbose:

godbolt rustc nightly. 

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchT-opsemRelevant to the opsem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions