mirror of
https://github.com/ziglang/zig.git
synced 2026-02-12 20:37:54 +00:00
std.builtin: remove layout field from Type.Enum
This commit is contained in:
parent
aac2d6b56f
commit
ceff03f3e9
@ -330,8 +330,6 @@ pub const Type = union(enum) {
|
||||
/// This data structure is used by the Zig language code generation and
|
||||
/// therefore must be kept in sync with the compiler implementation.
|
||||
pub const Enum = struct {
|
||||
/// TODO enums should no longer have this field in type info.
|
||||
layout: ContainerLayout,
|
||||
tag_type: type,
|
||||
fields: []const EnumField,
|
||||
decls: []const Declaration,
|
||||
|
||||
@ -371,16 +371,12 @@ test "std.meta.assumeSentinel" {
|
||||
pub fn containerLayout(comptime T: type) Type.ContainerLayout {
|
||||
return switch (@typeInfo(T)) {
|
||||
.Struct => |info| info.layout,
|
||||
.Enum => |info| info.layout,
|
||||
.Union => |info| info.layout,
|
||||
else => @compileError("Expected struct, enum or union type, found '" ++ @typeName(T) ++ "'"),
|
||||
else => @compileError("expected struct or union type, found '" ++ @typeName(T) ++ "'"),
|
||||
};
|
||||
}
|
||||
|
||||
test "std.meta.containerLayout" {
|
||||
const E1 = enum {
|
||||
A,
|
||||
};
|
||||
const S1 = struct {};
|
||||
const S2 = packed struct {};
|
||||
const S3 = extern struct {};
|
||||
@ -394,7 +390,6 @@ test "std.meta.containerLayout" {
|
||||
a: u8,
|
||||
};
|
||||
|
||||
try testing.expect(containerLayout(E1) == .Auto);
|
||||
try testing.expect(containerLayout(S1) == .Auto);
|
||||
try testing.expect(containerLayout(S2) == .Packed);
|
||||
try testing.expect(containerLayout(S3) == .Extern);
|
||||
@ -634,7 +629,6 @@ pub fn FieldEnum(comptime T: type) type {
|
||||
if (field_infos.len == 0) {
|
||||
return @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u0,
|
||||
.fields = &.{},
|
||||
.decls = &.{},
|
||||
@ -664,7 +658,6 @@ pub fn FieldEnum(comptime T: type) type {
|
||||
}
|
||||
return @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = std.math.IntFittingRange(0, field_infos.len - 1),
|
||||
.fields = &enumFields,
|
||||
.decls = &decls,
|
||||
@ -676,10 +669,6 @@ pub fn FieldEnum(comptime T: type) type {
|
||||
fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void {
|
||||
// TODO: https://github.com/ziglang/zig/issues/7419
|
||||
// testing.expectEqual(@typeInfo(expected).Enum, @typeInfo(actual).Enum);
|
||||
try testing.expectEqual(
|
||||
@typeInfo(expected).Enum.layout,
|
||||
@typeInfo(actual).Enum.layout,
|
||||
);
|
||||
try testing.expectEqual(
|
||||
@typeInfo(expected).Enum.tag_type,
|
||||
@typeInfo(actual).Enum.tag_type,
|
||||
@ -740,7 +729,6 @@ pub fn DeclEnum(comptime T: type) type {
|
||||
}
|
||||
return @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = std.math.IntFittingRange(0, fieldInfos.len - 1),
|
||||
.fields = &enumDecls,
|
||||
.decls = &decls,
|
||||
|
||||
@ -154,7 +154,6 @@ pub fn isExtern(comptime T: type) bool {
|
||||
return switch (@typeInfo(T)) {
|
||||
.Struct => |s| s.layout == .Extern,
|
||||
.Union => |u| u.layout == .Extern,
|
||||
.Enum => |e| e.layout == .Extern,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
@ -172,7 +171,6 @@ pub fn isPacked(comptime T: type) bool {
|
||||
return switch (@typeInfo(T)) {
|
||||
.Struct => |s| s.layout == .Packed,
|
||||
.Union => |u| u.layout == .Packed,
|
||||
.Enum => |e| e.layout == .Packed,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
24
src/Sema.zig
24
src/Sema.zig
@ -15690,14 +15690,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
|
||||
|
||||
const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespace());
|
||||
|
||||
const field_values = try sema.arena.create([5]Value);
|
||||
const field_values = try sema.arena.create([4]Value);
|
||||
field_values.* = .{
|
||||
// layout: ContainerLayout,
|
||||
try Value.Tag.enum_field_index.create(
|
||||
sema.arena,
|
||||
@enumToInt(std.builtin.Type.ContainerLayout.Auto),
|
||||
),
|
||||
|
||||
// tag_type: type,
|
||||
try Value.Tag.ty.create(sema.arena, int_tag_ty),
|
||||
// fields: []const EnumField,
|
||||
@ -18312,22 +18306,14 @@ fn zirReify(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData, in
|
||||
.Enum => {
|
||||
const struct_val: []const Value = union_val.val.castTag(.aggregate).?.data;
|
||||
// TODO use reflection instead of magic numbers here
|
||||
// layout: ContainerLayout,
|
||||
const layout_val = struct_val[0];
|
||||
// tag_type: type,
|
||||
const tag_type_val = struct_val[1];
|
||||
const tag_type_val = struct_val[0];
|
||||
// fields: []const EnumField,
|
||||
const fields_val = struct_val[2];
|
||||
const fields_val = struct_val[1];
|
||||
// decls: []const Declaration,
|
||||
const decls_val = struct_val[3];
|
||||
const decls_val = struct_val[2];
|
||||
// is_exhaustive: bool,
|
||||
const is_exhaustive_val = struct_val[4];
|
||||
|
||||
// enum layout is always auto
|
||||
const layout = layout_val.toEnum(std.builtin.Type.ContainerLayout);
|
||||
if (layout != .Auto) {
|
||||
return sema.fail(block, src, "reified enums must have a layout .Auto", .{});
|
||||
}
|
||||
const is_exhaustive_val = struct_val[3];
|
||||
|
||||
// Decls
|
||||
if (decls_val.sliceLen(mod) > 0) {
|
||||
|
||||
@ -273,7 +273,6 @@ test "generic function instantiation turns into comptime call" {
|
||||
var enumFields: [1]std.builtin.Type.EnumField = .{.{ .name = "A", .value = 0 }};
|
||||
return @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u0,
|
||||
.fields = &enumFields,
|
||||
.decls = &.{},
|
||||
|
||||
@ -354,7 +354,6 @@ test "Type.Enum" {
|
||||
|
||||
const Foo = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u8,
|
||||
.fields = &.{
|
||||
.{ .name = "a", .value = 1 },
|
||||
@ -369,7 +368,6 @@ test "Type.Enum" {
|
||||
try testing.expectEqual(@as(u8, 5), @enumToInt(Foo.b));
|
||||
const Bar = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u32,
|
||||
.fields = &.{
|
||||
.{ .name = "a", .value = 1 },
|
||||
@ -424,7 +422,6 @@ test "Type.Union" {
|
||||
|
||||
const Tag = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u1,
|
||||
.fields = &.{
|
||||
.{ .name = "signed", .value = 0 },
|
||||
@ -456,7 +453,6 @@ test "Type.Union from Type.Enum" {
|
||||
|
||||
const Tag = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u0,
|
||||
.fields = &.{
|
||||
.{ .name = "working_as_expected", .value = 0 },
|
||||
|
||||
@ -238,7 +238,6 @@ fn testEnum() !void {
|
||||
|
||||
const os_info = @typeInfo(Os);
|
||||
try expect(os_info == .Enum);
|
||||
try expect(os_info.Enum.layout == .Auto);
|
||||
try expect(os_info.Enum.fields.len == 4);
|
||||
try expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos"));
|
||||
try expect(os_info.Enum.fields[3].value == 3);
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
comptime {
|
||||
const E = @Type(.{ .Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u1,
|
||||
.fields = &.{
|
||||
.{ .name = "f0", .value = 0 },
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
export fn entry() void {
|
||||
_ = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u32,
|
||||
.fields = &.{
|
||||
.{ .name = "A", .value = 0 },
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
export fn entry() void {
|
||||
_ = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u32,
|
||||
.fields = &.{
|
||||
.{ .name = "A", .value = 10 },
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
const Tag = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = bool,
|
||||
.fields = &.{},
|
||||
.decls = &.{},
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
const Tag = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = undefined,
|
||||
.fields = &.{},
|
||||
.decls = &.{},
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
const Tag = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u2,
|
||||
.fields = &.{
|
||||
.{ .name = "signed", .value = 0 },
|
||||
@ -31,6 +30,6 @@ export fn entry() void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :14:16: error: enum field(s) missing in union
|
||||
// :13:16: error: enum field(s) missing in union
|
||||
// :1:13: note: field 'arst' missing, declared here
|
||||
// :1:13: note: enum declared here
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
const Tag = @Type(.{
|
||||
.Enum = .{
|
||||
.layout = .Auto,
|
||||
.tag_type = u1,
|
||||
.fields = &.{
|
||||
.{ .name = "signed", .value = 0 },
|
||||
@ -31,5 +30,5 @@ export fn entry() void {
|
||||
// backend=stage2
|
||||
// target=native
|
||||
//
|
||||
// :13:16: error: no field named 'arst' in enum 'tmp.Tag'
|
||||
// :12:16: error: no field named 'arst' in enum 'tmp.Tag'
|
||||
// :1:13: note: enum declared here
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user