llvm: add asserts and behavior tests for #14063

Closes #14063
This commit is contained in:
Jacob Young 2022-12-26 23:13:01 -05:00
parent 46b49a0a76
commit 81318e8704
2 changed files with 18 additions and 0 deletions

View File

@ -1928,6 +1928,7 @@ pub const Object = struct {
if (ty.castTag(.@"struct")) |payload| {
const struct_obj = payload.data;
if (struct_obj.layout == .Packed and struct_obj.haveFieldTypes()) {
assert(struct_obj.haveLayout());
const info = struct_obj.backing_int_ty.intInfo(target);
const dwarf_encoding: c_uint = switch (info.signedness) {
.signed => DW.ATE.signed,
@ -2931,6 +2932,7 @@ pub const DeclGen = struct {
const struct_obj = t.castTag(.@"struct").?.data;
if (struct_obj.layout == .Packed) {
assert(struct_obj.haveLayout());
const int_llvm_ty = try dg.lowerType(struct_obj.backing_int_ty);
gop.value_ptr.* = int_llvm_ty;
return int_llvm_ty;
@ -3632,6 +3634,7 @@ pub const DeclGen = struct {
const struct_obj = tv.ty.castTag(.@"struct").?.data;
if (struct_obj.layout == .Packed) {
assert(struct_obj.haveLayout());
const big_bits = struct_obj.backing_int_ty.bitSize(target);
const int_llvm_ty = dg.context.intType(@intCast(c_uint, big_bits));
const fields = struct_obj.fields.values();
@ -9152,6 +9155,7 @@ pub const FuncGen = struct {
.Struct => {
if (result_ty.containerLayout() == .Packed) {
const struct_obj = result_ty.castTag(.@"struct").?.data;
assert(struct_obj.haveLayout());
const big_bits = struct_obj.backing_int_ty.bitSize(target);
const int_llvm_ty = self.context.intType(@intCast(c_uint, big_bits));
const fields = struct_obj.fields.values();

View File

@ -1430,6 +1430,12 @@ test "struct has only one reference" {
fn errorUnionStructReturn() error{Foo}!struct { x: u8 } {
return error.Foo;
}
fn pointerPackedStruct(_: *packed struct { x: u8 }) void {}
fn nestedPointerPackedStruct(_: struct { x: *packed struct { x: u8 } }) void {}
fn pointerNestedPackedStruct(_: *struct { x: packed struct { x: u8 } }) void {}
fn pointerNestedPointerPackedStruct(_: *struct { x: *packed struct { x: u8 } }) void {}
fn optionalComptimeIntParam(comptime x: ?comptime_int) comptime_int {
return x.?;
}
@ -1446,6 +1452,14 @@ test "struct has only one reference" {
const error_union_struct_return: *const anyopaque = &S.errorUnionStructReturn;
try expect(optional_struct_return != error_union_struct_return);
const pointer_packed_struct: *const anyopaque = &S.pointerPackedStruct;
const nested_pointer_packed_struct: *const anyopaque = &S.nestedPointerPackedStruct;
try expect(pointer_packed_struct != nested_pointer_packed_struct);
const pointer_nested_packed_struct: *const anyopaque = &S.pointerNestedPackedStruct;
const pointer_nested_pointer_packed_struct: *const anyopaque = &S.pointerNestedPointerPackedStruct;
try expect(pointer_nested_packed_struct != pointer_nested_pointer_packed_struct);
try expectEqual(@alignOf(struct {}), S.optionalComptimeIntParam(@alignOf(struct {})));
try expectEqual(@alignOf(struct { x: u8 }), S.errorUnionComptimeIntParam(@alignOf(struct { x: u8 })));
try expectEqual(@sizeOf(struct { x: u16 }), S.optionalComptimeIntParam(@sizeOf(struct { x: u16 })));