Skip to content

variant_count intrinsic is evaluated incorrectly in generic code #79137

Closed
@tmiasko

Description

@tmiasko

For example:

#![feature(variant_count)]

fn a<T>() -> usize {
    let v = [0u8; std::mem::variant_count::<T>()];
    std::mem::size_of_val(&v)
}

fn b() -> usize {
    let v = [0u8; std::mem::variant_count::<Result<(), ()>>()];
    std::mem::size_of_val(&v)
}

fn main() {
    println!("{}", a::<Result<(), ()>>());
    println!("{}", b());
}
$ rustc a.rs
warning: cannot use constants which depend on generic parameters in types
 --> a.rs:4:19
  |
4 |     let v = [0u8; std::mem::variant_count::<T>()];
  |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(const_evaluatable_unchecked)]` on by default
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: for more information, see issue #76200 <https://github.com/rust-lang/rust/issues/76200>

warning: 1 warning emitted

$ ./a
0
2

Similar to #73976, but with a different intrinsic.

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.F-variant_count`#![feature(variant_count)]`I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/Soundnessrequires-nightlyThis issue requires a nightly compiler in some way.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions