-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
enum variants aren't considered to be const during const-evaluation #23898
Comments
(copied labels from #5873.) |
rustc doesn’t yet realize that enum variants without explicit discriminators are constant enough to be used as patterns in a match statement (rust-lang/rust#23898). This happens to be what #[derive(FromPrimitive)] does, anyway. Signed-off-by: Anders Kaseorg <[email protected]>
This also seems to affect const fn. |
Would like to throw in my pennies and say that this makes doing a 1:1 port of C code into Rust (an otherwise relatively easy process) slightly annoying when the C code in question uses this trick. I have to calculate the length of the enum, define that as a const of the same name, and update it every time to the enum changes. |
For what it's worth, this does "work" in Rust 1.14 if you give values to the variants: use self::Category::*;
#[derive(Debug)]
enum Category {
A = 0,
B = 1,
C = 2,
NumCategories = 3,
}
const NUM_CATEGORIES: usize = NumCategories as usize;
fn main() {
let the_categories: [Category; NUM_CATEGORIES] = [A, B, C];
println!("{:?}", the_categories);
} However, it continues to fail if you cross the crate boundary (an example is available in #38875) |
It does seem odd and less-than-intuitive that implicit values and explicit values are treated differently, and that intra-crate and inter-crate are treated differently. |
rustc: generalize monomorphic_const_eval to polymorphic constants. With the addition of `Substs` to the query key, we can now evaluate *and cache* polymorphic constants. Fixes #23898 by replacing the crippled explicit-discriminant-only local-crate-only `lookup_variant_by_id` with `ConstVal::Variant` which can describe variants irrespective of their discriminant. Fixes #41394 by fixing #23898 (for the original testcase) and by not looping past the first discriminant.
Spawned off of #5873
playpen
The text was updated successfully, but these errors were encountered: