Incorrect expected type for associated type with specialization
#132804
Open
Description
opened on Nov 9, 2024
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
Activity