Skip to content

Cannot use associated type as type of associated const #46969

Closed

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).

Activity

  1. added
    A-associated-itemsArea: Associated items (types, constants & functions)
    C-bugCategory: This is a bug.
    on Dec 23, 2017
  2. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    A-diagnosticsArea: Messages for errors, warnings, and lints
    on Jan 23, 2018
  3. oberien commented on Mar 10, 2018

    @oberien
    Contributor

    This exact issue just occured to @CryZe and me when trying to use associated constants to create a constant struct in a library with user-provided data (without const fns) (actual use case: playground). A minimized examples is this (playground):

    use std::marker::PhantomData;
    
    trait Const {
        type T;
        const VAL: Self::T;
    }
    
    struct MakeTuple<U>(PhantomData<U>);
    impl<U: Const + Sized> Const for MakeTuple<U> {
        type T = (U::T, U::T);
        const VAL: Self::T = (U::VAL, U::VAL);
    }

    Error:

    error[E0277]: the trait bound `U: std::marker::Sized` is not satisfied
      --> src/main.rs:11:5
       |
    11 |     const VAL: Self::T = (U::VAL, U::VAL);
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `U` does not have a constant size known at compile-time
       |
       = help: the trait `std::marker::Sized` is not implemented for `U`
       = help: consider adding a `where U: std::marker::Sized` bound
       = note: required because of the requirements on the impl of `Const` for `MakeTuple<U>`
    
    error[E0277]: the trait bound `U: Const` is not satisfied
      --> src/main.rs:11:5
       |
    11 |     const VAL: Self::T = (U::VAL, U::VAL);
       |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Const` is not implemented for `U`
       |
       = help: consider adding a `where U: Const` bound
       = note: required because of the requirements on the impl of `Const` for `MakeTuple<U>`

    The interesting thing is that even though both bounds are specified, the compiler doesn't seem to take them into account.

    Edit: The issue is not using a trait with an associated type and const as generic bound. Instead the problem is implementing a trait with an associated type and const in general: playground

  4. bgeron commented on May 19, 2018

    @bgeron

    I ran into the same problem. It's also in 1.26 stable.

    Here's a further minimized example (play):

    trait EmptyTrait {}
    
    trait Const {
        type T;
        const VAL: Self::T;
    }
    
    impl<U: EmptyTrait + std::marker::Sized> Const for U
    {
        type T = u32;
        const VAL: u32 = 5;
    }

    We can also use the type parameter in the trait rather than the type (play):

    trait EmptyTrait {}
    
    trait Const<U> {
        type T;
        const VAL: Self::T;
    }
    
    impl<U: EmptyTrait + Sized> Const<U> for () {
        type T = u32;
        const VAL: Self::T = 5;
    }

    Both code fragments give an error saying that in the blanket impl, Rust cannot figure out that U implements EmptyTrait and Sized:

    error[E0277]: the trait bound `U: std::marker::Sized` is not satisfied
      --> src/lib.rs:12:5
       |
    12 |     const VAL: Self::T = 5;
       |     ^^^^^^^^^^^^^^^^^^^^^^^ `U` does not have a constant size known at compile-time
       |
       = help: the trait `std::marker::Sized` is not implemented for `U`
       = help: consider adding a `where U: std::marker::Sized` bound
       = note: required because of the requirements on the impl of `Const<U>` for `()`
    
    error[E0277]: the trait bound `U: EmptyTrait` is not satisfied
      --> src/lib.rs:12:5
       |
    12 |     const VAL: Self::T = 5;
       |     ^^^^^^^^^^^^^^^^^^^^^^^ the trait `EmptyTrait` is not implemented for `U`
       |
       = help: consider adding a `where U: EmptyTrait` bound
       = note: required because of the requirements on the impl of `Const<U>` for `()`
    

    Note that in both cases, if you remove the EmptyTrait constraint on the quantified type U, the error about EmptyTrait goes away! This is very strange. However, like in #50824, the Sized error message stays.

    The problem goes away if you declare the type of VAL in Const to be u32, rather than some type that is computed potentially based on parameters.

    I'm not familiar with the compiler internals, but my hypothesis is that associated types and constants depending on a type parameter are not evaluated properly in the constant expression evaluator. In this case, it's associated types that do not reduce well: const VAL: Self::T = 5; forces Rust to do some fancy type of computation at compile time in order to type check, but there's a bug in the code for such computations.

    I feel there are a couple of different issues in the same spirit:

  5. CAD97 commented on Sep 2, 2018

    @CAD97
    Contributor

    Copying over my minimal example from #53908 as another data point (playground):

    use std::marker::PhantomData;
    
    pub trait FromPest: Sized {
        type Rule;
        const RULE: Self::Rule;
    }
    
    impl<T> FromPest for PhantomData<T>
    where
        T: FromPest,
    {
        type Rule = T::Rule;
        const RULE: T::Rule = T::RULE;
    }

    Here I've added a Sized supertrait to my trait to get rid of the Sized issue but it still can't determine the required trait implementation (of my trait in this case) to make this work.

  6. added a commit that references this issue on Sep 2, 2018
  7. estebank commented on Apr 29, 2019

    @estebank
    Contributor

    All of these cases now compile, except for the original report, which has a type error:

    error[E0308]: mismatched types
      --> src/lib.rs:11:21
       |
    11 |     const N: C::M = 4u8;
       |                     ^^^ expected associated type, found u8
       |
       = note: expected type `<C as O>::M`
                  found type `u8`
    

    Current output:

    error[E0308]: mismatched types
      --> src/lib.rs:11:21
       |
    11 |     const N: C::M = 4u8;
       |                     ^^^ expected associated type, found u8
       |
       = note: expected type `<C as O>::M`
                  found type `u8`
       = note: consider constraining the associated type `<C as O>::M` to `u8` or calling a method that returns `<C as O>::M`
       = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
    

    The correct code would be

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

    The outstanding work would be providing a suggestion explaining what "consider constraining the associated type <C as O>::M to u8" means. This will be hard to do given the different cases that need to be accounted for, for the case above you need to move the C: O constraint to where, for example (although that's not needed, just nicer stylistically).

  8. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    P-lowLow priority
    and removed
    C-bugCategory: This is a bug.
    on Sep 26, 2019
  9. added
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    on Dec 13, 2019
  10. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    on Feb 16, 2020
  11. added 2 commits that reference this issue on Oct 17, 2023
    4b86ebf
    dee86bf
  12. added a commit that references this issue on Oct 18, 2023
    d69cdb2
  13. added a commit that references this issue on Oct 18, 2023
    f4f402a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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