When working with compilers, linters, or static analyzers, a recurring frustration is that they reject programs we know are safe. In Rust, for example, the borrow checker rejects sound functions like Problem Case 3 from the Non-Lexical Lifetimes RFC, and attributes like #[must_use] only warn on obvious unused expressions while letting let _ = f(); pass silently.
This is not just an engineering limitation waiting for a smarter compiler pass. In computability theory, Rice’s Theorem shows that deciding any non-trivial property about what an arbitrary program does is fundamentally undecidable.
What Rice’s Theorem actually states
Informally, Rice’s Theorem is usually stated as:
All non-trivial semantic properties of programs are undecidable.
To make this precise, let be an enumeration of all computable partial functions, and let be the set of all computable functions. Suppose is some property of functions, and let be the corresponding set of program indices:
Two definitions matter here:
- Extensional (semantic). A set of indices is extensional if whenever two indices compute the exact same function, either both are in or neither is: In other words, only cares about the input-output behavior of the function, not about how its source code is written.1
- Non-trivial. Neither every function has the property nor none of them do, meaning and .
Formally, Rice’s Theorem then says: for an extensional index set , is recursive (decidable) if and only if or .
The Fixed Point Theorem
Many textbooks prove Rice’s Theorem by reducing the Halting Problem to . There is an even cleaner proof that uses the Fixed Point Theorem (Kleene’s Recursion Theorem) directly.2
The Fixed Point Theorem states that for every total computable function , there exists an index such that:
Note that this does not say as numbers. It says that the program at index computes the same function as the program at index . Even if tries to transform a program into a different one, there is always some program whose behavior leaves unchanged.
Proof of Rice’s Theorem
Using the Fixed Point Theorem, the proof takes five short steps:
- Since is non-trivial ( and ), there exist at least two indices and such that (so ) and (so ).
- Assume for contradiction that is recursive (decidable).
- Under that assumption, there is an algorithm that decides membership in , which lets us define the following function: Because is assumed to be recursive, is both total and computable. In essence, checks whether program has property and deliberately outputs an index from the opposite side.
- By the Fixed Point Theorem, there exists an index such that .
- Now we check the two possible values of :
- Case 1: . Since , we have . However, by the definition of , holds if and only if , meaning . Because and is extensional, this is a contradiction.
- Case 2: . Since , we have . By the definition of , holds if and only if , meaning . Again, gives a contradiction.
As the only assumption we made was that is recursive, cannot be recursive.
Why this matters for compilers and Rust
Rice’s Theorem draws a sharp line between two kinds of checks a compiler can run:
- Syntactic properties look at the structure of the code itself, not at the function it computes. Checking whether a variable is used in an expression (like Rust’s
#[must_use]lint) or whether every branch of amatchis covered is syntactic, and can therefore be checked efficiently at compile time. - Semantic properties ask about runtime behavior across all executions: whether a function always terminates, whether a pointer ever dangles, or what the exact minimum lifetime of every reference is. Because these properties are non-trivial and extensional, no algorithm can decide them exactly for all programs.
This is why static type systems like Rust’s borrow checker have to approximate. To stay both sound (never accepting an unsafe program) and decidable at compile time, the compiler must reject some programs that are actually safe. Better analyses like Non-Lexical Lifetimes or Polonius can shrink that gap and accept more valid patterns, but by Rice’s Theorem, the gap itself can never disappear completely.