diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-10-12 21:26:59 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-10-12 21:38:46 -0700 |
| commit | a3104a4a78089f3260c0dd3f4a96012c6d73a63b (patch) | |
| tree | 63d73840d9d48d09cd27c3fef5e52f051a2dc4bd /test/behavior/array.zig | |
| parent | 7f006287ae89b97ff0ca977f64744ea344ec94fd (diff) | |
| download | zig-a3104a4a78089f3260c0dd3f4a96012c6d73a63b.tar.gz zig-a3104a4a78089f3260c0dd3f4a96012c6d73a63b.zip | |
stage2: fix comptime stores and sentinel-terminated arrays
* ZIR: the `array_type_sentinel` now has a source node attached to it
for proper error reporting.
* Refactor: move `Module.arrayType` to `Type.array`
* Value: the `bytes` and `array` tags now include the sentinel, if the
type has one. This simplifies comptime evaluation logic.
* Sema: fix `zirStructInitEmpty` to properly handle when the type is
void or a sentinel-terminated array. This handles the syntax `void{}`
and `[0:X]T{}`.
* Sema: fix the logic for reporting "cannot store runtime value in
compile time variable" as well as for emitting a runtime store when a
pointer value is comptime known but it is a global variable.
* Sema: implement elemVal for double pointer to array. This can happen
with this code for example: `var a: *[1]u8 = undefined; _ = a[0];`
* Sema: Rework the `storePtrVal` function to properly handle nested
structs and arrays.
- Also it now handles comptime stores through a bitcasted pointer.
When the pointer element type and the type according to the Decl
don't match, the element value is bitcasted before storage.
Diffstat (limited to 'test/behavior/array.zig')
| -rw-r--r-- | test/behavior/array.zig | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/test/behavior/array.zig b/test/behavior/array.zig index c59a7e813a..7c2ab29280 100644 --- a/test/behavior/array.zig +++ b/test/behavior/array.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); const testing = std.testing; const mem = std.mem; const expect = testing.expect; @@ -76,3 +77,30 @@ test "array len field" { try expect(ptr.len == 4); comptime try expect(ptr.len == 4); } + +test "array with sentinels" { + const S = struct { + fn doTheTest(is_ct: bool) !void { + if (is_ct or builtin.zig_is_stage2) { + var zero_sized: [0:0xde]u8 = [_:0xde]u8{}; + // Stage1 test coverage disabled at runtime because of + // https://github.com/ziglang/zig/issues/4372 + try expect(zero_sized[0] == 0xde); + var reinterpreted = @ptrCast(*[1]u8, &zero_sized); + try expect(reinterpreted[0] == 0xde); + } + var arr: [3:0x55]u8 = undefined; + // Make sure the sentinel pointer is pointing after the last element. + if (!is_ct) { + const sentinel_ptr = @ptrToInt(&arr[3]); + const last_elem_ptr = @ptrToInt(&arr[2]); + try expect((sentinel_ptr - last_elem_ptr) == 1); + } + // Make sure the sentinel is writeable. + arr[3] = 0x55; + } + }; + + try S.doTheTest(false); + comptime try S.doTheTest(true); +} |
