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

Unexpected error in const generics: constant expression depends on a generic parameter #68630

Closed
qwerty19106 opened this issue Jan 29, 2020 · 2 comments
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

@qwerty19106
Copy link

qwerty19106 commented Jan 29, 2020

The code:

#![feature(const_generics)]

struct Stack<const N: usize> {
    stack: [u64; {N / 8}],
}

must be compiled but gives error:

error: constant expression depends on a generic parameter
 --> src/lib.rs:4:5
  |
4 |     stack: [u64; {N / 8}],
  |     ^^^^^^^^^^^^^^^^^^^
  |
  = note: this may fail depending on what value the parameter takes

note: rustc 1.42.0-nightly (8a79d08fa 2020-01-27) running on x86_64-unknown-linux-gnu

playground

It is the same as #61368, but not ICE.

@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 29, 2020
@Evrey
Copy link

Evrey commented Feb 13, 2020

Here are more cases where that error occurs:

direct link to playground

Test snippet as I don't know for how long the playground will keep the code.
// (╯°□°)╯︵ ┻┻

/*
error: constant expression depends on a generic parameter
   […snip…]
   = note: this may fail depending on what value the parameter takes
*/

#![feature(const_generics)]
#![allow(incomplete_features)]



const fn gud_gaemu() -> usize {
    0xFF7_600D
}



// Example showing a configurable type that takes
// const generics via a struct.
#[derive(Eq, PartialEq)]
struct Magic {
    pub fairy_dust: usize,
}

struct X<const N: Magic> {
    arr: [u32; N.fairy_dust],
}



// Example showing a configurable type that takes
// const generics via a trait.
trait Nightmare {
    const SCAWWY: usize;
}

impl Nightmare for () {
    const SCAWWY: usize = gud_gaemu();
}

struct Y<T: Nightmare> {
    arr: [u32; <T as Nightmare>::SCAWWY],
    _but_i_am_using_t_indirectly: std::marker::PhantomData<T>,
}



// Example showing a configurable type that takes
// bare const generics.
//
// This is the only one that works!
struct Z<const N: usize> {
    arr: [u32; N],
}



// Example showing a configurable type that takes
// const generics via a tuple.
struct W<const N: (usize,)> {
    arr: [u32; N.0],
}



fn main() {
    const M: Magic = Magic { fairy_dust: gud_gaemu() };
    println!("{}B", std::mem::size_of::<X<{           M  }>>());
    println!("{}B", std::mem::size_of::<Y<           ()   >>());
    println!("{}B", std::mem::size_of::<Z<{ gud_gaemu()  }>>());
    println!("{}B", std::mem::size_of::<W<{(gud_gaemu(),)}>>());
}

I'm happy to see this issue being tagged as C-bug. Would've been nice to mark that error message as TODO or something. I feared that to be a permanent anti-feature.

@jonas-schievink
Copy link
Contributor

It looks like this error is currently intentional (added in #68388), and needs some language design work to be fixed properly, which is tracked at #68436, so closing in favor of that.

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

No branches or pull requests

3 participants