zig
zig copied to clipboard
Audit uses of std.math.isPowerOfTwo in the compiler
When trying to compile
export fn x() u0 {
return 0;
}
a panic happens only with a debug build of the compiler:
src/Sema.zig:23338:43: 0x9395c1 in explainWhyTypeIsNotExtern (zig)
.Int => if (!std.math.isPowerOfTwo(ty.intInfo(sema.mod.getTarget()).bits)) {
^
In the release build of the compiler, it takes the wrong branch and
exhibits incorrect behavior because we're not checking that ty.intInfo(sema.mod.getTarget()).bits)
is not zero. isPowerOfTwo
has a assert(value != 0)
but in release optimize mode the assert
is optimized out and isPowerOfTwo
returns true for 0. Cases like this exist in many places in the compiler: git grep isPowerOfTwo
.
In many places the solution is to add x != 0 and
checks.
This is a bit of a prerequisite for #15194
See also the now outdated title and description of #15888.