diff options
| author | Mitchell Hashimoto <mitchell.hashimoto@gmail.com> | 2022-03-23 09:40:29 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2022-03-23 17:08:08 -0400 |
| commit | a36f4ee290fa9f3f1515e8aa9bd2bb0f0117c505 (patch) | |
| tree | 48684ef68021b319f9ed9844c5222e998a21b9e5 /test/behavior/slice.zig | |
| parent | f27d3409bdbe4a2b1b59f0b7336e582488e35986 (diff) | |
| download | zig-a36f4ee290fa9f3f1515e8aa9bd2bb0f0117c505.tar.gz zig-a36f4ee290fa9f3f1515e8aa9bd2bb0f0117c505.zip | |
stage2: able to slice to sentinel index at comptime
The runtime behavior allowed this in both stage1 and stage2, but stage1
fails with index out of bounds during comptime. This behavior makes
sense to support, and comptime behavior should match runtime behavior. I
implement this fix only in stage2.
Diffstat (limited to 'test/behavior/slice.zig')
| -rw-r--r-- | test/behavior/slice.zig | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/test/behavior/slice.zig b/test/behavior/slice.zig index 432524ebf7..9f3ba001cf 100644 --- a/test/behavior/slice.zig +++ b/test/behavior/slice.zig @@ -640,3 +640,46 @@ test "slice sentinel access at comptime" { try expect(slice0[slice0.len] == 0); } } + +test "slicing array with sentinel as end index" { + // Doesn't work in stage1 + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + + const S = struct { + fn do() !void { + var array = [_:0]u8{ 1, 2, 3, 4 }; + var slice = array[4..5]; + try expect(slice.len == 1); + try expect(slice[0] == 0); + try expect(@TypeOf(slice) == *[1]u8); + } + }; + + try S.do(); + comptime try S.do(); +} + +test "slicing slice with sentinel as end index" { + // Doesn't work in stage1 + if (builtin.zig_backend == .stage1) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; + + const S = struct { + fn do() !void { + var array = [_:0]u8{ 1, 2, 3, 4 }; + var src_slice: [:0]u8 = &array; + var slice = src_slice[4..5]; + try expect(slice.len == 1); + try expect(slice[0] == 0); + try expect(@TypeOf(slice) == *[1]u8); + } + }; + + try S.do(); + comptime try S.do(); +} |
