I tried this code:
trait RandomTrait
{
type Item;
}
struct BoolWrapper<const VAL : bool>;
impl RandomTrait for BoolWrapper<true> { type Item = String; }
impl RandomTrait for BoolWrapper<false> { type Item = i32; }
// RandomTrait is now implemented for all possible values
// of the constant generic parameter of BoolWrapper. There
// exists no bool X for which BoolWrapper<X> does not
// implement RandomTrait.
//
// And yet...
trait BoolWrapperTypeConstructor
{
type Of<const VAL : bool> : RandomTrait;
}
struct BoolWrapperFamily;
impl BoolWrapperTypeConstructor for BoolWrapperFamily {
type Of<const VAL : bool> = BoolWrapper<VAL>;
}
I expected the code to compile, as RandomTrait is implemented for all 'flavors' of BoolWrapper.
Instead, the trait solver does not recognize that true and false are the only possible values a bool can have, and assumes that RandomTrait is not implemented for all possible types constructable by BoolWrapper.
This surprises me since a match expression can, in fact, recognize at compile-time whether all possible variants/values of a type are handled, in even more sophisticated ways than possible with const generics.
Meta
rustc --version --verbose:
rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: x86_64-pc-windows-msvc
release: 1.86.0
LLVM version: 19.1.7
I tried this code:
I expected the code to compile, as
RandomTraitis implemented for all 'flavors' ofBoolWrapper.Instead, the trait solver does not recognize that
trueandfalseare the only possible values aboolcan have, and assumes thatRandomTraitis not implemented for all possible types constructable byBoolWrapper.This surprises me since a match expression can, in fact, recognize at compile-time whether all possible variants/values of a type are handled, in even more sophisticated ways than possible with const generics.
Meta
rustc --version --verbose: