rust-lang / rust-lang/rust

Equality constraints break type resolution

Open
#134,483 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Imagine a trait for serialization (AsRaw). I'm trying to make a trait for wrappers that serialize into the same encoding as their inner type. I tried this:

trait AsRaw {
    type Raw;
}

trait WrapperTrait: AsRaw {
    type Inner: AsRaw<Raw = Self::Raw>;
}

#[repr(u32)]
enum Foo {
    Bar = 1,
    Baz = 2,
}

impl AsRaw for Foo {
    type Raw = u32;
}

fn noop1<P: WrapperTrait>(x: <P::Inner as AsRaw>::Raw) -> <P as AsRaw>::Raw {
    x
}

fn noop2<P: WrapperTrait<Inner = Foo>>(x: <P::Inner as AsRaw>::Raw) -> <P as AsRaw>::Raw {
    // ERROR: mismatched types
    x
}

fn noop3<P: WrapperTrait<Inner = Foo>>(x: <P::Inner as AsRaw>::Raw) -> <P as AsRaw>::Raw {
    noop1::<P>(x)
}

For some reason, noop2() fails to compile, even though noop1() (a strictly more general function) compiles successfully. Furthermore, noop3() (which has the same exact signature as noop2()) compiles successfully, taking advantage of noop1().

Providing a specific value for an associated type seems to cause the compiler to "forget" certain bounds on the associated type, at least while checking type equality.

Meta

rustc --version --verbose:

rustc 1.83.0 (90b35a623 2024-11-26)
binary: rustc
commit-hash: 90b35a6239c3d8bdabc530a6a0816f7ff89a0aaf
commit-date: 2024-11-26
host: aarch64-apple-darwin
release: 1.83.0
LLVM version: 19.1.1

Nightly has the same behavior.

Error message:

$ RUST_BACKTRACE=1 cargo build                                                                              02:53:57 PM
   Compiling equality-bug v0.1.0 (/Users/jacobgreenfield/Downloads/equality-bug)
error[E0308]: mismatched types
  --> src/main.rs:25:5
   |
23 | fn noop2<P: WrapperTrait<Inner = Foo>>(x: <P::Inner as AsRaw>::Raw) -> <P as AsRaw>::Raw {
   |                                                                        ----------------- expected `<P as AsRaw>::Raw` because of return type
24 |     // ERROR: mismatched types
25 |     x
   |     ^ expected associated type, found `u32`
   |
   = note: expected associated type `<P as AsRaw>::Raw`
                         found type `u32`
   = help: consider constraining the associated type `<P as AsRaw>::Raw` to `u32` or calling a method that returns `<P as AsRaw>::Raw`
   = note: for more information, visit https://doc.rust-lang.org/book/ch19-03-advanced-traits.html

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

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

Start with the reproducer in src/main.rs and run cargo build using the reported Rust toolchain to confirm the mismatch in noop2. Investigate rustc's associated-type equality and trait-bound resolution, then verify that noop2 accepts the same return value as noop1 and noop3 without the E0308 error.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.