Unreachable

unreachable is an assertion that the programmer makes to give more information to the compiler.

in unsafe release modes, unreachable (and assertions more in general) become logical propositions that the compiler can leverage to perform better optimizations. when those assertions are wrong, then you get undefined behavior because you gave a “false fact” to the compiler and the resulting behavior breaks the model generally used to describe computation.

in debug / safe modes the assertions are instead tested at runtime to help catch bugs, making unreachable behave similarly to a panic, although those are two very different things, almost opposite to one another.

unreachable means that you are confident a given condition will never manifest, while a panic means that you expect that a given condition could manifest, but that the program has no better way of dealing with it other than crashing.

unreachable when evaluated at comptime behaves like a panic always simply because comptime is always run in a safe evaluation context.

6 Likes