UB Might Be a Wrong Term for Newer Languages
A short note on undefined behavior, which assumes familiarity with the subject (see this article for the introduction). The TL;DR is that I think that carrying the wording from the C standard into newer languages, like Zig and Rust, might be a mistake. This is strictly the word choice, the “lexical syntax of the comments” argument.
The C standard leaves many behaviors undefined.
However, it allows any particular implementation to fill in the gaps and define some of undefined-in-the-standard behaviors.
For example, C23 makes realloc(ptr, 0)
into an undefined behavior, so that POSIX can further refine it without interfering with the standard (source).
It’s also valid for an implementation to leave UB undefined. If a program compiled with this implementation hits this UB path, the behavior of the program as a whole is undefined (or rather, bounded by the execution environment. It is not actually possible to summon nasal daemons, because a user-space process can not escape its memory space other than by calling syscalls, and there are no nasal daemons summoning syscalls).
C implementations are not required to but may define behaviors left undefined by the standard. A C program written for a specific implementation may rely on undefined-in-the-standard but defined-in-the-implementation behavior.
Modern languages like Rust and Zig re-use the “undefined behavior” term. However, the intended semantics is subtly different. A program exhibiting UB is always considered invalid. Even if an alternative implementation of Rust defines some of Rust’s UB, the programs hitting those behaviors would still be incorrect.
For this reason, I think it would be better to use a different term here. I am not ready to suggest a specific wording, but a couple of reasonable options would be “non-trapping programming error” or “invalid behavior”. The intended semantics being that any program execution containing illegal behavior is invalid under any implementation.
Curiously, C++ is ahead of the pack here, as it has an explicit notion of “ill-formed, no diagnostic required”.
Update: I’ve since learned that Zig is updating its terminology. The new term is illegal behavior. This is perfect, “illegal” has just the right connotation of being explicitly declared incorrect by a written specifciation.