Ixrec / rust-orphan-rules

An unofficial, experimental place for documenting and gathering feedback on the design problems around Rust's orphan rules

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How much would (stable) specialization help?

Ixrec opened this issue · comments

commented

See #1 for more history, but the basic links are:

Specialization RFC: rust-lang/rfcs#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.

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.

commented

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!()
    }
}
commented

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