The following code results in an error (Playground)
#![feature(specialization)]
trait Foo {
type Assoc;
fn foo(&self) -> &Self::Assoc;
}
default impl<T> Foo for T {
type Assoc = T;
fn foo(&self) -> &Self::Assoc {
self
}
}
Error:
error[E0308]: mismatched types
--> src/main.rs:20:9
|
20 | self
| ^^^^ expected associated type, found type parameter
|
= note: expected type `&<T as Foo>::Assoc`
found type `&T`
This is strange since <T as Foo>::Assoc
is T
, so it should work. Stranger even: when I remove the default
keyword from the impl, it works (but of course, in my real code, I need to mark the impl as default
).
The same error happens when providing default values in the trait definition (Playground):
#![feature(specialization)]
#![feature(associated_type_defaults)]
trait Foo {
type Assoc = Self;
fn foo(&self) -> &Self::Assoc {
self
}
}
What's going on here? Is this a compiler bug? Or -- and that's why I'm asking this question -- does this error make sense because there is something special about specialization that I haven't understood yet? In case this is a bug, mem::transmute
is surely safe, riiiight?
impl Foo for String { type Assoc = i32; }
– trent ᶠᵒʳᵐᵉʳˡʸ ᶜˡassociated_type_defaults
feature is enabled. In this case it makes sense that the first example should fail for the same reason as the second example. – Calculator