rust-lang / rust-lang/rust

Incorrect expected type for associated type with `specialization`

Open
#132,804 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-specialization C-discussion
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

The following code:

#![feature(specialization)]

#[derive(Clone, Copy, Debug)]
struct Cons<Item, Tail>(pub Item, pub Tail);

#[derive(Clone, Copy, Debug)]
struct Nil;

trait GetInternal<Item> {
    type Item;

    fn get(self) -> Self::Item;
}

impl<Item, Other, Tail> GetInternal<Item> for Cons<Other, Tail>
where
    Tail: GetInternal<Item>,
{
    default type Item = Tail::Item;

    fn get(self) -> Self::Item {
        self.1.get()
    }
}

Results in the following error:

   Compiling playground v0.0.1 (/playground)
warning: the feature `specialization` is incomplete and may not be safe to use and/or cause compiler crashes
 --> src/lib.rs:1:12
  |
1 | #![feature(specialization)]
  |            ^^^^^^^^^^^^^^
  |
  = note: see issue #31844 <https://github.com/rust-lang/rust/issues/31844> for more information
  = help: consider using `min_specialization` instead, which is more stable and complete
  = note: `#[warn(incomplete_features)]` on by default

error[E0308]: mismatched types
  --> src/lib.rs:22:9
   |
15 | impl<Item, Other, Tail> GetInternal<Item> for Cons<Other, Tail>
   |                   ---- found this type parameter
...
21 |     fn get(self) -> Self::Item {
   |                     ---------- expected `<Cons<Other, Tail> as GetInternal<Item>>::Item` because of return type
22 |         self.1.get()
   |         ^^^^^^^^^^^^ expected `Cons<Other, Tail>`, found type parameter `Tail`
   |
   = note: expected associated type `<Cons<Other, Tail> as GetInternal<Item>>::Item`
              found associated type `<Tail as GetInternal<Item>>::Item`
   = note: an associated type was expected, but a different one was found

For more information about this error, try `rustc --explain E0308`.
warning: `playground` (lib) generated 1 warning
error: could not compile `playground` (lib) due to 1 previous error; 1 warning emitted

However, the expected associated type should be <Tail as GetInternal<Item>>::Item, not <Cons<Other, Tail> as GetInternal<Item>>::Item. The error disappears if specialization is removed.

The error occurs with min_specialization as well, but min_specialization also gives an error about trying to specialize an associated type, so I assume min_specialization is not supposed to support specializing associated types.

P.S. This example was pared down from a larger example, so the reason for specialization is no longer present.

Meta

rustc --version --verbose:

rustc 1.84.0-nightly (59cec72a5 2024-11-08)
binary: rustc
commit-hash: 59cec72a57af178767a7b8e7f624b06cc50f1087
commit-date: 2024-11-08
host: x86_64-unknown-linux-gnu
release: 1.84.0-nightly
LLVM version: 19.1.3

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

First reproduce the minimized example with rustc 1.84.0-nightly and compare the diagnostics with and without specialization. Investigate specialization's associated-type handling in rustc's type checking; done means the implementation accepts the Tail::Item return type or reports the correct supported-language limitation.

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.