Open Jarred-Sumner opened 11 months ago
although obviously not a solution to this problem, its vaguely related. i think undefined
should not be allowed as a default value for fields, its a terrible terrible pattern
it would still be a valid issue if the field init wasnt there and the var init was var garb: Garbagey = undefined;
i think undefined should not be allowed as a default value for fields, its a terrible terrible pattern
@xdBronch Why do you think so? IMO undefined
perfectly signals that the field's value won't be used.
If it's f.e. an internal field only used after being set by a method, it can make perfect sense not to burden every instantiation site with setting it to undefined.
As another example, std.BoundedArray
defaults its length to 0
and its elements to undefined
, because that is the most correct formulation to make .{}
coerce to its empty state. (Its elements logically don't exist, therefore their values shouldn't be read from as if they did exist.)
Setting a non-undefined
default value in these situations is strictly worse, because it incorrectly signals that the field's value would be used when in fact it won't (meaning we use the opportunity for both safety and optimizations).
i just feel that it goes against some of the ideas that zig tries to push. the existence of undefined
makes it so that youre less likely to have undefined memory problems because it always has to be explicitly done e.g. var foo: u32 = undefined;
, just you typing this out makes it 10x safer than the C equivalent. when a field defaults to undefined
its hidden from the user for imo very little reason which is more likely to cause the problems this issue aims to solve. BoundedArray is a fair enough use case but even then i think it would be better fit by making it part of an init function
Sounds like Usage of Uninitialized Memory (UUM) static analysis checker you are asking for as being part of point 6 in https://github.com/ziglang/zig/issues/2301#issuecomment-804496642.
Related could be moving AstGen to std.zig, which is pending "rework comptime mutation" and would be the first step to play with doing it out of tree with AstGen, but this would not be able to capture comptime semantics.
i just feel that it goes against some of the ideas that zig tries to push
Zig enables to write correct programs, which includes the absence of any value, if there would be no valid initial value. Default initialization would incur always an extra cost. Further more, default initialization can hide logic bugs. So you could have an incorrect program, which may be safer.
Zig Version
0.12.0-dev.1830+779b8e259
Steps to Reproduce and Observed Behavior
The following Zig compiles successfully:
The following C++ also compiles successfully:
However, when run through
clang-tidy
, it produces the following warnings:If we change the code to instead have no undefined memory accesses:
clang-tidy
detects that undefined memory is unreachable and no longer warns.Expected Behavior
Zig should have builtin safety checks that are at least as comprehensive as what C++ tooling provides, if not more.