New trait solver and simple specialization

⚓ Rust    📅 2025-08-30    👤 surdeus    👁️ 1      

surdeus

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)?

Thanks!

3 posts - 2 participants

Read full topic

🏷️ Rust_feed