Ixrec / rust-orphan-rules

An unofficial, experimental place for documenting and gathering feedback on the design problems around Rust's orphan rules
Apache License 2.0
194 stars 3 forks source link

How much would (stable) specialization help? #6

Open Ixrec opened 6 years ago

Ixrec commented 6 years ago

See https://github.com/Ixrec/rust-orphan-rules/issues/1 for more history, but the basic links are:

Specialization RFC: https://github.com/rust-lang/rfcs/pull/1210 Most recent blog post on making specialization sound: http://aturon.github.io/2018/04/05/sound-specialization/

This issue is for gathering feedback on whether or not stabilizing specialization actually solves use cases that are forbidden by the orphan rules today (since the orphan and overlap rules are intertwined). Since specialization is implemented on nightly, it should be possible to test and verify that it really does help, even if you can't use it for real yet.

dhardy commented 4 years ago

Play. With the current implementation of specialisation, I cannot write:

struct Wrap<X>(X);

impl<S, T: From<S>> From<Wrap<S>> for Wrap<T> {
    fn from(w: Wrap<S>) -> Self {
        Wrap(T::from(w.0))
    }
}

I suspect this is simply a case of lacking the commonly-referred to "lattice rules" (see here).

Edit: an even simpler example fails. I am forced to conclude that the current implementation of Specialization is too limited to be of much use in testing which situations specialisation would solve.

jyn514 commented 4 years ago

Play. With the current rules, I cannot write impl TryFrom<T> for MyStruct for a generic T.

use std::convert::TryFrom;
trait MyTrait {}
struct MyStruct;

impl<T> TryFrom<T> for MyStruct where T: MyTrait {
    type Error = ();
    fn try_from(t: T) -> Result<Self, Self::Error> {
        unimplemented!()
    }
}
jyn514 commented 4 years ago

Err hold on, that doesn't compile on nightly either. Ignore me.