Skip to content

Erroneous compilation failure with associated constant #54822

@joshlf

Description

@joshlf

EDIT: To anyone coming to this from the This Week in Rust CFP: This is currently an unsolved issue, so the first step to tackling this is to figure out where the bug is! That alone would be a huge help. If you want to implement a fix, that'd be great too :)

=====

The following program should compile on stable, but doesn't:

trait ConstDefault {
    const Default: Self;
}

trait Foo: Sized {}

trait FooExt: Foo {
    type T: ConstDefault;
}

trait Bar<F: FooExt> {
    const T: F::T;
}

impl<F: FooExt> Bar<F> for () {
    const T: F::T = <F::T as ConstDefault>::Default;
}

It fails to compile with the following error:

error[E0277]: the trait bound `F: FooExt` is not satisfied
  --> src/lib.rs:16:5
   |
16 |     const T: F::T = <F::T as ConstDefault>::Default;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `FooExt` is not implemented for `F`
   |
   = help: consider adding a `where F: FooExt` bound

Note that if we amend as either of the following, it works (credit to @ezrosent for figuring this out):

trait Bar<T: ConstDefault, F: FooExt<T=T>> {
    const C: T;
}

impl<T: ConstDefault, F: FooExt<T=T>> Bar<T, F> for () {
    const C: T = F::T::Default;
    const C: T = T::Default;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemT-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