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
I searched to my best ability but could not find any reports, so I hope this is genuine new:
The following correctly throws a compile time error:
pub fn main() void {
const result: i32 = undefined;
defer result = -555;
if (result == -555) {
std.debug.print("Result is: {}\n", .{result});
}
else {
std.debug.print("Result is not -555 but {}\n", .{result});
}
}
while the following is accepted:
pub fn main() void {
var result: i32 = undefined;
defer result = -555;
if (result == -555) {
std.debug.print("Result is: {}\n", .{result});
}
else {
std.debug.print("Result is not -555 but {}\n", .{result});
}
}
I am new to the language, and thus I appreciate the fact that the above might be a skill issue instead of a bug.
However, my expectation would be that undefined behaviour should be undefined behaviour independent of
Mutability of variables.
One run on my system lead to -1431655766, I doubt it will be the same on yours.
I also tested the above against the enum std.heap.Check and got the same undefined behaviour, though with an enum of only two members it is sort of predictable.
I am also not sure if for a newcomer like me, it is appropriate to provide suggestions, so feel free to ignore my thoughts.
Having said this, I would find it perfectly acceptable to disallow var x: type = undefined.
If, for some reason, it is necessary to have a mutable variable with an uninitialised state, one can always do:
const x: i32 = undefined;
...
x = 5;
var y = x;
As said, I am new and my first issue, feel free to close if not relevant.
Thank you for your hard work on this interesting language.
Expected Behavior
Either disallow var x: type = undefined
or
throw an error if used before initialisation. I appreciate with defer this might not trivial.
The text was updated successfully, but these errors were encountered:
jmfrank63
added
the
bug
Observed behavior contradicts documented or intended behavior
label
Mar 14, 2024
The reason it is detected with const is that the value of const variables is comptime known unlike with var, you would get the same error by using comptime var.
The issue for runtime safety for branching on undefined is #63 and #211
Detecting the issue in examples like yours would be simpler than to fully implement those other issues but it is also not that common.
Vexu
added
question
No questions on the issue tracker, please.
and removed
bug
Observed behavior contradicts documented or intended behavior
labels
Mar 14, 2024
Zig Version
0.12.0-dev.3290+b9cca3b63
Steps to Reproduce and Observed Behavior
I searched to my best ability but could not find any reports, so I hope this is genuine new:
The following correctly throws a compile time error:
while the following is accepted:
I am new to the language, and thus I appreciate the fact that the above might be a skill issue instead of a bug.
However, my expectation would be that undefined behaviour should be undefined behaviour independent of
Mutability of variables.
One run on my system lead to -1431655766, I doubt it will be the same on yours.
I also tested the above against the enum
std.heap.Check
and got the same undefined behaviour, though with an enum of only two members it is sort of predictable.I am also not sure if for a newcomer like me, it is appropriate to provide suggestions, so feel free to ignore my thoughts.
Having said this, I would find it perfectly acceptable to disallow
var x: type = undefined
.If, for some reason, it is necessary to have a mutable variable with an uninitialised state, one can always do:
As said, I am new and my first issue, feel free to close if not relevant.
Thank you for your hard work on this interesting language.
Expected Behavior
Either disallow
var x: type = undefined
or
throw an error if used before initialisation. I appreciate with defer this might not trivial.
The text was updated successfully, but these errors were encountered: