Skip to content

index out of bound in slice not caught at compilation #38035

Open

Description

@aybabtme

Hi! I'm just learning Rust! I saw this odd behavior, where the compiler will nicely tell me that an index in an array is impossible at compile time, but won't tell me that an index in a slice of known size is also impossible.

I thought I'd share.

This fails at compile time:

let y = [1];
println!("y={}", y[1]); 

This fails at runtime:

let y = [1];
println!("y={}", &y[0..1][1]); 

Would be cool if &y[0..1][1] also failed at compile time.

Activity

  1. leonardo-m commented on Nov 28, 2016

    @leonardo-m

    This may need a significant improvement of the compiler, but it's worth doing.

  2. clarfonthey commented on Nov 28, 2016

    @clarfonthey
    Contributor

    There are currently lints in clippy for this, I believe. Not sure if the compiler ever plans to add these.

  3. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 9, 2017
  4. added
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    and removed
    A-diagnosticsArea: Messages for errors, warnings, and lints
    on Dec 13, 2018
  5. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-mir-optArea: MIR optimizations
    and removed
    A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)
    on Oct 9, 2020
  6. added a commit that references this issue on Oct 5, 2024
  7. added a commit that references this issue on Oct 5, 2024
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-diagnosticsArea: Messages for errors, warnings, and lintsA-mir-optArea: MIR optimizationsC-feature-requestCategory: A feature request, i.e: not implemented / a PR.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