Hi! I just gave a shot at the new trait solver in the nightly compiler dated from yesterday with the -Znext-solver rustflag, set in the cargo's config.toml.
Even if specialization is not complete/unsound, I am surprised that this simple case still doesn't compile:
#![feature(specialization)]
trait A<T> {
fn cool(&self, arg: &T);
}
struct MyStruct;
struct TraitDefault;
struct TraitSpecialized;
trait MarkerTraitForSpecialization {}
trait MarkerTraitForGeneric {}
impl MarkerTraitForSpecialization for TraitSpecialized {
}
impl MarkerTraitForGeneric for TraitDefault {
}
/*
enabling this block instead of the following one yields:
error[E0119]: conflicting implementations of trait `A<_>` for type `MyStruct`
default impl<T: MarkerTraitForGeneric> A<T> for B {
fn cool(&self, _arg: &T) {
println!("Generic")
}
}*/
default impl<T> A<T> for MyStruct {
fn cool(&self, _arg: &T) {
println!("Generic")
}
}
impl<T: MarkerTraitForSpecialization> A<T> for MyStruct {
fn cool(&self, _arg: &T) {
println!("Specialized")
}
}
fn main() {
let bee = MyStruct;
let foo = TraitSpecialized;
let bar = TraitDefault;
bee.cool(&foo);
bee.cool(&bar);
}
Error: error[E0277]: the trait bound TraitDefault: MarkerTraitForSpecialization is not satisfied
Traits are well defined here.
Any way to make that work now on nightly (without impl different structs obviously)?
Your snippet works if we mark A<T>::cool as default, instead of the whole impl<T> A<T> for MyStruct. To me, this speaks to the highly unstable state of the specialization feature.
Should I understand that when specialization will be sound, the code I wrote in the link above will be correct? Right now, I am welcomed with "conflicting implementation for MyStruct".
When you uncomment the different parts, it works but the meaning is not the same.
The -Zsolver-next flag doesn't change anything here.
Without the commented parts, the implementations do indeed conflict, as a struct that implements MarkerTraitForSpecialization, MarkerTraitForSpecialization2, and MarkerTraitForSpecialization3 has two non-overridable implementations.