Skip to content

return const array from impl function, loses const information #67144

Closed
@robinvd

Description

@robinvd

When returning an const length array from an associated function the length is lost and it becomes a [T; _] array.

I tried this code:
(Playground)

#![feature(const_generics)]

struct X;

impl X {
    pub fn getn<const N: usize>(&self) -> [u8; N] {
        getn::<N>()
    }
}

fn getn<const N: usize>() -> [u8; N] {
    unsafe {
        std::mem::zeroed()
    }
}

fn test() {
    // works
    let [a,b,c] = getn::<3>();
    
    // cannot pattern-match on an array without a fixed length
    let [a,b,c] = X.getn::<3>();
    
    // mismatched types, expected array `[u8; 3]` found array `[u8; _]`
    let arr: [u8; 3] = X.getn::<3>();
}

I expected to see this happen:
all lines in test compile

Instead, this happened:
functions from the impl did not return a const array

Meta

rustc --version --verbose:

rustc 1.41.0-nightly (412f43ac5 2019-11-24)
binary: rustc
commit-hash: 412f43ac5b4ae8c3599e71c6972112e9be4758fa
commit-date: 2019-11-24
host: x86_64-unknown-linux-gnu
release: 1.41.0-nightly
LLVM version: 9.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-lazy-normalizationArea: Lazy normalization (tracking issue: #60471)C-bugCategory: This is a bug.F-const_generics`#![feature(const_generics)]`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions