RC RANDOM CHAOS

Rust stabilizes the never type after a two-year backward-compatibility fight

· via Hacker News

Original source

Stabilizing Rust's Never Type

Hacker News →

Rust has finally promoted its “never” type (written !) to stable status, a change landed on August 24 by compiler contributor “waffle” after more than two years of work. The type marks functions and expressions that can never produce a value—an infinite loop, a diverging call, or the error branch of an infallible conversion. It exists for two reasons: it lets generic code optimize away branches that provably can’t execute (for instance, a FromStr implementation whose error type is ! compiles as if the error path didn’t exist), and it gives the type checker a clean way to reason about diverging expressions instead of bolting on special cases. Because a value of ! can safely coerce to any type, it effectively acts as type-system-driven dead-code elimination.

The hold-up was a nasty inference corner case. When code produces a ! that must be coerced but the compiler has no concrete target type to infer, it falls back to a default. Older editions defaulted to the unit type (); the 2024 edition switches the fallback to ! itself, which better matches programmer intent but is technically a breaking change. A related trap sat in the standard library’s Infallible type, long used as a stand-in for !. Making Infallible a mere alias for !—the plan for silently speeding up existing code—turned out to be breaking on its own, because !’s implicit coercions can force new type annotations. The two changes, fortunately, nearly cancel each other out when applied together.

To gauge real-world fallout, waffle ran crater, Rust’s tool for recompiling every public crate on crates.io against a candidate compiler. The April run flagged 3,300 crates as negatively affected, but only seven were genuinely broken; the rest failed only through pinned dependencies on old library versions that had already been fixed, leaving the door open to backported patches for a handful of core crates. The episode is a concrete demonstration of how Rust’s edition system plus crater let the language make front-end breaking changes without fracturing the ecosystem.

Read the full article

Continue reading at Hacker News →

This is an AI-generated summary. Read the original for the full story.