rust-lang / const-eval

home for proposals in and around compile-time function evaluation
Apache License 2.0
105 stars 17 forks source link

Use case: const bitflags #47

Closed joshtriplett closed 4 years ago

joshtriplett commented 4 years ago

I'd like to be able to use bitflags as the value of a const:

const MY_FLAGS: SomeFlags = SomeFlags::A | SomeFlags::B;

This produces an error saying that calls in constants are limited to constant functions, tuple structs and tuple variants.

What would be the blockers for making bitflags operations work as the value of a const?

jplatte commented 4 years ago

As written, this requires const fn in traits (https://github.com/rust-lang/rfcs/pull/2632). There is an implementation based on that at https://github.com/bitflags/bitflags/pull/217.

As a workaround, this could be done on stable today with const inherent methods instead of operators:

const MY_FLAGS: SomeFlags = SomeFlags::A.or(SomeFlags::B);
joshtriplett commented 4 years ago

@jplatte .or looks reasonable to me; thanks! I'll inquire about that on bitflags.

In the meantime, if there's already an issue tracking const handling in traits, feel free to close this in favor of such an issue. Might be worth noting this as a use case in the skill-tree graph, though.

RalfJung commented 4 years ago

There's no tracking issue yet because the RFC has not been finalized yet. Closing in favor of https://github.com/rust-lang/rfcs/pull/2632.