rust-lang / rust-lang/rust

Coherence rejects otherwise valid impl

Open
#147,554 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-associated-items A-coherence C-bug T-compiler T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the two-crate example from src/lib.rs with rustc 1.92.0-nightly, then compare the failing associated-type impl with the equivalent impl using TableA and TableB. The issue is done when the restricted impl is accepted without weakening the reported orphan-rule checks, with the reproduction covered by an appropriate compiler test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.