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

Error: constant expression depends on a generic parameter #73899

Closed
est31 opened this issue Jun 30, 2020 · 5 comments · Fixed by #79302
Closed

Error: constant expression depends on a generic parameter #73899

est31 opened this issue Jun 30, 2020 · 5 comments · Fixed by #79302
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)]` F-generic_const_exprs `#![feature(generic_const_exprs)]` 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

@est31
Copy link
Member

est31 commented Jun 30, 2020

Successor of #61935 and #61383.

This code:

#![feature(const_generics)]
trait Foo {}

impl<const N: usize> Foo for [(); N]
where
    Self:FooImpl<{N==0}>
{}

trait FooImpl<const IS_ZERO: bool>{}

impl FooImpl<{0u8==0u8}> for [();0] {}

impl<const N:usize> FooImpl<{0u8!=0u8}> for [();N] {}

fn foo<T: Foo>(v: T) {}

fn main() {
    foo([]);
    foo([()]);
}

Gives this error message (on nightly):

error: constant expression depends on a generic parameter
 --> src/main.rs:6:10
  |
6 |     Self:FooImpl<{N==0}>
  |          ^^^^^^^^^^^^^^^
  |
  = note: this may fail depending on what value the parameter takes

It'd be great if this could work so that you can implement a trait differently for different values of type parameters.

cc @lcnr who suggested to open a new issue for this.

@est31 est31 added the C-bug Category: This is a bug. label Jun 30, 2020
@jonas-schievink jonas-schievink added A-const-generics Area: const generics (parameters and arguments) F-const_generics `#![feature(const_generics)]` requires-nightly This issue requires a nightly compiler in some way. labels Jun 30, 2020
@lcnr
Copy link
Contributor

lcnr commented Jun 30, 2020

background: his error was introduced in #70107 as the previous state was unsound, see #70107 (comment):

unfortunately, without this check, const generic would be unsound. The problem is really that const generics is incomplete and we're missing some critical components that are necessary to get them working properly. Const generics is unstable ("very unstable" in some sense in that it explicitly warns you things may break if you try to use them at the moment). We're grateful to have people testing them so that we can address the flaws before stabilising the feature, but we haven't yet reached the point where we can guarantee any kind of stability. Hopefully that will change before too long :)

@JohnTitor JohnTitor added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 5, 2020
@lcnr lcnr added E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-generic_const_exprs `#![feature(generic_const_exprs)]` labels Nov 22, 2020
@lcnr
Copy link
Contributor

lcnr commented Nov 22, 2020

With feature(const_evaluatable_checked) this test is now working as expected.

Marking as E-needs-test.

@est31
Copy link
Member Author

est31 commented Nov 22, 2020

@lcnr if I try this:

#![feature(const_evaluatable_checked, min_const_generics)]
trait Foo {}

impl<const N: usize> Foo for [(); N]
where
    Self:FooImpl<{N==0}>
{}

trait FooImpl<const IS_ZERO: bool>{}

impl FooImpl<{0u8==0u8}> for [();0] {}

impl<const N:usize> FooImpl<{0u8!=0u8}> for [();N] {}

fn foo<T: Foo>(v: T) {}

fn main() {
    foo([]);
    foo([()]);
}

I get this error:

error: generic parameters may not be used in const operations
 --> src/main.rs:6:19
  |
6 |     Self:FooImpl<{N==0}>
  |                   ^ cannot perform const operation using `N`
  |
  = help: const parameters may only be used as standalone arguments, i.e. `N`

Using the nightly from playground 1.50.0-nightly (2020-11-21 da384694807172f0ca40)

@lcnr
Copy link
Contributor

lcnr commented Nov 22, 2020

you need full const_generics for this, we should probably add a note here which recommends using feature(const_generics) and feature(const_evaluatable_checked) here.

@est31
Copy link
Member Author

est31 commented Nov 22, 2020

@lcnr alright that works. I'll file a PR

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)]` F-generic_const_exprs `#![feature(generic_const_exprs)]` 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