Skip to content

Compiler unable to apply trait bound #57905

Open
@grantslatton

Description

@grantslatton

Sorry for the vague title, please change. I have found a strange bug that is at the intersection of associated types, trait type parameters, and method type parameters.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=8584b2c75acb51a8c6082b668649ad29

trait Example {
    type Foo;
    
    fn foo<T: AsRef<Self::Foo>>(&self, t: T);
}

impl <A, B> Example for (A, B) where
    A: Iterator<Item = u32>,
    B: AsRef<A::Item>,
{
    type Foo = ();
    
    fn foo<T: AsRef<Self::Foo>>(&self, t: T) {}

}

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `B: std::convert::AsRef<u32>` is not satisfied
  --> src/main.rs:14:5
   |
14 |     fn foo<T: AsRef<Self::Foo>>(&self, t: T) {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::AsRef<u32>` is not implemented for `B`
   |
   = help: consider adding a `where B: std::convert::AsRef<u32>` bound
   = note: required because of the requirements on the impl of `Example` for `(A, B)`

error[E0277]: the trait bound `T: std::convert::AsRef<()>` is not satisfied
  --> src/main.rs:14:5
   |
14 |     fn foo<T: AsRef<Self::Foo>>(&self, t: T) {}
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::convert::AsRef<()>` is not implemented for `T`
   |
   = help: consider adding a `where T: std::convert::AsRef<()>` bound
   = note: required by `std::convert::AsRef`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `playground`.

Commenting out line 10 B: AsRef<A::Item> makes the program compile, or changing it to B: AsRef<u32> makes it compile (even though A::Item is a u32).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemA-type-systemArea: Type systemT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.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