rust-lang / rust-lang/rust

rust should able to infer `Array<T>[CONST_START..CONST_END]` to type `[T; CONST_END - CONST_START]` at compile time

Open
#117,599 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-inference C-discussion
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

title

rust should able to infer Array<T>[CONST_START..CONST_END] to type [T; CONST_END - CONST_START] at compile time

code
const A: usize = 2;
const B: usize = 6;

fn main() {
    let buf: &[u8] = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

    // OK as expected
    {
        let _: &[u8] = &buf[A..B];
    }

    {
        // // error[E0308]: mismatched types
        // // expected `&[u8; 4]`, found `&[u8]`
        // let _: &[u8; 4] = &buf[A..B];
    }

    // https://doc.rust-lang.org/src/core/array/mod.rs.html
    {
        let _ = <[u8; 4]>::try_from(&buf[A..B]).unwrap();
    }

    // https://docs.rs/crate/arrayref/latest/source/src/lib.rs
    {
        let _ = unsafe { &*((&buf[A..B]).as_ptr() as *const [u8; B - A]) };
    }
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the examples in the issue and reading the referenced core array module and arrayref implementation. Determine the language and compiler changes needed for a constant range slice to infer its array length, with the desired outcome being successful compile-time inference of [T; CONST_END - CONST_START].

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.