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 generics: Incorrect error "wrong number of const arguments" with struct constructor #61346

Closed
andreytkachenko opened this issue May 30, 2019 · 5 comments · Fixed by #61570
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@andreytkachenko
Copy link

this works fine:

#![feature(const_generics)]
pub struct Array<T, const N: usize> ([T; {N}]);
impl <T, const N: usize> Array<T, {N}> {
    #[inline]
    pub fn cap(&self) -> usize {
        {N}
    }
}

fn main() {
    let arr = Array([0u32; 8]);

    println!("{}", arr.cap());
}

but this fails to compile:

#![feature(const_generics)]
pub struct Array<T, const N: usize> {
    data: [T; {N}]
}
impl <T, const N: usize> Array<T, {N}> {
    #[inline]
    pub fn cap(&self) -> usize {
        {N}
    }
}

fn main() {
    let arr = Array { data: [0u32; 8] };

    println!("{}", arr.cap());
}

here is the error:

error[E0107]: wrong number of const arguments: expected 1, found 0
  --> src/main.rs:28:15
   |
28 |     let arr = Array { data: [0u32; 8] };
   |               ^^^^^ expected 1 const argument

error: aborting due to previous error
@Centril
Copy link
Contributor

Centril commented May 30, 2019

Reduced:

#![feature(const_generics)]

pub struct Array<T, const N: usize> {
    data: [T; {N}]
}

fn main() {
    let _ = Array { data: [0u32; 8] };
}

@Centril Centril added A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 30, 2019
@Centril Centril changed the title Error with struct constructor Const generics: Incorrect error "wrong number of const arguments" with struct constructor May 30, 2019
@Centril Centril added the A-inference Area: Type inference label May 30, 2019
@Centril
Copy link
Contributor

Centril commented May 30, 2019

cc @varkor @yodaldevoid

@varkor
Copy link
Member

varkor commented May 31, 2019

I wonder whether the right fix is removing:

let infer_consts = position != GenericArgPosition::Type && arg_counts.consts == 0;

and just using infer_types instead. However, this leads to a different error:

error: internal compiler error: src/librustc_codegen_llvm/context.rs:851: failed to get layout for `[type error]`: the type `[type error]` has an unknown layout

which might be "more correct", but is an ICE. However, making this change does fix #60724.

@varkor varkor self-assigned this May 31, 2019
@varkor
Copy link
Member

varkor commented Jun 5, 2019

Interestingly, the following works.

#![feature(const_generics)]

pub struct Array<T, const N: usize>([T; N]);

fn main() {
    let _ = Array([0u32; 8]);
}

So it's not a problem with tuple structs.

@varkor
Copy link
Member

varkor commented Jun 5, 2019

Additionally, this works.

#![feature(const_generics)]

pub struct Array<T, const N: usize> { x: [T; N] }

fn main() {
    let _ = Array::<u32, 8> { x: [0u32; 8] };
}

So there's a problem with the inference.

Centril added a commit to Centril/rust that referenced this issue Jun 10, 2019
Centril added a commit to Centril/rust that referenced this issue Jun 10, 2019
Centril added a commit to Centril/rust that referenced this issue Jun 10, 2019
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) A-inference Area: Type inference C-bug Category: This is a bug. 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.

3 participants