authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-22 10:17:58+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-22 11:16:36+03:00
logb0bcd4add29b6fbbe2bbc22719703d3c81ed594b
tree8604903fa493cc8f9f328ffcefcd2bcea2f8b8bb
parent74c7782c6083d398a4f0f126a4597c605d5223cd

Sema: allow optional pointers in packed structs

Closes #12572

3 files changed, 19 insertions(+), 4 deletions(-)

src/Sema.zig+1-1
......@@ -20565,8 +20565,8 @@ fn validatePackedType(ty: Type) bool {
2056520565 .AnyFrame,
2056620566 .Fn,
2056720567 .Array,
20568 .Optional,
2056920568 => return false,
20569 .Optional => return ty.isPtrLikeOptional(),
2057020570 .Void,
2057120571 .Bool,
2057220572 .Float,
src/codegen/llvm.zig+6-3
......@@ -3417,7 +3417,10 @@ pub const DeclGen = struct {
34173417 });
34183418 const ty_bit_size = @intCast(u16, field.ty.bitSize(target));
34193419 const small_int_ty = dg.context.intType(ty_bit_size);
3420 const small_int_val = non_int_val.constBitCast(small_int_ty);
3420 const small_int_val = if (field.ty.isPtrAtRuntime())
3421 non_int_val.constPtrToInt(small_int_ty)
3422 else
3423 non_int_val.constBitCast(small_int_ty);
34213424 const shift_rhs = int_llvm_ty.constInt(running_bits, .False);
34223425 // If the field is as large as the entire packed struct, this
34233426 // zext would go from, e.g. i16 to i16. This is legal with
......@@ -5343,7 +5346,7 @@ pub const FuncGen = struct {
53435346 const same_size_int = self.context.intType(elem_bits);
53445347 const truncated_int = self.builder.buildTrunc(shifted_value, same_size_int, "");
53455348 return self.builder.buildBitCast(truncated_int, elem_llvm_ty, "");
5346 } else if (field_ty.zigTypeTag() == .Pointer) {
5349 } else if (field_ty.isPtrAtRuntime()) {
53475350 const elem_bits = @intCast(c_uint, field_ty.bitSize(target));
53485351 const same_size_int = self.context.intType(elem_bits);
53495352 const truncated_int = self.builder.buildTrunc(shifted_value, same_size_int, "");
......@@ -8408,7 +8411,7 @@ pub const FuncGen = struct {
84088411 const non_int_val = try self.resolveInst(elem);
84098412 const ty_bit_size = @intCast(u16, field.ty.bitSize(target));
84108413 const small_int_ty = self.dg.context.intType(ty_bit_size);
8411 const small_int_val = if (field.ty.zigTypeTag() == .Pointer)
8414 const small_int_val = if (field.ty.isPtrAtRuntime())
84128415 self.builder.buildPtrToInt(non_int_val, small_int_ty, "")
84138416 else
84148417 self.builder.buildBitCast(non_int_val, small_int_ty, "");
test/behavior/packed-struct.zig+12
......@@ -434,3 +434,15 @@ test "@ptrToInt on a packed struct field" {
434434 };
435435 try expect(@ptrToInt(&S.p0.z) - @ptrToInt(&S.p0.x) == 2);
436436}
437
438test "optional pointer in packed struct" {
439 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest;
440 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
441 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
442 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
443
444 const T = packed struct { ptr: ?*const u8 };
445 var n: u8 = 0;
446 const x = T{ .ptr = &n };
447 try expect(x.ptr.? == &n);
448}