diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2023-11-10 07:57:54 +0000 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2023-11-19 09:55:07 +0000 |
| commit | baabc6013ea4f44082e69375214e76b5d803c5cb (patch) | |
| tree | 00f89e2c48f72b581bedd7b2c8a068d28839368f /test/cases/compile_errors/pointer_arithmetic_on_pointer-to-array.zig | |
| parent | 325e0f5f0e8a9ce2540ec3ec5b7cbbecac15257a (diff) | |
| download | zig-baabc6013ea4f44082e69375214e76b5d803c5cb.tar.gz zig-baabc6013ea4f44082e69375214e76b5d803c5cb.zip | |
compiler: add error for unnecessary use of 'var'
When a local variable is never used as an lvalue, we can determine that
`const` would be sufficient for this variable, so emit an error in this
case. More sophisticated checking is unfortunately not possible with
Zig's current analysis model, since whether an lvalue is actually
mutated depends on semantic analysis, in which some code paths may not
be analyzed, so attempting to determine this would result in false
positive compile errors.
It's worth noting that an unfortunate consequence of this is that any
field call `a.b()` will allow `a` to be `var`, even if `b` does not take
a pointer as its first parameter - this is again a necessary compromise
because the parameter type is not known until semantic analysis.
Also update `translate-c` to not trigger these errors. This is done by
replacing the `_ = @TypeOf(x)` emitted with `_ = &x` - the reference
there means that the local is permitted to be `var`. A similar strategy
will be used to prevent compile errors in the behavior tests, where we
sometimes want to force a value to be runtime-known.
Resolves: #224
Diffstat (limited to 'test/cases/compile_errors/pointer_arithmetic_on_pointer-to-array.zig')
0 files changed, 0 insertions, 0 deletions
