Sema: fix @typeInfo for pointers returning 0 alignment

This commit is contained in:
Andrew Kelley 2022-02-08 23:02:13 -07:00
parent 1678825c14
commit f4fa32a632
3 changed files with 7 additions and 12 deletions

View File

@ -9651,6 +9651,11 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
},
.Pointer => {
const info = ty.ptrInfo().data;
const alignment = if (info.@"align" != 0)
info.@"align"
else
info.pointee_type.abiAlignment(target);
const field_values = try sema.arena.alloc(Value, 8);
// size: Size,
field_values[0] = try Value.Tag.enum_field_index.create(sema.arena, @enumToInt(info.size));
@ -9659,7 +9664,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
// is_volatile: bool,
field_values[2] = if (info.@"volatile") Value.@"true" else Value.@"false";
// alignment: comptime_int,
field_values[3] = try Value.Tag.int_u64.create(sema.arena, info.@"align");
field_values[3] = try Value.Tag.int_u64.create(sema.arena, alignment);
// address_space: AddressSpace
field_values[4] = try Value.Tag.enum_field_index.create(sema.arena, @enumToInt(info.@"addrspace"));
// child: type,

View File

@ -4687,7 +4687,7 @@ pub const Type = extern union {
pub const Data = struct {
pointee_type: Type,
sentinel: ?Value = null,
/// If zero use pointee_type.AbiAlign()
/// If zero use pointee_type.abiAlignment()
@"align": u32 = 0,
/// See src/target.zig defaultAddressSpace function for how to obtain
/// an appropriate value for this field.

View File

@ -71,8 +71,6 @@ fn testBasic() !void {
}
test "type info: pointer type info" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
try testPointer();
comptime try testPointer();
}
@ -89,8 +87,6 @@ fn testPointer() !void {
}
test "type info: unknown length pointer type info" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
try testUnknownLenPtr();
comptime try testUnknownLenPtr();
}
@ -125,8 +121,6 @@ fn testNullTerminatedPtr() !void {
}
test "type info: slice type info" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
try testSlice();
comptime try testSlice();
}
@ -306,8 +300,6 @@ const TestStruct = packed struct {
};
test "type info: opaque info" {
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
try testOpaque();
comptime try testOpaque();
}
@ -417,8 +409,6 @@ test "type info: TypeId -> TypeInfo impl cast" {
}
test "sentinel of opaque pointer type" {
if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
const c_void_info = @typeInfo(*anyopaque);
try expect(c_void_info.Pointer.sentinel == null);
}