Rust funny enough has unreachable()! for that case, but it also has unreachable_unchecked() for actually unreachable code. The latter has undefined behavior and exists to help the optimizer.
Rust funny enough has unreachable()! for that case, but it also has unreachable_unchecked() for actually unreachable code. The latter has undefined behavior and exists to help the optimizer.
I stripped it out before production after verifying it couldn’t actually happen but it was something like an artefact of my thinking process while writing the code.
It feels like a kind of assertion of underlying assumptions, and I know enough to know I’m fallible.
I’m always careful to make the error message something reasonable if it ever did actually come up in a tech demo or something though. Anything else is tempting the Fates.
I’d always make it something like “If you see this, something entirely unexpected went wrong.”
Showing and triggering the error is helpful in itself since it’ll generate a trace and all the attendant stuff.
The usual argument is that the program would be in an invalid state if the condition was reached, so the only option is to crash. If it turns out it's a valid state, then the programmer can treat in a branch. I don't think tests would capture this because they would operating under the same assumption that such states cannot exist. Maybe fuzzy testing could surface an issue like this.