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

Cannot use generic const argument without {}, despite no conflict #68257

Closed
DrGabble opened this issue Jan 15, 2020 · 7 comments · Fixed by #68434
Closed

Cannot use generic const argument without {}, despite no conflict #68257

DrGabble opened this issue Jan 15, 2020 · 7 comments · Fixed by #68434
Assignees
Labels
A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. 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

@DrGabble
Copy link

DrGabble commented Jan 15, 2020

I would expect this code to compile, but it does not:

#![feature(const_generics)]

struct Foo<const A: usize, const B: usize> {
    a: [usize; A],
    b: [usize; B],
}

impl<const A: usize> Foo<A, 1> {
    fn get_b(&self) -> usize {
        self.b[0]
    }
}

impl<const B: usize> Foo<1, B> {
    fn get_a(&self) -> usize {
        self.a[0]
    }
}


fn main() {
    let foo = Foo { a: [0, 3], b: [0] };
    dbg!(foo.get_b());
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error: type arguments must be declared prior to const arguments
  --> src/main.rs:14:29
   |
14 | impl<const B: usize> Foo<1, B> {
   |                             ^

error: aborting due to previous error

error: could not compile `playground`.

To learn more, run the command again with --verbose.

@hellow554
Copy link
Contributor

hellow554 commented Jan 15, 2020

impl<const B: usize> Foo<1, { B }>

works

@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) C-bug Category: This is a bug. F-const_generics `#![feature(const_generics)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. requires-nightly This issue requires a nightly compiler in some way. labels Jan 15, 2020
@DrGabble
Copy link
Author

DrGabble commented Jan 15, 2020 via email

@varkor varkor changed the title Const Generics Not Compiling Cannot use generic const argument without {}, despite no conflict Jan 15, 2020
@varkor
Copy link
Member

varkor commented Jan 15, 2020

I'm surprised this doesn't work, actually, as B shouldn't conflict with anything, so should be inferrable as a const. cc @yodaldevoid

@DrGabble
Copy link
Author

Yeah it confused be how it could figure it out the other way, but not this way round...

@yodaldevoid
Copy link
Contributor

Hmm, I'll try taking a poke at this this weekend.

@yodaldevoid
Copy link
Contributor

Looks like the check for if consts are before types in argument lists happens before we do our fancy lowering magic that turns consts args that had been parsed as type args into their true selves. Why wasn't this caught earlier? We just didn't test it.

As for how to fix it? We could just remove parameter ordering or at least slightly relax it. Otherwise, we might have to just move this check later on after things have been lowered to HIR.

@varkor
Copy link
Member

varkor commented Jan 20, 2020

I don't think there's a strong reason for parameter ordering to be enforced and was mainly intended to be conservative. However, it is a small language change, so it might be easier to move this check later on for now.

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. 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
5 participants