You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is actually working as designed because Zig is not able to prove that x is undefined in the var case.
This case looks easy to prove, and indeed it is, but we could construct an arbitrarily difficult problem for which solving it would be the halting problem. Currently Zig's analysis stops at var. So, on the line with the if expression, the !x is an undefined value, and then if (undefined) is unchecked undefined behavior due to branching on an undefined value.
The issue for adding a safety check for this kind of undefined behavior is #63.
I don't think we have a proposal issue open for advanced control flow analysis which would attempt to prove that an undefined value gets branched on, such as in this case. If you'd like to transform this issue into such a proposal, feel free to do so. Just edit your issue and title and I'll add the appropriate labels.
andrewrk
removed
the
bug
Observed behavior contradicts documented or intended behavior
label
Feb 1, 2022
Ah #63 was what I was looking for. Yeah sorry I figured the comptime angle wasn’t exhaustively solvable but was unsure about the simple case. Runtime made a lot more sense. Thanks!
Zig Version
0.10.0-dev.500+66cf011aa
Steps to Reproduce
bug.zig
zig run bug.zig
-- passesvar
toconst
zig run bug.zig
-- compiler errorExpected Behavior
Compiler error regardless of var vs const usage, since either way you're comptime-provably using an undefined value.
Actual Behavior
Compiler error only if
const
.The text was updated successfully, but these errors were encountered: