diff options
| -rw-r--r-- | src/Sema.zig | 7 | ||||
| -rw-r--r-- | test/behavior.zig | 1 | ||||
| -rw-r--r-- | test/behavior/slice_stage2.zig | 12 |
3 files changed, 19 insertions, 1 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index d290ea8ec0..eaa99a42a7 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -8001,7 +8001,12 @@ fn analyzePtrArithmetic( const offset_int = offset_val.toUnsignedInt(); if (ptr_val.getUnsignedInt()) |addr| { const target = sema.mod.getTarget(); - const elem_ty = ptr_ty.childType(); + const ptr_child_ty = ptr_ty.childType(); + const elem_ty = if (ptr_ty.isSinglePointer() and ptr_child_ty.zigTypeTag() == .Array) + ptr_child_ty.childType() + else + ptr_child_ty; + const elem_size = elem_ty.abiSize(target); const new_addr = switch (air_tag) { .ptr_add => addr + elem_size * offset_int, diff --git a/test/behavior.zig b/test/behavior.zig index 05e05d51fc..42e3e7f07d 100644 --- a/test/behavior.zig +++ b/test/behavior.zig @@ -65,6 +65,7 @@ test { if (builtin.zig_is_stage2) { // When all comptime_memory.zig tests pass, #9646 can be closed. // _ = @import("behavior/comptime_memory.zig"); + _ = @import("behavior/slice_stage2.zig"); } else { _ = @import("behavior/align_stage1.zig"); _ = @import("behavior/alignof.zig"); diff --git a/test/behavior/slice_stage2.zig b/test/behavior/slice_stage2.zig new file mode 100644 index 0000000000..360527e8ba --- /dev/null +++ b/test/behavior/slice_stage2.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const expect = std.testing.expect; + +const x = @intToPtr([*]i32, 0x1000)[0..0x500]; +const y = x[0x100..]; +test "compile time slice of pointer to hard coded address" { + try expect(@ptrToInt(x) == 0x1000); + try expect(x.len == 0x500); + + try expect(@ptrToInt(y) == 0x1400); + try expect(y.len == 0x400); +} |
