Skip to content

Const-generic type mismatch: #[repr(usize)] enum as const parameter in arrays #61522

Closed
@qwerty19106

Description

@qwerty19106

The code:

#![feature(const_generics)]

use core::ptr::null_mut;

#[derive(Clone, Copy, Debug)]
#[repr(usize)]
pub enum ArrayCount {
    Values3 = 3,
    Values4 = 4,
    Values6 = 6
}

/// Non-Copy type!
#[repr(transparent)]
struct ArrayItem<T>(*mut T);

impl<T> ArrayItem<T> {
    pub fn new() -> Self {
        Self(null_mut())
    }
}

#[repr(transparent)]
pub struct MyArray<T, const COUNT: ArrayCount>([ArrayItem<T>; COUNT as usize]);

impl<T> MyArray<T, {ArrayCount::Values3}> {
    pub(crate) const fn new() -> Self {
        Self([
            ArrayItem::new(),
            ArrayItem::new(),
            ArrayItem::new(),
        ])
    }
}

fn main() {
}

It produced error:

error[E0308]: mismatched types
  --> src/main.rs:28:14
   |
28 |           Self([
   |  ______________^
29 | |             ArrayItem::new(),
30 | |             ArrayItem::new(),
31 | |             ArrayItem::new(),
32 | |         ])
   | |_________^ expected `COUNT as usize`, found `3usize`
   |
   = note: expected type `[ArrayItem<T>; _]`
              found type `[ArrayItem<T>; 3]`

Playground

Although Constants in array repeat expressions RFC not yet implemented, this code must work already.

Or I'm wrong and this syntax does not supported by const-generics?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.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