Skip to content

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

Closed

Description

@DrGabble

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.

Activity

  1. hellow554 commented on Jan 15, 2020

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

    works

  2. added
    A-const-genericsArea: const generics (parameters and arguments)
    C-bugCategory: This is a bug.
    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. When possible, use a F-* label instead.
    on Jan 15, 2020
  3. DrGabble commented on Jan 15, 2020

    @DrGabble
    Author
  4. changed the title [-]Const Generics Not Compiling[/-] [+]Cannot use generic const argument without `{}`, despite no conflict[/+] on Jan 15, 2020
  5. varkor commented on Jan 15, 2020

    @varkor
    Contributor

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

  6. DrGabble commented on Jan 17, 2020

    @DrGabble
    Author

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

  7. yodaldevoid commented on Jan 17, 2020

    @yodaldevoid
    Contributor

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

  8. yodaldevoid commented on Jan 19, 2020

    @yodaldevoid
    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.

  9. varkor commented on Jan 20, 2020

    @varkor
    Contributor

    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.

  10. self-assigned this
    on Jan 20, 2020
  11. added a commit that references this issue on Feb 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-const-genericsArea: const generics (parameters and arguments)C-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.requires-nightlyThis issue requires a nightly compiler in some way. When possible, use a F-* label instead.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions