|
|
Log in / Subscribe / Register

Infallible is not as bad as it sounds

Infallible is not as bad as it sounds

Posted Sep 9, 2026 7:58 UTC (Wed) by NYKevin (subscriber, #129325)
Parent article: Stabilizing Rust's never type

> For many years, the standard library has had an Infallible type to work around the unstable nature of the never type. It served the same semantic purpose as the never type, but did not have any special compiler support. Therefore, code using it would be technically correct but suboptimal (such as having an extra layer of tags in an enumeration or emitting dead code), because the optimizer would not always be able to remove references to Infallible.

Infallible is (was) defined as an empty enum, and Rust knows very well that empty enums cannot be instantiated. For example, you can legally write something like the following:

let Ok(s) = String::FromStr("hello world!");

Note the complete lack of an Err case. This works even without the never type stabilization, because Rust knows the Err() case can't really exist. If this is known early enough for the above code to type-check successfully, then it obviously should also be known at trans (which is much later in the compilation process).

The real problem with Infallible is mostly that it has a silly name and does not auto-coerce at all. The latter can be worked around by writing match(x) {} where x is (or contains) an Infallible. The match statement does not require arms because (again) Rust knows that there are no possible values to match against, and does not require any match arms. This expression will coerce into an arbitrary type. But that's more unwieldy than using !, which auto-coerces and doesn't require this workaround.


to post comments


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds