rust-lang / rust-lang/rust

Unexpected error when resolving bounds involving associated types

Open
#133,314 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-impl-trait A-trait-system C-bug fixed-by-next-solver T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

I am trying to compile the following code

struct MyBlock;

trait Block {
    type Header;
}
impl Block for MyBlock {
    type Header = ();
}

trait Header {}
impl Header for () {}

trait FullBlock: Block<Header: Header> {}
impl<T> FullBlock for T where T: Block<Header: Header> {}

trait Primitives {
    type Block;
}

trait FullPrimitives: Primitives<Block: FullBlock<Header = Self::Header>> {
    type Header;
}

impl<T> FullPrimitives for T where T: Primitives<Block: FullBlock> {
    type Header = <<T as Primitives>::Block as Block>::Header;
}

fn test<P: FullPrimitives<Block = MyBlock>>() {}

The compilation is failing with:

error[E0283]: type annotations needed: cannot satisfy `MyBlock: FullBlock`
  --> src/main.rs:30:12
   |
30 | fn test<P: FullPrimitives<Block = MyBlock>>() {}
   |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: cannot satisfy `MyBlock: FullBlock`
note: required by a bound in `FullPrimitives`
  --> src/main.rs:22:41
   |
22 | trait FullPrimitives: Primitives<Block: FullBlock<Header = Self::Header>> {
   |                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `FullPrimitives`

For more information about this error, try `rustc --explain E0283`.
error: could not compile `my_project` (bin "my") due to 1 previous error

However, if I relax the FullBlock trait requirements it compiles fine

-trait FullBlock: Block<Header: Header> {}
-impl<T> FullBlock for T where T: Block<Header: Header> {}
+trait FullBlock: Block {}
+impl<T> FullBlock for T where T: Block {}
Meta

rustc --version --verbose:

rustc 1.84.0-nightly (c1db4dc24 2024-10-25)
binary: rustc
commit-hash: c1db4dc24267a707409c9bf2e67cf3c7323975c8
commit-date: 2024-10-25
host: aarch64-apple-darwin
release: 1.84.0-nightly
LLVM version: 19.1.1

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 example from src/main.rs with the reported nightly rustc, then compare it with the relaxed FullBlock bounds. Trace how the compiler handles the associated-type and nested trait bounds, and consider the original example fixed when it compiles without the erroneous E0283 diagnostic and has regression coverage.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.