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 unconstrained generics #68366

Closed
DutchGhost opened this issue Jan 19, 2020 · 3 comments · Fixed by #76401
Closed

Const generics unconstrained generics #68366

DutchGhost opened this issue Jan 19, 2020 · 3 comments · Fixed by #76401
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

@DutchGhost
Copy link
Contributor

The following code does NOT compile:

#![feature(const_generics)]

struct Collatz<const N: Option<usize>>;

impl <const N: usize> Collatz<{Some(N)}> {
    
}

fn main() {
}

it errors with:

error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
 --> src/main.rs:5:13
  |
5 | impl <const N: usize> Collatz<{Some(N)}> {
  |             ^ unconstrained const parameter

However, when using normal generics in the places of the const generics, wrapping the T in an Option and sticking that into the Collatz struct does work:

struct Collatz<T>([T; 0]);

impl <T> Collatz<Option<T>> {}

Is it intended that const generic dont mimmic this behaviour?

@Centril Centril added F-const_generics `#![feature(const_generics)]` C-bug Category: This is a bug. 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 19, 2020
@jonas-schievink jonas-schievink added the A-const-generics Area: const generics (parameters and arguments) label Jan 19, 2020
@varkor
Copy link
Member

varkor commented Jan 20, 2020

Is it intended that const generic dont mimmic this behaviour?

This is probably a decision that has to be made (although if it is disallowed, then we'd want a better error message). However, at present, this is a implementation limitation: we really only properly support const generics of the form Foo<N> at the moment (cc ##60471).

@lcnr
Copy link
Contributor

lcnr commented Aug 11, 2020

Considering that anon consts are mostly just projections, wouldn't this be similar to

#![feature(const_generics)]

struct Collatz<T>(T);

trait Foo {
    type Assoc;
}

impl<T: Foo> Collatz<T::Assoc> {}

fn main() {}

which doesn't really make a lot of sense (we have to guarantee that the projection maps each value of T to a unique type).

While Some(N) is injective, finding an intuitive subset of expressions for which this is the case is hard and imo not actually desirable rn.

Not quite sure what's the best error message though 🤔

probably something like

error[E0207]: the const parameter `N` is not constrained by the impl trait, self type, or predicates
 --> src/main.rs:5:13
  |
5 | impl <const N: usize> Collatz<{Some(N)}> {
  |             ^ unconstrained const parameter
note: const parameters can be constrained by using them in a trivial expression

@varkor
Copy link
Member

varkor commented Aug 12, 2020

That's a good point @lcnr. I think adding a note explaining the problem would be the best solution, though the tricky part is working out how to explain it concisely (rather than just stating what the problem is).

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
Development

Successfully merging a pull request may close this issue.

5 participants