Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
qwerty19106 opened this issue Jun 4, 2019 · 9 comments · Fixed by #70939
Closed

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

qwerty19106 opened this issue Jun 4, 2019 · 9 comments · Fixed by #70939
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@qwerty19106
Copy link

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?

@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 4, 2019
@jonas-schievink
Copy link
Contributor

Not sure where you see the ICE, removing that.

@jonas-schievink jonas-schievink removed the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Jun 4, 2019
@jonas-schievink jonas-schievink changed the title Const-generic ICE: #[repr(usize)] enum as const paremeter in arrays Const-generic type mismatch: #[repr(usize)] enum as const paremeter in arrays Jun 4, 2019
@qwerty19106
Copy link
Author

This is glitch after the previous 2 issues.. It is not ICE.

@qwerty19106
Copy link
Author

To @varkor

Minimal code:

#![feature(const_generics)]

pub struct MyArray<const COUNT: usize>([u8; COUNT + 1]);

impl<const COUNT: usize> MyArray<{COUNT}> {
    fn inner(&self) -> &[u8; COUNT + 1] {
        &self.0
    }
}

fn main() {
}

produce error:

error[E0308]: mismatched types
 --> src/main.rs:8:9
  |
8 |         &self.0
  |         ^^^^^^^ expected `COUNT + 1`, found `COUNT + 1`
  |
  = note: expected type `&[u8; _]`
             found type `&[u8; _]`

The {COUNT} + 1 variant also give this error.

playground

@Centril Centril added F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. labels Aug 6, 2019
@qwerty19106
Copy link
Author

Now it is ICE:

error: internal compiler error: src/librustc/ty/subst.rs:651: const parameter `COUNT/#0` (Const { ty: usize, val: Param(COUNT/#0) }/0) out of range when substituting substs=[]

@varkor varkor added the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Oct 24, 2019
@varkor
Copy link
Member

varkor commented Jan 6, 2020

It's back to the original error.

@varkor varkor removed the I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ label Jan 6, 2020
@qwerty19106
Copy link
Author

Now it produce this errors:

error: constant expression depends on a generic parameter
 --> src/main.rs:3:40
  |
3 | pub struct MyArray<const COUNT: usize>([u8; COUNT + 1]);
  |                                        ^^^^^^^^^^^^^^^
  |
  = note: this may fail depending on what value the parameter takes

error: constant expression depends on a generic parameter
 --> src/main.rs:6:5
  |
6 | /     fn inner(&self) -> &[u8; COUNT + 1] {
7 | |         &self.0
8 | |     }
  | |_____^
  |
  = note: this may fail depending on what value the parameter takes

@varkor varkor changed the title Const-generic type mismatch: #[repr(usize)] enum as const paremeter in arrays Const-generic type mismatch: #[repr(usize)] enum as const parameter in arrays Apr 8, 2020
@varkor
Copy link
Member

varkor commented Apr 8, 2020

The behaviour is now as expected. We just need a test now.

@varkor varkor added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Apr 8, 2020
varkor added a commit to varkor/rust that referenced this issue Apr 9, 2020
@bors bors closed this as completed in 7944f39 Apr 9, 2020
@qwerty19106
Copy link
Author

The behaviour is now as expected. We just need a test now.

Const Generic RFC leads the code as possible:

fn foo<const N: usize>() -> [i32; N + 1] {
    let x: [i32; N + 1] = [0; N + 1];
    x
}

Is there another issue that is tracking it?

@varkor
Copy link
Member

varkor commented Apr 10, 2020

Yes, there are some subtle issues about making sure that constants in types are well-formed. The tracking issue for this is #68436.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants