Skip to content

Cannot use associated type as type of associated const #46969

Closed
@kennytm

Description

@kennytm

Repro:

trait O {
    type M;
}
trait U<A: O> {
    const N: A::M;
}
impl<D> O for D {
    type M = u8;
}
impl<C: O> U<C> for u16 {
    const N: C::M = 4;
}

This caused E0277 "the trait bound is not satisfied" error:

error[E0277]: the trait bound `C: std::marker::Sized` is not satisfied
  --> src/main.rs:11:5
   |
11 |     const N: C::M = 4;
   |     ^^^^^^^^^^^^^^^^^^ `C` does not have a constant size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `C`
   = help: consider adding a `where C: std::marker::Sized` bound
   = note: required because of the requirements on the impl of `O` for `C`

The error doesn't make sense because C is obviously Sized. It seems the associated const type is evaluated before the generic bounds are added, causing this error.

Changing both A::M and C::M to u8 makes the error go away.


Reproducible on all 3 versions on playground (1.23.0, 1.24.0-beta, 1.25.0-nightly).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.P-lowLow priorityT-compilerRelevant to the compiler 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