diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 1dac55dbb3..2c94569bf1 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -487,19 +487,14 @@ test "std.meta.fields" { testing.expect(comptime uf[0].field_type == u8); } -pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) { +pub fn fieldInfo(comptime T: type, comptime field: FieldEnum(T)) switch (@typeInfo(T)) { .Struct => TypeInfo.StructField, .Union => TypeInfo.UnionField, .ErrorSet => TypeInfo.Error, .Enum => TypeInfo.EnumField, else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), } { - inline for (comptime fields(T)) |field| { - if (comptime mem.eql(u8, field.name, field_name)) - return field; - } - - @compileError("'" ++ @typeName(T) ++ "' has no field '" ++ field_name ++ "'"); + return fields(T)[@enumToInt(field)]; } test "std.meta.fieldInfo" { @@ -514,10 +509,10 @@ test "std.meta.fieldInfo" { a: u8, }; - const e1f = comptime fieldInfo(E1, "A"); - const e2f = comptime fieldInfo(E2, "A"); - const sf = comptime fieldInfo(S1, "a"); - const uf = comptime fieldInfo(U1, "a"); + const e1f = fieldInfo(E1, .A); + const e2f = fieldInfo(E2, .A); + const sf = fieldInfo(S1, .a); + const uf = fieldInfo(U1, .a); testing.expect(mem.eql(u8, e1f.name, "A")); testing.expect(mem.eql(u8, e2f.name, "A")); diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index 9e191ee5fe..c0e72ea822 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -688,7 +688,7 @@ pub const Node = struct { /// Prefer `castTag` to this. pub fn cast(base: *Node, comptime T: type) ?*T { - if (std.meta.fieldInfo(T, "base").default_value) |default_base| { + if (std.meta.fieldInfo(T, .base).default_value) |default_base| { return base.castTag(default_base.tag); } inline for (@typeInfo(Tag).Enum.fields) |field| { diff --git a/src/astgen.zig b/src/astgen.zig index 5cf8e12587..3670cb260e 100644 --- a/src/astgen.zig +++ b/src/astgen.zig @@ -758,7 +758,7 @@ fn ptrSliceType(mod: *Module, scope: *Scope, src: usize, ptr_info: *ast.PtrInfo, }, child_type); } - var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, "kw_args").field_type = .{}; + var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, .kw_args).field_type = .{}; kw_args.size = size; kw_args.@"allowzero" = ptr_info.allowzero_token != null; if (ptr_info.align_info) |some| { @@ -2756,8 +2756,8 @@ pub fn addZIRInstSpecial( scope: *Scope, src: usize, comptime T: type, - positionals: std.meta.fieldInfo(T, "positionals").field_type, - kw_args: std.meta.fieldInfo(T, "kw_args").field_type, + positionals: std.meta.fieldInfo(T, .positionals).field_type, + kw_args: std.meta.fieldInfo(T, .kw_args).field_type, ) !*T { const gen_zir = scope.getGenZIR(); try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1); @@ -2874,8 +2874,8 @@ pub fn addZIRInst( scope: *Scope, src: usize, comptime T: type, - positionals: std.meta.fieldInfo(T, "positionals").field_type, - kw_args: std.meta.fieldInfo(T, "kw_args").field_type, + positionals: std.meta.fieldInfo(T, .positionals).field_type, + kw_args: std.meta.fieldInfo(T, .kw_args).field_type, ) !*zir.Inst { const inst_special = try addZIRInstSpecial(mod, scope, src, T, positionals, kw_args); return &inst_special.base; @@ -2883,12 +2883,12 @@ pub fn addZIRInst( /// TODO The existence of this function is a workaround for a bug in stage1. pub fn addZIRInstConst(mod: *Module, scope: *Scope, src: usize, typed_value: TypedValue) !*zir.Inst { - const P = std.meta.fieldInfo(zir.Inst.Const, "positionals").field_type; + const P = std.meta.fieldInfo(zir.Inst.Const, .positionals).field_type; return addZIRInst(mod, scope, src, zir.Inst.Const, P{ .typed_value = typed_value }, .{}); } /// TODO The existence of this function is a workaround for a bug in stage1. pub fn addZIRInstLoop(mod: *Module, scope: *Scope, src: usize, body: zir.Module.Body) !*zir.Inst.Loop { - const P = std.meta.fieldInfo(zir.Inst.Loop, "positionals").field_type; + const P = std.meta.fieldInfo(zir.Inst.Loop, .positionals).field_type; return addZIRInstSpecial(mod, scope, src, zir.Inst.Loop, P{ .body = body }, .{}); } diff --git a/src/ir.zig b/src/ir.zig index dda625c735..e43397faba 100644 --- a/src/ir.zig +++ b/src/ir.zig @@ -189,7 +189,7 @@ pub const Inst = struct { } pub fn Args(comptime T: type) type { - return std.meta.fieldInfo(T, "args").field_type; + return std.meta.fieldInfo(T, .args).field_type; } /// Returns `null` if runtime-known. diff --git a/src/type.zig b/src/type.zig index 7d35200727..bb2e3b3bc2 100644 --- a/src/type.zig +++ b/src/type.zig @@ -3232,7 +3232,7 @@ pub const Type = extern union { } pub fn Data(comptime t: Tag) type { - return std.meta.fieldInfo(t.Type(), "data").field_type; + return std.meta.fieldInfo(t.Type(), .data).field_type; } }; diff --git a/src/value.zig b/src/value.zig index 4450099069..8180df2f9e 100644 --- a/src/value.zig +++ b/src/value.zig @@ -207,7 +207,7 @@ pub const Value = extern union { } pub fn Data(comptime t: Tag) type { - return std.meta.fieldInfo(t.Type(), "data").field_type; + return std.meta.fieldInfo(t.Type(), .data).field_type; } };