Skip to content

Trait solver fails to see trait is implemented for all values of const generic param #145133

@Keaudes

Description

@Keaudes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-trait-systemArea: Trait systemC-feature-requestCategory: A feature request, i.e: not implemented / a PR.T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions