Skip to content

Commit

Permalink
Sema: fix empty slice pointer value
Browse files Browse the repository at this point in the history
We just checked that inst_child_ty was effectively a zero-bit type, so
it is certainly not the non-zero alignment we are looking for.

Closes ziglang#15085
  • Loading branch information
jacobly0 committed Mar 27, 2023
1 parent 1de64db commit dd24cf7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -25152,7 +25152,7 @@ fn coerceExtra(
.ptr = if (dest_info.@"align" != 0)
try Value.Tag.int_u64.create(sema.arena, dest_info.@"align")
else
try inst_child_ty.lazyAbiAlignment(target, sema.arena),
try dest_info.pointee_type.lazyAbiAlignment(target, sema.arena),
.len = Value.zero,
});
return sema.addConstant(dest_ty, slice_val);
Expand Down
16 changes: 12 additions & 4 deletions test/behavior/slice.zig
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,18 @@ test "slice with dereferenced value" {
test "empty slice ptr is non null" {
if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest; // TODO

const empty_slice: []u8 = &[_]u8{};
const p: [*]u8 = empty_slice.ptr + 0;
const t = @ptrCast([*]i8, p);
try expect(@ptrToInt(t) == @ptrToInt(empty_slice.ptr));
{
const empty_slice: []u8 = &[_]u8{};
const p: [*]u8 = empty_slice.ptr + 0;
const t = @ptrCast([*]i8, p);
try expect(@ptrToInt(t) == @ptrToInt(empty_slice.ptr));
}
{
const empty_slice: []u8 = &.{};
const p: [*]u8 = empty_slice.ptr + 0;
const t = @ptrCast([*]i8, p);
try expect(@ptrToInt(t) == @ptrToInt(empty_slice.ptr));
}
}

test "slice decays to many pointer" {
Expand Down

0 comments on commit dd24cf7

Please sign in to comment.