logoalt Hacker News

Stabilizing Rust's Never Type

154 pointsby cjd8last Wednesday at 11:57 AM41 commentsview on HN

Comments

Georgelementalyesterday at 11:39 PM

> 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`.

This is incorrect. The compiler has always treated `Infallible` as uninhabited, and used that fact for optimizations. The downside of its lack of compiler support is losing out on the coercions. (The article is excellent otherwise)

show 1 reply
cipherjimyesterday at 11:24 PM

My favourite never type ability is when you need to conform to a trait that returns Result but your specific implementation can never produce an error.

Return Result<T, !> and the compiler knows that callers never have to check the error case because by definition it can’t be constructed.

show 1 reply
xg15yesterday at 8:47 PM

> After this change (and on the 2024 edition), the compiler assumes that T should be !, which doesn't implement Default, and therefore causes a compilation error.

If ! can coerce to every type, why not treat it as if it implemented every trait too?

show 2 replies
LatticeAnimalyesterday at 9:34 PM

Is it obvious to rust developers that "!" would be the never type? I frequently use "never" in typescript. I could imagine using the never type frequently in rust too. I feel like a longer more human-understandable name would've been a good decision here. (feels like more rust jargon that makes the language harder to learn)

show 4 replies
salsa_catsuptoday at 2:03 AM

Is this so central, that it justifies the use of a single ascii char? Instead of, say, `Never`?

weinzierlyesterday at 9:03 PM

Relevant talk by Waffle at RustWeek earlier this year:

"When is never?"

https://youtube.com/watch?v=3jM4cnEVrLc

kccqzyyesterday at 10:02 PM

The lesson here is that implicit conversions are bad. The never type itself having implicit conversions to other types is bad enough (even though such coercions are logically valid: “ex falso quodlibet” they should be explicit), but having a fallback type when type inference doesn’t have enough information to produce a type is even worse. Rust is famous for not even having implicit numeric coercions (say from i8 to i32) but it seems like a shortsighted decision to allow implicit coercions here.

show 3 replies
munchleryesterday at 9:24 PM

As a fan of the Curry-Howard correspondence, I approve of this decision.

show 1 reply
epolanskiyesterday at 8:44 PM

The never type seems very useful in various languages to either signal that a branch can never happen (the example of string -> bytestring never erroring) or to mark that a function will never return a value (and thus control) to the caller.

A simple TypeScript example:

const forever = (): never => { while (true) { // whatever } }

kevinbaivyesterday at 11:51 PM

[dead]

HNBeLiketoday at 12:25 AM

Sounds serious. Armageddon serious.

Nobody should get ahold of this technology.

Shut down the schools!

Get rid of all small business (to mitigate the risk).

15 days to prevent Never from destabilizing!

We’re all in this together.