Add impl Into<!> for Infallible
#83493
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
It's currently possible to convert a never (
!
) type instance intoInfallible
(impl From<!> for Infallible
). But not the other way around. That means that the (unstable) methodsResult::into_ok
andResult::into_err
(tracking issue: #61695) that use the trait boundsInto<!>
will currently not work when you have aResult<Infallible, _>
orResult<_, Infallible>
.I tried to implement
impl From<Infallible> for !
and ended up with a number of compilation errors like these:I found in the git history that this was attempted previously, in commit b2cf9a0 in PR #58302 by @SimonSapin. And the problem is brought up in comment #58302 (comment) but no solution or discussion followed. I also don't understand the error. Is this the same inference problem that is currently preventing the never type from becoming stable? Clearly, custom never tokens can implement conversion into
!
, just not the standardInfallible
one?I then tried
impl Into<!> for Infallible
instead. And it seems to work. In the future when!
is (hopefully) stabilized, this impl can just be removed, as it will be covered by by the blanket implementationimpl<T> From<T> for T
. I know that manually implementingInto
is not recommended. But as far as I know it's also not forbidden? This might be a legitimate case where we need it, since the correspondingFrom
does not work?I'm not sure if this could complicate the stabilization of the never type maybe? If so, we should likely not merge this. As I'm really hoping to get the proper never type on stable sometime. Ping @nikomatsakis, since I think you are still looking into that?
I tagged it as
#[stable]
directly. My understanding is that trait impls can't be unstable? Please advice on how to proceed here if this is wrong. Is an RFC needed?Alternative solution (for Result::{into_ok, into_err} specifically)
For the
into_ok
andinto_err
methods specifically there is another way to make them work on stable before!
is stabilized. The trait bounds can be changed fromInto<!>
toInto<Infallible
. Since the conversion between the types work that way it will continue to be usable on nightly for the real never type, and it will allow stable to use the methods withInfallible
. But unless there are reasons we don't wantInfallible
to be convertible to the real never type, I think this is cleaner and more useful forInfallible
in general.