authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-27 06:55:48-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-03-28 23:12:19+03:00
logdd66e0addb30d795a04324096c913ca89ccbcf40
tree928cf49e81d9047d629ff40832719d40dd3cca0f
parent2b805526033b0446fba7f38990df707204e7e23a

Sema: fix empty slice pointer value

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 #15085

4 files changed, 20 insertions(+), 7 deletions(-)

src/Sema.zig+6-1
......@@ -25152,7 +25152,7 @@ fn coerceExtra(
2515225152 .ptr = if (dest_info.@"align" != 0)
2515325153 try Value.Tag.int_u64.create(sema.arena, dest_info.@"align")
2515425154 else
25155 try inst_child_ty.lazyAbiAlignment(target, sema.arena),
25155 try dest_info.pointee_type.lazyAbiAlignment(target, sema.arena),
2515625156 .len = Value.zero,
2515725157 });
2515825158 return sema.addConstant(dest_ty, slice_val);
......@@ -30213,6 +30213,11 @@ fn resolveLazyValue(sema: *Sema, val: Value) CompileError!void {
3021330213 try sema.resolveLazyValue(elem_val);
3021430214 }
3021530215 },
30216 .slice => {
30217 const slice = val.castTag(.slice).?.data;
30218 try sema.resolveLazyValue(slice.ptr);
30219 return sema.resolveLazyValue(slice.len);
30220 },
3021630221 else => return,
3021730222 }
3021830223}
src/codegen/c.zig+1-1
......@@ -1069,7 +1069,7 @@ pub const DeclGen = struct {
10691069 const extern_fn = val.castTag(.extern_fn).?.data;
10701070 try dg.renderDeclName(writer, extern_fn.owner_decl, 0);
10711071 },
1072 .int_u64, .one => {
1072 .int_u64, .one, .int_big_positive, .lazy_align, .lazy_size => {
10731073 try writer.writeAll("((");
10741074 try dg.renderType(writer, ty);
10751075 return writer.print("){x})", .{try dg.fmtIntLiteral(Type.usize, val, .Other)});
src/codegen/llvm.zig+1-1
......@@ -3397,7 +3397,7 @@ pub const DeclGen = struct {
33973397 };
33983398 return dg.context.constStruct(&fields, fields.len, .False);
33993399 },
3400 .int_u64, .one, .int_big_positive => {
3400 .int_u64, .one, .int_big_positive, .lazy_align, .lazy_size => {
34013401 const llvm_usize = try dg.lowerType(Type.usize);
34023402 const llvm_int = llvm_usize.constInt(tv.val.toUnsignedInt(target), .False);
34033403 return llvm_int.constIntToPtr(try dg.lowerType(tv.ty));
test/behavior/slice.zig+12-4
......@@ -723,10 +723,18 @@ test "slice with dereferenced value" {
723723test "empty slice ptr is non null" {
724724 if (builtin.zig_backend == .stage2_aarch64 and builtin.os.tag == .macos) return error.SkipZigTest; // TODO
725725
726 const empty_slice: []u8 = &[_]u8{};
727 const p: [*]u8 = empty_slice.ptr + 0;
728 const t = @ptrCast([*]i8, p);
729 try expect(@ptrToInt(t) == @ptrToInt(empty_slice.ptr));
726 {
727 const empty_slice: []u8 = &[_]u8{};
728 const p: [*]u8 = empty_slice.ptr + 0;
729 const t = @ptrCast([*]i8, p);
730 try expect(@ptrToInt(t) == @ptrToInt(empty_slice.ptr));
731 }
732 {
733 const empty_slice: []u8 = &.{};
734 const p: [*]u8 = empty_slice.ptr + 0;
735 const t = @ptrCast([*]i8, p);
736 try expect(@ptrToInt(t) == @ptrToInt(empty_slice.ptr));
737 }
730738}
731739
732740test "slice decays to many pointer" {