mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 14:23:09 +00:00
std: have std.meta.fieldInfo take an enum rather than a string
This commit is contained in:
parent
73bf2e1525
commit
9c97a07f18
@ -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"));
|
||||
|
||||
@ -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| {
|
||||
|
||||
@ -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 }, .{});
|
||||
}
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user