Skip to content

Type parameter bounded as associated type does not inherit associated type bounds #86284

Closed
@sharnoff

Description

@sharnoff

My apologies in advance if this is a duplicate or a known limitation. I spent a long while looking and it appears to be similar to #24159 (resolved by #81671), but not quite the same (and this behavior is still present).

Rustc version

Both current stable (1.52.1) and nightly (1.55.0-nightly (2021-06-12 24bdc6d)) fail to compile the following code.

Minimal example

(playground link)

trait Foo {
    // There isn't anything special about `Clone` here; it just happens to be
    // a trait we can bound this by.
    type Bar: Clone;
}

// Because `<F as Foo>::Bar = B`, and `Foo::Bar: Clone`, we'd expect that this
// also requires `B: Clone`.
fn do_thing<F, B>(bar: B) where F: Foo<Bar = B> {
    // rustc says that `B` does not implement clone, so there is no `.clone()`
    // method we can call here.
    bar.clone();
}

I used a function to create the bound conflict here, but it works in a variety of other contexts as well -- I first noticed it in an impl block with the call to clone in one of the methods.

Essentially, the issue seems to be that the compiler is failing to make the following deduction:

trait Foo with Foo::Bar: MyTrait and T: Foo<Bar = B> implies B: MyTrait

If there's anything I can do, I'm happy to help where possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions