rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
98.76k stars 12.76k forks source link

Unexpected error when resolving bounds involving associated types #133314

Open klkvr opened 17 hours ago

klkvr commented 17 hours ago

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