Skip to content

Associated type projections don't count for local types in trait impls #87350

Closed
@alexcrichton

Description

@alexcrichton

I ran into a case with generated code today where I believe the compiler is giving a false error trying to verify a trait implementation.

Given a crate foo defined as:

// foo.rs
pub trait Foo {}

and then a consumer crate bar:

// bar.rs
struct A;

trait B {
    type C;
}

impl B for A {
    type C = MyType;
}

struct MyType;

impl foo::Foo for <A as B>::C {}

when compiled this yields:

$ rustc +nightly foo.rs --crate-type lib
$ rustc +nightly bar.rs --crate-type lib --extern foo=libfoo.rlib
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
  --> bar.rs:13:1
   |
13 | impl foo::Foo for <A as B>::C {}
   | ^^^^^^^^^^^^^^^^^^-----------
   | |                 |
   | |                 `<A as B>::C` is not defined in the current crate
   | impl doesn't use only types from inside the current crate
   |
   = note: define and implement a trait or new type instead

error: aborting due to previous error

For more information about this error, try `rustc --explain E0117`.

I don't believe that the error here is accurate since A, B, C, and MyType are all local to the crate and the compiler would ideally follow the projection to conclude that this trait implementation is valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)A-coherenceArea: CoherenceC-bugCategory: This is a bug.T-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