Skip to content

Coherence rejects otherwise valid impl #147554

@weiznich

Description

@weiznich

I tried this code:

// in crate a
pub trait Table {}
pub trait IsTable {
    type Table: Table;
}

pub struct Wrapper<'a, T>(&'a T);
impl<'a, T> IsTable for Wrapper<'a, T> where T: Table {
    type Table = T;
}

pub struct Only<T>(T);

impl<T> Table for Only<T> where T: Table {}
pub trait TableNotEqual<T: Table>: Table {}

// in crate b

struct TableA;
struct TableB;

impl Table for TableA {}
impl Table for TableB {}

impl TableNotEqual<<Wrapper<'static, TableA> as IsTable>::Table>
    for Only<<Wrapper<'static, TableB> as IsTable>::Table>
where
    for<'a> Wrapper<'a, TableA>: IsTable<Table = TableA>,
    for<'a> Wrapper<'a, TableB>: IsTable<Table = TableB>,
{
}

I expected to see this happen: Code compiles without error
Instead, this happened: Compilation fails with the following error:

error[E0117]: only traits defined in the current crate can be implemented for types defined outside of the crate
  --> src/lib.rs:9:1
   |
 9 |   impl TableNotEqual<<Wrapper<'static, TableA> as IsTable>::Table>
   |   ^    ----------------------------------------------------------- `<crate_a::Wrapper<'_, TableA> as crate_a::IsTable>::Table` is not defined in the current crate
   |  _|
   | |
10 | |     for Only<<Wrapper<'static, TableB> as IsTable>::Table>
   | |         -------------------------------------------------- `crate_a::Only` is not defined in the current crate
11 | | where
12 | |     for<'a> Wrapper<'a, TableA>: IsTable<Table = TableA>,
13 | |     for<'a> Wrapper<'a, TableB>: IsTable<Table = TableB>,
   | |_________________________________________________________^
   |
   = note: impl doesn't have any local type before any uncovered type parameters
   = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
   = note: define and implement a trait or new type instead

The impl is restricted in such a way that the Table types used in the definition are local types. The equivalent impl impl TableNotEqual<TableA> for Only<TableB> {} compiles without error.

Meta

rustc --version --verbose:

rustc 1.92.0-nightly (b925a865e 2025-10-09)
binary: rustc
commit-hash: b925a865e2c9a0aefe5a2877863cb4df796f2eaf
commit-date: 2025-10-09
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2

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