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 28d6dd7 commit 3e00321
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
7 changes: 6 additions & 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 Expand Up @@ -30213,6 +30213,11 @@ fn resolveLazyValue(sema: *Sema, val: Value) CompileError!void {
try sema.resolveLazyValue(elem_val);
}
},
.slice => {
const slice = val.castTag(.slice).?.data;
try sema.resolveLazyValue(slice.ptr);
return sema.resolveLazyValue(slice.len);
},
else => return,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ pub const DeclGen = struct {
const extern_fn = val.castTag(.extern_fn).?.data;
try dg.renderDeclName(writer, extern_fn.owner_decl, 0);
},
.int_u64, .one => {
.int_u64, .one, .int_big_positive, .lazy_align, .lazy_size => {
try writer.writeAll("((");
try dg.renderType(writer, ty);
return writer.print("){x})", .{try dg.fmtIntLiteral(Type.usize, val, .Other)});
Expand Down
2 changes: 1 addition & 1 deletion src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3397,7 +3397,7 @@ pub const DeclGen = struct {
};
return dg.context.constStruct(&fields, fields.len, .False);
},
.int_u64, .one, .int_big_positive => {
.int_u64, .one, .int_big_positive, .lazy_align, .lazy_size => {
const llvm_usize = try dg.lowerType(Type.usize);
const llvm_int = llvm_usize.constInt(tv.val.toUnsignedInt(target), .False);
return llvm_int.constIntToPtr(try dg.lowerType(tv.ty));
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 3e00321

Please sign in to comment.