Idea
The idea is that a new (internal/unstable) attribute, #[with_unstable]
, would exist and be applicable to impls. As in,
#[with_unstable]
impl Foo for Bar {
}
This is different from marking impls as #[unstable]
in that it changes how impls are applied rather than whether you're allowed to use an impl. In particular:
- If
Bar
references an unstable type, it should be a compile error unless#[unstable]
is used. - If
Bar
is generic, unstable types shouldn't resolve to said generics unless#[unstable]
is used.
On stable, this attribute would be completely ignored, due to backwards compatibility.
Alternatively:
- Every type is
rustc_internals::marker::Stable
by default - Generics are
: rustc_internals::marker::Stable
by default - (1) can be opted out with
#[unstable]
- (2) can be opted-out with
: ?rustc_internals::marker::Stable
(similar to?Sized
)
(Note: this is both stronger and weaker than #[with_unstable]
: it applies to functions, but not to non-generic trait impls. In particular #[derive(Eq, Hash, ...)]
on non-Stable
types would still work. Nevertheless it should not affect e.g. std::str::pattern::Pattern
in str
methods because that's a trait bound rather than a type argument, and the impls exist on stable types.)
Motivation
Option
to Result
was a mistake:
- rfcs/0000-try-trait-v2.md at do-or-do-not · scottmcm/rfcs · GitHub
- Alternatives to TryFrom - #7 by cuviper - help - The Rust Programming Language Forum
You can't fix this on stable. However, you can break this on nightly. Since nightly doesn't have the stability guarantees it's technically perfectly allowed to break compatibility with broken "features" (aka mistakes) on nightly. To encourage users to stop converting Option to Result, one just has to break Option to Result on nightly and call it a day. Stable support wouldn't be affected so it's fine.
Benefits
Code like this shouldn't work in the first place, and the fact that it does is arguably a bug. This is just fixing that bug - on nightly. It is extremely discouraged to write code that converts Option to Result like that, and Rust users are pretty good about chastising anyone who attempts to do so, which is nice. But it's not quite good enough, and there should be more motivations for not doing it. Making sure said code doesn't compile on nightly (altho with many easy workarounds - just throw #[with_unstable]
at it, or change the code to use .ok_or_else
or whatever) would provide such motivation.
Drawbacks
This would cause nightly behaviour to diverge from stable behaviour. If this is actually undesired, even for cases like this, then Rust would have to mark NoneError
as stable and deprecated. We believe this is the next step to getting rid of NoneError
tho.