| author | |
| committer | |
| log | 9c97a07f18413fb0a535bc89ea9aecd1656cda6a |
| tree | 95952ace758a0938eeed0347d366f71931e0e8c3 |
| parent | 73bf2e1525a392d367525ef96196fe15b14d8c30 |
6 files changed, 17 insertions(+), 22 deletions(-)
lib/std/meta.zig+6-11| ... | @@ -487,19 +487,14 @@ test "std.meta.fields" { | ... | @@ -487,19 +487,14 @@ test "std.meta.fields" { |
| 487 | testing.expect(comptime uf[0].field_type == u8); | 487 | testing.expect(comptime uf[0].field_type == u8); |
| 488 | } | 488 | } |
| 489 | 489 | ||
| 490 | pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) { | 490 | pub fn fieldInfo(comptime T: type, comptime field: FieldEnum(T)) switch (@typeInfo(T)) { |
| 491 | .Struct => TypeInfo.StructField, | 491 | .Struct => TypeInfo.StructField, |
| 492 | .Union => TypeInfo.UnionField, | 492 | .Union => TypeInfo.UnionField, |
| 493 | .ErrorSet => TypeInfo.Error, | 493 | .ErrorSet => TypeInfo.Error, |
| 494 | .Enum => TypeInfo.EnumField, | 494 | .Enum => TypeInfo.EnumField, |
| 495 | else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), | 495 | else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), |
| 496 | } { | 496 | } { |
| 497 | inline for (comptime fields(T)) |field| { | 497 | return fields(T)[@enumToInt(field)]; |
| 498 | if (comptime mem.eql(u8, field.name, field_name)) | ||
| 499 | return field; | ||
| 500 | } | ||
| 501 | |||
| 502 | @compileError("'" ++ @typeName(T) ++ "' has no field '" ++ field_name ++ "'"); | ||
| 503 | } | 498 | } |
| 504 | 499 | ||
| 505 | test "std.meta.fieldInfo" { | 500 | test "std.meta.fieldInfo" { |
| ... | @@ -514,10 +509,10 @@ test "std.meta.fieldInfo" { | ... | @@ -514,10 +509,10 @@ test "std.meta.fieldInfo" { |
| 514 | a: u8, | 509 | a: u8, |
| 515 | }; | 510 | }; |
| 516 | 511 | ||
| 517 | const e1f = comptime fieldInfo(E1, "A"); | 512 | const e1f = fieldInfo(E1, .A); |
| 518 | const e2f = comptime fieldInfo(E2, "A"); | 513 | const e2f = fieldInfo(E2, .A); |
| 519 | const sf = comptime fieldInfo(S1, "a"); | 514 | const sf = fieldInfo(S1, .a); |
| 520 | const uf = comptime fieldInfo(U1, "a"); | 515 | const uf = fieldInfo(U1, .a); |
| 521 | 516 | ||
| 522 | testing.expect(mem.eql(u8, e1f.name, "A")); | 517 | testing.expect(mem.eql(u8, e1f.name, "A")); |
| 523 | testing.expect(mem.eql(u8, e2f.name, "A")); | 518 | testing.expect(mem.eql(u8, e2f.name, "A")); |
lib/std/zig/ast.zig+1-1| ... | @@ -688,7 +688,7 @@ pub const Node = struct { | ... | @@ -688,7 +688,7 @@ pub const Node = struct { |
| 688 | 688 | ||
| 689 | /// Prefer `castTag` to this. | 689 | /// Prefer `castTag` to this. |
| 690 | pub fn cast(base: *Node, comptime T: type) ?*T { | 690 | pub fn cast(base: *Node, comptime T: type) ?*T { |
| 691 | if (std.meta.fieldInfo(T, "base").default_value) |default_base| { | 691 | if (std.meta.fieldInfo(T, .base).default_value) |default_base| { |
| 692 | return base.castTag(default_base.tag); | 692 | return base.castTag(default_base.tag); |
| 693 | } | 693 | } |
| 694 | inline for (@typeInfo(Tag).Enum.fields) |field| { | 694 | inline for (@typeInfo(Tag).Enum.fields) |field| { |
src/astgen.zig+7-7| ... | @@ -758,7 +758,7 @@ fn ptrSliceType(mod: *Module, scope: *Scope, src: usize, ptr_info: *ast.PtrInfo, | ... | @@ -758,7 +758,7 @@ fn ptrSliceType(mod: *Module, scope: *Scope, src: usize, ptr_info: *ast.PtrInfo, |
| 758 | }, child_type); | 758 | }, child_type); |
| 759 | } | 759 | } |
| 760 | 760 | ||
| 761 | var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, "kw_args").field_type = .{}; | 761 | var kw_args: std.meta.fieldInfo(zir.Inst.PtrType, .kw_args).field_type = .{}; |
| 762 | kw_args.size = size; | 762 | kw_args.size = size; |
| 763 | kw_args.@"allowzero" = ptr_info.allowzero_token != null; | 763 | kw_args.@"allowzero" = ptr_info.allowzero_token != null; |
| 764 | if (ptr_info.align_info) |some| { | 764 | if (ptr_info.align_info) |some| { |
| ... | @@ -2756,8 +2756,8 @@ pub fn addZIRInstSpecial( | ... | @@ -2756,8 +2756,8 @@ pub fn addZIRInstSpecial( |
| 2756 | scope: *Scope, | 2756 | scope: *Scope, |
| 2757 | src: usize, | 2757 | src: usize, |
| 2758 | comptime T: type, | 2758 | comptime T: type, |
| 2759 | positionals: std.meta.fieldInfo(T, "positionals").field_type, | 2759 | positionals: std.meta.fieldInfo(T, .positionals).field_type, |
| 2760 | kw_args: std.meta.fieldInfo(T, "kw_args").field_type, | 2760 | kw_args: std.meta.fieldInfo(T, .kw_args).field_type, |
| 2761 | ) !*T { | 2761 | ) !*T { |
| 2762 | const gen_zir = scope.getGenZIR(); | 2762 | const gen_zir = scope.getGenZIR(); |
| 2763 | try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1); | 2763 | try gen_zir.instructions.ensureCapacity(mod.gpa, gen_zir.instructions.items.len + 1); |
| ... | @@ -2874,8 +2874,8 @@ pub fn addZIRInst( | ... | @@ -2874,8 +2874,8 @@ pub fn addZIRInst( |
| 2874 | scope: *Scope, | 2874 | scope: *Scope, |
| 2875 | src: usize, | 2875 | src: usize, |
| 2876 | comptime T: type, | 2876 | comptime T: type, |
| 2877 | positionals: std.meta.fieldInfo(T, "positionals").field_type, | 2877 | positionals: std.meta.fieldInfo(T, .positionals).field_type, |
| 2878 | kw_args: std.meta.fieldInfo(T, "kw_args").field_type, | 2878 | kw_args: std.meta.fieldInfo(T, .kw_args).field_type, |
| 2879 | ) !*zir.Inst { | 2879 | ) !*zir.Inst { |
| 2880 | const inst_special = try addZIRInstSpecial(mod, scope, src, T, positionals, kw_args); | 2880 | const inst_special = try addZIRInstSpecial(mod, scope, src, T, positionals, kw_args); |
| 2881 | return &inst_special.base; | 2881 | return &inst_special.base; |
| ... | @@ -2883,12 +2883,12 @@ pub fn addZIRInst( | ... | @@ -2883,12 +2883,12 @@ pub fn addZIRInst( |
| 2883 | 2883 | ||
| 2884 | /// TODO The existence of this function is a workaround for a bug in stage1. | 2884 | /// TODO The existence of this function is a workaround for a bug in stage1. |
| 2885 | pub fn addZIRInstConst(mod: *Module, scope: *Scope, src: usize, typed_value: TypedValue) !*zir.Inst { | 2885 | pub fn addZIRInstConst(mod: *Module, scope: *Scope, src: usize, typed_value: TypedValue) !*zir.Inst { |
| 2886 | const P = std.meta.fieldInfo(zir.Inst.Const, "positionals").field_type; | 2886 | const P = std.meta.fieldInfo(zir.Inst.Const, .positionals).field_type; |
| 2887 | return addZIRInst(mod, scope, src, zir.Inst.Const, P{ .typed_value = typed_value }, .{}); | 2887 | return addZIRInst(mod, scope, src, zir.Inst.Const, P{ .typed_value = typed_value }, .{}); |
| 2888 | } | 2888 | } |
| 2889 | 2889 | ||
| 2890 | /// TODO The existence of this function is a workaround for a bug in stage1. | 2890 | /// TODO The existence of this function is a workaround for a bug in stage1. |
| 2891 | pub fn addZIRInstLoop(mod: *Module, scope: *Scope, src: usize, body: zir.Module.Body) !*zir.Inst.Loop { | 2891 | pub fn addZIRInstLoop(mod: *Module, scope: *Scope, src: usize, body: zir.Module.Body) !*zir.Inst.Loop { |
| 2892 | const P = std.meta.fieldInfo(zir.Inst.Loop, "positionals").field_type; | 2892 | const P = std.meta.fieldInfo(zir.Inst.Loop, .positionals).field_type; |
| 2893 | return addZIRInstSpecial(mod, scope, src, zir.Inst.Loop, P{ .body = body }, .{}); | 2893 | return addZIRInstSpecial(mod, scope, src, zir.Inst.Loop, P{ .body = body }, .{}); |
| 2894 | } | 2894 | } |
src/ir.zig+1-1| ... | @@ -189,7 +189,7 @@ pub const Inst = struct { | ... | @@ -189,7 +189,7 @@ pub const Inst = struct { |
| 189 | } | 189 | } |
| 190 | 190 | ||
| 191 | pub fn Args(comptime T: type) type { | 191 | pub fn Args(comptime T: type) type { |
| 192 | return std.meta.fieldInfo(T, "args").field_type; | 192 | return std.meta.fieldInfo(T, .args).field_type; |
| 193 | } | 193 | } |
| 194 | 194 | ||
| 195 | /// Returns `null` if runtime-known. | 195 | /// Returns `null` if runtime-known. |
src/type.zig+1-1| ... | @@ -3232,7 +3232,7 @@ pub const Type = extern union { | ... | @@ -3232,7 +3232,7 @@ pub const Type = extern union { |
| 3232 | } | 3232 | } |
| 3233 | 3233 | ||
| 3234 | pub fn Data(comptime t: Tag) type { | 3234 | pub fn Data(comptime t: Tag) type { |
| 3235 | return std.meta.fieldInfo(t.Type(), "data").field_type; | 3235 | return std.meta.fieldInfo(t.Type(), .data).field_type; |
| 3236 | } | 3236 | } |
| 3237 | }; | 3237 | }; |
| 3238 | 3238 |
src/value.zig+1-1| ... | @@ -207,7 +207,7 @@ pub const Value = extern union { | ... | @@ -207,7 +207,7 @@ pub const Value = extern union { |
| 207 | } | 207 | } |
| 208 | 208 | ||
| 209 | pub fn Data(comptime t: Tag) type { | 209 | pub fn Data(comptime t: Tag) type { |
| 210 | return std.meta.fieldInfo(t.Type(), "data").field_type; | 210 | return std.meta.fieldInfo(t.Type(), .data).field_type; |
| 211 | } | 211 | } |
| 212 | }; | 212 | }; |
| 213 | 213 |