Skip to content

Const-generics don't work with constants #69816

Closed
@Boscop

Description

@Boscop

This compiles:

use arrayvec::{Array, ArrayVec};

#[ext(pub, name = IterExt)]
impl<T: Iterator<Item = U>, U> T {
    fn collect_arr<const N: usize>(self) -> [U; N]
    where
        [U; N]: Array<Item = U>,
        ArrayVec<[U; N]>: Debug,
    {
        self.collect::<ArrayVec<[U; N]>>().into_inner().expect("collect_arr")
    }
}

(Using the extend and arrayvec crates.)

But using it with a constant doesn't compile:
image

Even though pub const CUE_POINT_COUNT: usize = 8; is in scope!
If I write .collect_arr::<8usize>() instead, it compiles.
Also, if I don't use the extension trait method but inline it (.collect::<ArrayVec<[_; CUE_POINT_COUNT]>>().into_inner().unwrap()) it works with the constant!
So for some reason rustc can't see that CUE_POINT_COUNT == 8 here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-inferenceArea: Type inferenceC-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.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions