Date: Fri, 19 Sep 2025 17:45:19 -0700
On Friday, 19 September 2025 00:02:00 Pacific Daylight Time David Brown via
Std-Discussion wrote:
> I expect the compiler did exactly as you suggest -
> replace the loop with its internal representation for UB, exactly as it
> would if you wrote "__builtin_unreachable();". I am simply suggesting
> that in a situation like this, a clear halt is a bit more developer
> friendly.
The problem is reading the developer's mind. That's why we have a Standard: so
the developer can tell the compiler what they intended.
Suppose you have:
void will_exit();
bool f(int i)
{
if (i)
return false;
will_exit();
}
That will_exit() function comes from some third-party library and isn't marked
[[noreturn]]. We know it won't return, so we don't want the compiler to
generate unnecessary code. So the author of this code needs to insert a
__builtin_unreachabble() to suppress the "control reaches end of non-void
function" warning. But note how there's no difference in code generation: the
__builtin simply suppresses the warning.
That's no different from any other UB: the compiler can't tell the the UB could
happen and it should insert a halt. If you want that, you should compile with
UBSan.
Std-Discussion wrote:
> I expect the compiler did exactly as you suggest -
> replace the loop with its internal representation for UB, exactly as it
> would if you wrote "__builtin_unreachable();". I am simply suggesting
> that in a situation like this, a clear halt is a bit more developer
> friendly.
The problem is reading the developer's mind. That's why we have a Standard: so
the developer can tell the compiler what they intended.
Suppose you have:
void will_exit();
bool f(int i)
{
if (i)
return false;
will_exit();
}
That will_exit() function comes from some third-party library and isn't marked
[[noreturn]]. We know it won't return, so we don't want the compiler to
generate unnecessary code. So the author of this code needs to insert a
__builtin_unreachabble() to suppress the "control reaches end of non-void
function" warning. But note how there's no difference in code generation: the
__builtin simply suppresses the warning.
That's no different from any other UB: the compiler can't tell the the UB could
happen and it should insert a halt. If you want that, you should compile with
UBSan.
-- Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org Principal Engineer - Intel Platform & System Engineering
Received on 2025-09-20 00:45:27