Skip to content

Unable to put trait bound on associated type in generic method in trait with its own associated type #116377

Closed
@jarimayenburg

Description

@jarimayenburg

I'm trying to get the following to compile.

pub trait MyTryFrom<T> {
    type Error;
}

pub trait Foo {
    type FooType;

    fn foo<E>(x: Self::FooType) -> E
    where
        E: MyTryFrom<Self::FooType>,
        E::Error: Sized;
}

struct MyFoo;

impl Foo for MyFoo {
    type FooType = MyFooType;

    fn foo<E>(x: Self::FooType) -> E
    where
        E: MyTryFrom<Self::FooType>,
        E::Error: Sized,
    {
        todo!()
    }
}

struct MyFooType;

This is a minimal example that illustrates the issue. The compiler gives me the following error, asking me to further restrict E with the exact same bound.

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `E: MyTryFrom<MyFooType>` is not satisfied
  --> src/lib.rs:19:5
   |
19 | /     fn foo<E>(x: Self::FooType) -> E
20 | |     where
21 | |         E: MyTryFrom<Self::FooType>,
22 | |         E::Error: Sized,
   | |________________________^ the trait `MyTryFrom<MyFooType>` is not implemented for `E`
   |
help: consider further restricting this bound
   |
21 |         E: MyTryFrom<Self::FooType> + MyTryFrom<MyFooType>,
   |                                     ++++++++++++++++++++++

error[E0277]: the trait bound `E: MyTryFrom<MyFooType>` is not satisfied
  --> src/lib.rs:19:8
   |
19 |     fn foo<E>(x: Self::FooType) -> E
   |        ^^^ the trait `MyTryFrom<MyFooType>` is not implemented for `E`
   |
help: consider further restricting this bound
   |
21 |         E: MyTryFrom<Self::FooType> + MyTryFrom<MyFooType>,
   |                                     ++++++++++++++++++++++

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 2 previous errors

This snippet compiles just fine if I remove the E::Error: Sized bound.

Meta

I've tried this on stable (1.72.1) and nightly (1.75.0-nightly)

rustc --version --verbose:

rustc 1.72.1 (d5c2e9c34 2023-09-13)
binary: rustc
commit-hash: d5c2e9c342b358556da91d61ed4133f6f50fc0c3
commit-date: 2023-09-13
host: x86_64-unknown-linux-gnu
release: 1.72.1
LLVM version: 16.0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-trait-systemArea: Trait systemC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions