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
It is the same as #61368, but not ICE.
Here are more cases where that error occurs:
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.
It looks like this error is currently intentional (added in https://github.com/rust-lang/rust/pull/68388), and needs some language design work to be fixed properly, which is tracked at https://github.com/rust-lang/rust/issues/68436, so closing in favor of that.
Most helpful comment
It looks like this error is currently intentional (added in https://github.com/rust-lang/rust/pull/68388), and needs some language design work to be fixed properly, which is tracked at https://github.com/rust-lang/rust/issues/68436, so closing in favor of that.