unreachable-codezig

What happen when program reach unreachable on ReleaseFast? (Zig lang)


I read on Zig Doc it has undefined behavior. is that it? isn't there any way tho predict the behavior of the code after hitting unreachable? like if it's process next line or try to continue like unreachable never been there!


Solution

  • isn't there any way tho predict the behavior of the code after hitting unreachable? like if it's process next line or try to continue like unreachable never been there!

    No, the compiler couldn't optimize as much otherwise.

    Never use unreachable if you want to control what happens.

    Remember: UB is worse than unpredictability when you hit that line. The optimizer could break your program even before you arrive at the point you hit UB.