authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-02-24 23:03:30+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-02-24 23:09:01+02:00
log45da72c5b64069b7d5238465130a50f96678a148
tree136529c703d9cb9e52f2df2354dc0bb03fcbc482
parent1d06c82c3bc44e808a3d7b4fe07e5c9fc492e8c3
signaturelock-open Commit is signed but in an unrecognized format.

remove usages of `@typeId`, `@memberCount`, `@memberName` and `@memberType`


28 files changed, 145 insertions(+), 315 deletions(-)

lib/std/build.zig+11-11
...@@ -607,13 +607,13 @@ pub const Builder = struct {...@@ -607,13 +607,13 @@ pub const Builder = struct {
607 }607 }
608608
609 fn typeToEnum(comptime T: type) TypeId {609 fn typeToEnum(comptime T: type) TypeId {
610 return switch (@typeId(T)) {610 return switch (@typeInfo(T)) {
611 builtin.TypeId.Int => TypeId.Int,611 .Int => .Int,
612 builtin.TypeId.Float => TypeId.Float,612 .Float => .Float,
613 builtin.TypeId.Bool => TypeId.Bool,613 .Bool => .Bool,
614 else => switch (T) {614 else => switch (T) {
615 []const u8 => TypeId.String,615 []const u8 => .String,
616 []const []const u8 => TypeId.List,616 []const []const u8 => .List,
617 else => @compileError("Unsupported type: " ++ @typeName(T)),617 else => @compileError("Unsupported type: " ++ @typeName(T)),
618 },618 },
619 };619 };
...@@ -625,11 +625,11 @@ pub const Builder = struct {...@@ -625,11 +625,11 @@ pub const Builder = struct {
625625
626 pub fn typeIdName(id: TypeId) []const u8 {626 pub fn typeIdName(id: TypeId) []const u8 {
627 return switch (id) {627 return switch (id) {
628 TypeId.Bool => "bool",628 .Bool => "bool",
629 TypeId.Int => "int",629 .Int => "int",
630 TypeId.Float => "float",630 .Float => "float",
631 TypeId.String => "string",631 .String => "string",
632 TypeId.List => "list",632 .List => "list",
633 };633 };
634 }634 }
635635
lib/std/fmt.zig+8-7
...@@ -405,7 +405,7 @@ pub fn formatType(...@@ -405,7 +405,7 @@ pub fn formatType(
405 try format(context, Errors, output, "@{x}", .{@ptrToInt(&value)});405 try format(context, Errors, output, "@{x}", .{@ptrToInt(&value)});
406 }406 }
407 },407 },
408 .Struct => {408 .Struct => |StructT| {
409 if (comptime std.meta.trait.hasFn("format")(T)) {409 if (comptime std.meta.trait.hasFn("format")(T)) {
410 return value.format(fmt, options, context, Errors, output);410 return value.format(fmt, options, context, Errors, output);
411 }411 }
...@@ -416,27 +416,28 @@ pub fn formatType(...@@ -416,27 +416,28 @@ pub fn formatType(
416 }416 }
417 comptime var field_i = 0;417 comptime var field_i = 0;
418 try output(context, "{");418 try output(context, "{");
419 inline while (field_i < @memberCount(T)) : (field_i += 1) {419 inline for (StructT.fields) |f| {
420 if (field_i == 0) {420 if (field_i == 0) {
421 try output(context, " .");421 try output(context, " .");
422 } else {422 } else {
423 try output(context, ", .");423 try output(context, ", .");
424 }424 }
425 try output(context, @memberName(T, field_i));425 try output(context, f.name);
426 try output(context, " = ");426 try output(context, " = ");
427 try formatType(@field(value, @memberName(T, field_i)), fmt, options, context, Errors, output, max_depth - 1);427 try formatType(@field(value, f.name), fmt, options, context, Errors, output, max_depth - 1);
428 field_i += 1;
428 }429 }
429 try output(context, " }");430 try output(context, " }");
430 },431 },
431 .Pointer => |ptr_info| switch (ptr_info.size) {432 .Pointer => |ptr_info| switch (ptr_info.size) {
432 .One => switch (@typeInfo(ptr_info.child)) {433 .One => switch (@typeInfo(ptr_info.child)) {
433 builtin.TypeId.Array => |info| {434 .Array => |info| {
434 if (info.child == u8) {435 if (info.child == u8) {
435 return formatText(value, fmt, options, context, Errors, output);436 return formatText(value, fmt, options, context, Errors, output);
436 }437 }
437 return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });438 return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
438 },439 },
439 builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {440 .Enum, .Union, .Struct => {
440 return formatType(value.*, fmt, options, context, Errors, output, max_depth);441 return formatType(value.*, fmt, options, context, Errors, output, max_depth);
441 },442 },
442 else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),443 else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),
...@@ -509,7 +510,7 @@ fn formatValue(...@@ -509,7 +510,7 @@ fn formatValue(
509 }510 }
510511
511 const T = @TypeOf(value);512 const T = @TypeOf(value);
512 switch (@typeId(T)) {513 switch (@typeInfo(T)) {
513 .Float => return formatFloatValue(value, fmt, options, context, Errors, output),514 .Float => return formatFloatValue(value, fmt, options, context, Errors, output),
514 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output),515 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output),
515 .Bool => return output(context, if (value) "true" else "false"),516 .Bool => return output(context, if (value) "true" else "false"),
lib/std/io.zig+23-23
...@@ -831,7 +831,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -831,7 +831,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
831831
832 //@BUG: inferred error issue. See: #1386832 //@BUG: inferred error issue. See: #1386
833 fn deserializeInt(self: *Self, comptime T: type) (Error || error{EndOfStream})!T {833 fn deserializeInt(self: *Self, comptime T: type) (Error || error{EndOfStream})!T {
834 comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T));834 comptime assert(trait.is(.Int)(T) or trait.is(.Float)(T));
835835
836 const u8_bit_count = 8;836 const u8_bit_count = 8;
837 const t_bit_count = comptime meta.bitCount(T);837 const t_bit_count = comptime meta.bitCount(T);
...@@ -880,9 +880,9 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -880,9 +880,9 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
880 /// Deserializes data into the type pointed to by `ptr`880 /// Deserializes data into the type pointed to by `ptr`
881 pub fn deserializeInto(self: *Self, ptr: var) !void {881 pub fn deserializeInto(self: *Self, ptr: var) !void {
882 const T = @TypeOf(ptr);882 const T = @TypeOf(ptr);
883 comptime assert(trait.is(builtin.TypeId.Pointer)(T));883 comptime assert(trait.is(.Pointer)(T));
884884
885 if (comptime trait.isSlice(T) or comptime trait.isPtrTo(builtin.TypeId.Array)(T)) {885 if (comptime trait.isSlice(T) or comptime trait.isPtrTo(.Array)(T)) {
886 for (ptr) |*v|886 for (ptr) |*v|
887 try self.deserializeInto(v);887 try self.deserializeInto(v);
888 return;888 return;
...@@ -891,7 +891,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -891,7 +891,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
891 comptime assert(trait.isSingleItemPtr(T));891 comptime assert(trait.isSingleItemPtr(T));
892892
893 const C = comptime meta.Child(T);893 const C = comptime meta.Child(T);
894 const child_type_id = @typeId(C);894 const child_type_id = @typeInfo(C);
895895
896 //custom deserializer: fn(self: *Self, deserializer: var) !void896 //custom deserializer: fn(self: *Self, deserializer: var) !void
897 if (comptime trait.hasFn("deserialize")(C)) return C.deserialize(ptr, self);897 if (comptime trait.hasFn("deserialize")(C)) return C.deserialize(ptr, self);
...@@ -902,10 +902,10 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -902,10 +902,10 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
902 }902 }
903903
904 switch (child_type_id) {904 switch (child_type_id) {
905 builtin.TypeId.Void => return,905 .Void => return,
906 builtin.TypeId.Bool => ptr.* = (try self.deserializeInt(u1)) > 0,906 .Bool => ptr.* = (try self.deserializeInt(u1)) > 0,
907 builtin.TypeId.Float, builtin.TypeId.Int => ptr.* = try self.deserializeInt(C),907 .Float, .Int => ptr.* = try self.deserializeInt(C),
908 builtin.TypeId.Struct => {908 .Struct => {
909 const info = @typeInfo(C).Struct;909 const info = @typeInfo(C).Struct;
910910
911 inline for (info.fields) |*field_info| {911 inline for (info.fields) |*field_info| {
...@@ -915,7 +915,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -915,7 +915,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
915 if (FieldType == void or FieldType == u0) continue;915 if (FieldType == void or FieldType == u0) continue;
916916
917 //it doesn't make any sense to read pointers917 //it doesn't make any sense to read pointers
918 if (comptime trait.is(builtin.TypeId.Pointer)(FieldType)) {918 if (comptime trait.is(.Pointer)(FieldType)) {
919 @compileError("Will not " ++ "read field " ++ name ++ " of struct " ++919 @compileError("Will not " ++ "read field " ++ name ++ " of struct " ++
920 @typeName(C) ++ " because it " ++ "is of pointer-type " ++920 @typeName(C) ++ " because it " ++ "is of pointer-type " ++
921 @typeName(FieldType) ++ ".");921 @typeName(FieldType) ++ ".");
...@@ -924,7 +924,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -924,7 +924,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
924 try self.deserializeInto(&@field(ptr, name));924 try self.deserializeInto(&@field(ptr, name));
925 }925 }
926 },926 },
927 builtin.TypeId.Union => {927 .Union => {
928 const info = @typeInfo(C).Union;928 const info = @typeInfo(C).Union;
929 if (info.tag_type) |TagType| {929 if (info.tag_type) |TagType| {
930 //we avoid duplicate iteration over the enum tags930 //we avoid duplicate iteration over the enum tags
...@@ -948,7 +948,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -948,7 +948,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
948 @compileError("Cannot meaningfully deserialize " ++ @typeName(C) ++948 @compileError("Cannot meaningfully deserialize " ++ @typeName(C) ++
949 " because it is an untagged union. Use a custom deserialize().");949 " because it is an untagged union. Use a custom deserialize().");
950 },950 },
951 builtin.TypeId.Optional => {951 .Optional => {
952 const OC = comptime meta.Child(C);952 const OC = comptime meta.Child(C);
953 const exists = (try self.deserializeInt(u1)) > 0;953 const exists = (try self.deserializeInt(u1)) > 0;
954 if (!exists) {954 if (!exists) {
...@@ -960,7 +960,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,...@@ -960,7 +960,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
960 const val_ptr = &ptr.*.?;960 const val_ptr = &ptr.*.?;
961 try self.deserializeInto(val_ptr);961 try self.deserializeInto(val_ptr);
962 },962 },
963 builtin.TypeId.Enum => {963 .Enum => {
964 var value = try self.deserializeInt(@TagType(C));964 var value = try self.deserializeInt(@TagType(C));
965 ptr.* = try meta.intToEnum(C, value);965 ptr.* = try meta.intToEnum(C, value);
966 },966 },
...@@ -1009,7 +1009,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co...@@ -1009,7 +1009,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
10091009
1010 fn serializeInt(self: *Self, value: var) Error!void {1010 fn serializeInt(self: *Self, value: var) Error!void {
1011 const T = @TypeOf(value);1011 const T = @TypeOf(value);
1012 comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T));1012 comptime assert(trait.is(.Int)(T) or trait.is(.Float)(T));
10131013
1014 const t_bit_count = comptime meta.bitCount(T);1014 const t_bit_count = comptime meta.bitCount(T);
1015 const u8_bit_count = comptime meta.bitCount(u8);1015 const u8_bit_count = comptime meta.bitCount(u8);
...@@ -1058,11 +1058,11 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co...@@ -1058,11 +1058,11 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
1058 return;1058 return;
1059 }1059 }
10601060
1061 switch (@typeId(T)) {1061 switch (@typeInfo(T)) {
1062 builtin.TypeId.Void => return,1062 .Void => return,
1063 builtin.TypeId.Bool => try self.serializeInt(@as(u1, @boolToInt(value))),1063 .Bool => try self.serializeInt(@as(u1, @boolToInt(value))),
1064 builtin.TypeId.Float, builtin.TypeId.Int => try self.serializeInt(value),1064 .Float, .Int => try self.serializeInt(value),
1065 builtin.TypeId.Struct => {1065 .Struct => {
1066 const info = @typeInfo(T);1066 const info = @typeInfo(T);
10671067
1068 inline for (info.Struct.fields) |*field_info| {1068 inline for (info.Struct.fields) |*field_info| {
...@@ -1072,7 +1072,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co...@@ -1072,7 +1072,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
1072 if (FieldType == void or FieldType == u0) continue;1072 if (FieldType == void or FieldType == u0) continue;
10731073
1074 //It doesn't make sense to write pointers1074 //It doesn't make sense to write pointers
1075 if (comptime trait.is(builtin.TypeId.Pointer)(FieldType)) {1075 if (comptime trait.is(.Pointer)(FieldType)) {
1076 @compileError("Will not " ++ "serialize field " ++ name ++1076 @compileError("Will not " ++ "serialize field " ++ name ++
1077 " of struct " ++ @typeName(T) ++ " because it " ++1077 " of struct " ++ @typeName(T) ++ " because it " ++
1078 "is of pointer-type " ++ @typeName(FieldType) ++ ".");1078 "is of pointer-type " ++ @typeName(FieldType) ++ ".");
...@@ -1080,7 +1080,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co...@@ -1080,7 +1080,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
1080 try self.serialize(@field(value, name));1080 try self.serialize(@field(value, name));
1081 }1081 }
1082 },1082 },
1083 builtin.TypeId.Union => {1083 .Union => {
1084 const info = @typeInfo(T).Union;1084 const info = @typeInfo(T).Union;
1085 if (info.tag_type) |TagType| {1085 if (info.tag_type) |TagType| {
1086 const active_tag = meta.activeTag(value);1086 const active_tag = meta.activeTag(value);
...@@ -1101,7 +1101,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co...@@ -1101,7 +1101,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
1101 @compileError("Cannot meaningfully serialize " ++ @typeName(T) ++1101 @compileError("Cannot meaningfully serialize " ++ @typeName(T) ++
1102 " because it is an untagged union. Use a custom serialize().");1102 " because it is an untagged union. Use a custom serialize().");
1103 },1103 },
1104 builtin.TypeId.Optional => {1104 .Optional => {
1105 if (value == null) {1105 if (value == null) {
1106 try self.serializeInt(@as(u1, @boolToInt(false)));1106 try self.serializeInt(@as(u1, @boolToInt(false)));
1107 return;1107 return;
...@@ -1112,10 +1112,10 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co...@@ -1112,10 +1112,10 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
1112 const val_ptr = &value.?;1112 const val_ptr = &value.?;
1113 try self.serialize(val_ptr.*);1113 try self.serialize(val_ptr.*);
1114 },1114 },
1115 builtin.TypeId.Enum => {1115 .Enum => {
1116 try self.serializeInt(@enumToInt(value));1116 try self.serializeInt(@enumToInt(value));
1117 },1117 },
1118 else => @compileError("Cannot serialize " ++ @tagName(@typeId(T)) ++ " types (unimplemented)."),1118 else => @compileError("Cannot serialize " ++ @tagName(@typeInfo(T)) ++ " types (unimplemented)."),
1119 }1119 }
1120 }1120 }
1121 };1121 };
lib/std/math.zig+15-17
...@@ -1,6 +1,4 @@...@@ -1,6 +1,4 @@
1const builtin = @import("builtin");
2const std = @import("std.zig");1const std = @import("std.zig");
3const TypeId = builtin.TypeId;
4const assert = std.debug.assert;2const assert = std.debug.assert;
5const testing = std.testing;3const testing = std.testing;
64
...@@ -89,7 +87,7 @@ pub const snan = @import("math/nan.zig").snan;...@@ -89,7 +87,7 @@ pub const snan = @import("math/nan.zig").snan;
89pub const inf = @import("math/inf.zig").inf;87pub const inf = @import("math/inf.zig").inf;
9088
91pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {89pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {
92 assert(@typeId(T) == TypeId.Float);90 assert(@typeInfo(T) == .Float);
93 return fabs(x - y) < epsilon;91 return fabs(x - y) < epsilon;
94}92}
9593
...@@ -198,7 +196,7 @@ test "" {...@@ -198,7 +196,7 @@ test "" {
198}196}
199197
200pub fn floatMantissaBits(comptime T: type) comptime_int {198pub fn floatMantissaBits(comptime T: type) comptime_int {
201 assert(@typeId(T) == builtin.TypeId.Float);199 assert(@typeInfo(T) == .Float);
202200
203 return switch (T.bit_count) {201 return switch (T.bit_count) {
204 16 => 10,202 16 => 10,
...@@ -211,7 +209,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {...@@ -211,7 +209,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
211}209}
212210
213pub fn floatExponentBits(comptime T: type) comptime_int {211pub fn floatExponentBits(comptime T: type) comptime_int {
214 assert(@typeId(T) == builtin.TypeId.Float);212 assert(@typeInfo(T) == .Float);
215213
216 return switch (T.bit_count) {214 return switch (T.bit_count) {
217 16 => 5,215 16 => 5,
...@@ -526,7 +524,7 @@ fn testOverflow() void {...@@ -526,7 +524,7 @@ fn testOverflow() void {
526524
527pub fn absInt(x: var) !@TypeOf(x) {525pub fn absInt(x: var) !@TypeOf(x) {
528 const T = @TypeOf(x);526 const T = @TypeOf(x);
529 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt527 comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt
530 comptime assert(T.is_signed); // must pass a signed integer to absInt528 comptime assert(T.is_signed); // must pass a signed integer to absInt
531529
532 if (x == minInt(@TypeOf(x))) {530 if (x == minInt(@TypeOf(x))) {
...@@ -560,7 +558,7 @@ fn testAbsFloat() void {...@@ -560,7 +558,7 @@ fn testAbsFloat() void {
560pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {558pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
561 @setRuntimeSafety(false);559 @setRuntimeSafety(false);
562 if (denominator == 0) return error.DivisionByZero;560 if (denominator == 0) return error.DivisionByZero;
563 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;561 if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
564 return @divTrunc(numerator, denominator);562 return @divTrunc(numerator, denominator);
565}563}
566564
...@@ -581,7 +579,7 @@ fn testDivTrunc() void {...@@ -581,7 +579,7 @@ fn testDivTrunc() void {
581pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {579pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
582 @setRuntimeSafety(false);580 @setRuntimeSafety(false);
583 if (denominator == 0) return error.DivisionByZero;581 if (denominator == 0) return error.DivisionByZero;
584 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;582 if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
585 return @divFloor(numerator, denominator);583 return @divFloor(numerator, denominator);
586}584}
587585
...@@ -602,7 +600,7 @@ fn testDivFloor() void {...@@ -602,7 +600,7 @@ fn testDivFloor() void {
602pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {600pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
603 @setRuntimeSafety(false);601 @setRuntimeSafety(false);
604 if (denominator == 0) return error.DivisionByZero;602 if (denominator == 0) return error.DivisionByZero;
605 if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;603 if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow;
606 const result = @divTrunc(numerator, denominator);604 const result = @divTrunc(numerator, denominator);
607 if (result * denominator != numerator) return error.UnexpectedRemainder;605 if (result * denominator != numerator) return error.UnexpectedRemainder;
608 return result;606 return result;
...@@ -727,8 +725,8 @@ test "math.negateCast" {...@@ -727,8 +725,8 @@ test "math.negateCast" {
727/// Cast an integer to a different integer type. If the value doesn't fit,725/// Cast an integer to a different integer type. If the value doesn't fit,
728/// return an error.726/// return an error.
729pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {727pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
730 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer728 comptime assert(@typeInfo(T) == .Int); // must pass an integer
731 comptime assert(@typeId(@TypeOf(x)) == builtin.TypeId.Int); // must pass an integer729 comptime assert(@typeInfo(@TypeOf(x)) == .Int); // must pass an integer
732 if (maxInt(@TypeOf(x)) > maxInt(T) and x > maxInt(T)) {730 if (maxInt(@TypeOf(x)) > maxInt(T) and x > maxInt(T)) {
733 return error.Overflow;731 return error.Overflow;
734 } else if (minInt(@TypeOf(x)) < minInt(T) and x < minInt(T)) {732 } else if (minInt(@TypeOf(x)) < minInt(T) and x < minInt(T)) {
...@@ -793,7 +791,7 @@ fn testFloorPowerOfTwo() void {...@@ -793,7 +791,7 @@ fn testFloorPowerOfTwo() void {
793/// Only unsigned integers can be used. Zero is not an allowed input.791/// Only unsigned integers can be used. Zero is not an allowed input.
794/// Result is a type with 1 more bit than the input type.792/// Result is a type with 1 more bit than the input type.
795pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) @IntType(T.is_signed, T.bit_count + 1) {793pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) @IntType(T.is_signed, T.bit_count + 1) {
796 comptime assert(@typeId(T) == builtin.TypeId.Int);794 comptime assert(@typeInfo(T) == .Int);
797 comptime assert(!T.is_signed);795 comptime assert(!T.is_signed);
798 assert(value != 0);796 assert(value != 0);
799 comptime const PromotedType = @IntType(T.is_signed, T.bit_count + 1);797 comptime const PromotedType = @IntType(T.is_signed, T.bit_count + 1);
...@@ -805,7 +803,7 @@ pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) @IntType(T.is_signed, T...@@ -805,7 +803,7 @@ pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) @IntType(T.is_signed, T
805/// Only unsigned integers can be used. Zero is not an allowed input.803/// Only unsigned integers can be used. Zero is not an allowed input.
806/// If the value doesn't fit, returns an error.804/// If the value doesn't fit, returns an error.
807pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) {805pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) {
808 comptime assert(@typeId(T) == builtin.TypeId.Int);806 comptime assert(@typeInfo(T) == .Int);
809 comptime assert(!T.is_signed);807 comptime assert(!T.is_signed);
810 comptime const PromotedType = @IntType(T.is_signed, T.bit_count + 1);808 comptime const PromotedType = @IntType(T.is_signed, T.bit_count + 1);
811 comptime const overflowBit = @as(PromotedType, 1) << T.bit_count;809 comptime const overflowBit = @as(PromotedType, 1) << T.bit_count;
...@@ -878,10 +876,10 @@ test "std.math.log2_int_ceil" {...@@ -878,10 +876,10 @@ test "std.math.log2_int_ceil" {
878876
879pub fn lossyCast(comptime T: type, value: var) T {877pub fn lossyCast(comptime T: type, value: var) T {
880 switch (@typeInfo(@TypeOf(value))) {878 switch (@typeInfo(@TypeOf(value))) {
881 builtin.TypeId.Int => return @intToFloat(T, value),879 .Int => return @intToFloat(T, value),
882 builtin.TypeId.Float => return @floatCast(T, value),880 .Float => return @floatCast(T, value),
883 builtin.TypeId.ComptimeInt => return @as(T, value),881 .ComptimeInt => return @as(T, value),
884 builtin.TypeId.ComptimeFloat => return @as(T, value),882 .ComptimeFloat => return @as(T, value),
885 else => @compileError("bad type"),883 else => @compileError("bad type"),
886 }884 }
887}885}
lib/std/math/big/int.zig+4-7
...@@ -1,5 +1,4 @@...@@ -1,5 +1,4 @@
1const std = @import("../../std.zig");1const std = @import("../../std.zig");
2const builtin = @import("builtin");
3const debug = std.debug;2const debug = std.debug;
4const testing = std.testing;3const testing = std.testing;
5const math = std.math;4const math = std.math;
...@@ -9,8 +8,6 @@ const ArrayList = std.ArrayList;...@@ -9,8 +8,6 @@ const ArrayList = std.ArrayList;
9const maxInt = std.math.maxInt;8const maxInt = std.math.maxInt;
10const minInt = std.math.minInt;9const minInt = std.math.minInt;
1110
12const TypeId = builtin.TypeId;
13
14pub const Limb = usize;11pub const Limb = usize;
15pub const DoubleLimb = @IntType(false, 2 * Limb.bit_count);12pub const DoubleLimb = @IntType(false, 2 * Limb.bit_count);
16pub const Log2Limb = math.Log2Int(Limb);13pub const Log2Limb = math.Log2Int(Limb);
...@@ -270,7 +267,7 @@ pub const Int = struct {...@@ -270,7 +267,7 @@ pub const Int = struct {
270 const T = @TypeOf(value);267 const T = @TypeOf(value);
271268
272 switch (@typeInfo(T)) {269 switch (@typeInfo(T)) {
273 TypeId.Int => |info| {270 .Int => |info| {
274 const UT = if (T.is_signed) @IntType(false, T.bit_count - 1) else T;271 const UT = if (T.is_signed) @IntType(false, T.bit_count - 1) else T;
275272
276 try self.ensureCapacity(@sizeOf(UT) / @sizeOf(Limb));273 try self.ensureCapacity(@sizeOf(UT) / @sizeOf(Limb));
...@@ -294,7 +291,7 @@ pub const Int = struct {...@@ -294,7 +291,7 @@ pub const Int = struct {
294 }291 }
295 }292 }
296 },293 },
297 TypeId.ComptimeInt => {294 .ComptimeInt => {
298 comptime var w_value = if (value < 0) -value else value;295 comptime var w_value = if (value < 0) -value else value;
299296
300 const req_limbs = @divFloor(math.log2(w_value), Limb.bit_count) + 1;297 const req_limbs = @divFloor(math.log2(w_value), Limb.bit_count) + 1;
...@@ -332,8 +329,8 @@ pub const Int = struct {...@@ -332,8 +329,8 @@ pub const Int = struct {
332 ///329 ///
333 /// Returns an error if self cannot be narrowed into the requested type without truncation.330 /// Returns an error if self cannot be narrowed into the requested type without truncation.
334 pub fn to(self: Int, comptime T: type) ConvertError!T {331 pub fn to(self: Int, comptime T: type) ConvertError!T {
335 switch (@typeId(T)) {332 switch (@typeInfo(T)) {
336 TypeId.Int => {333 .Int => {
337 const UT = @IntType(false, T.bit_count);334 const UT = @IntType(false, T.bit_count);
338335
339 if (self.bitCountTwosComp() > T.bit_count) {336 if (self.bitCountTwosComp() > T.bit_count) {
lib/std/math/big/rational.zig+3-6
...@@ -1,5 +1,4 @@...@@ -1,5 +1,4 @@
1const std = @import("../../std.zig");1const std = @import("../../std.zig");
2const builtin = @import("builtin");
3const debug = std.debug;2const debug = std.debug;
4const math = std.math;3const math = std.math;
5const mem = std.mem;4const mem = std.mem;
...@@ -7,8 +6,6 @@ const testing = std.testing;...@@ -7,8 +6,6 @@ const testing = std.testing;
7const Allocator = mem.Allocator;6const Allocator = mem.Allocator;
8const ArrayList = std.ArrayList;7const ArrayList = std.ArrayList;
98
10const TypeId = builtin.TypeId;
11
12const bn = @import("int.zig");9const bn = @import("int.zig");
13const Limb = bn.Limb;10const Limb = bn.Limb;
14const DoubleLimb = bn.DoubleLimb;11const DoubleLimb = bn.DoubleLimb;
...@@ -129,7 +126,7 @@ pub const Rational = struct {...@@ -129,7 +126,7 @@ pub const Rational = struct {
129 /// completely represent the provided float.126 /// completely represent the provided float.
130 pub fn setFloat(self: *Rational, comptime T: type, f: T) !void {127 pub fn setFloat(self: *Rational, comptime T: type, f: T) !void {
131 // Translated from golang.go/src/math/big/rat.go.128 // Translated from golang.go/src/math/big/rat.go.
132 debug.assert(@typeId(T) == builtin.TypeId.Float);129 debug.assert(@typeInfo(T) == .Float);
133130
134 const UnsignedIntType = @IntType(false, T.bit_count);131 const UnsignedIntType = @IntType(false, T.bit_count);
135 const f_bits = @bitCast(UnsignedIntType, f);132 const f_bits = @bitCast(UnsignedIntType, f);
...@@ -187,7 +184,7 @@ pub const Rational = struct {...@@ -187,7 +184,7 @@ pub const Rational = struct {
187 pub fn toFloat(self: Rational, comptime T: type) !T {184 pub fn toFloat(self: Rational, comptime T: type) !T {
188 // Translated from golang.go/src/math/big/rat.go.185 // Translated from golang.go/src/math/big/rat.go.
189 // TODO: Indicate whether the result is not exact.186 // TODO: Indicate whether the result is not exact.
190 debug.assert(@typeId(T) == builtin.TypeId.Float);187 debug.assert(@typeInfo(T) == .Float);
191188
192 const fsize = T.bit_count;189 const fsize = T.bit_count;
193 const BitReprType = @IntType(false, T.bit_count);190 const BitReprType = @IntType(false, T.bit_count);
...@@ -653,7 +650,7 @@ test "big.rational gcd one large" {...@@ -653,7 +650,7 @@ test "big.rational gcd one large" {
653}650}
654651
655fn extractLowBits(a: Int, comptime T: type) T {652fn extractLowBits(a: Int, comptime T: type) T {
656 testing.expect(@typeId(T) == builtin.TypeId.Int);653 testing.expect(@typeInfo(T) == .Int);
657654
658 if (T.bit_count <= Limb.bit_count) {655 if (T.bit_count <= Limb.bit_count) {
659 return @truncate(T, a.limbs[0]);656 return @truncate(T, a.limbs[0]);
lib/std/math/ln.zig+5-7
...@@ -7,8 +7,6 @@...@@ -7,8 +7,6 @@
7const std = @import("../std.zig");7const std = @import("../std.zig");
8const math = std.math;8const math = std.math;
9const expect = std.testing.expect;9const expect = std.testing.expect;
10const builtin = @import("builtin");
11const TypeId = builtin.TypeId;
1210
13/// Returns the natural logarithm of x.11/// Returns the natural logarithm of x.
14///12///
...@@ -19,21 +17,21 @@ const TypeId = builtin.TypeId;...@@ -19,21 +17,21 @@ const TypeId = builtin.TypeId;
19/// - ln(nan) = nan17/// - ln(nan) = nan
20pub fn ln(x: var) @TypeOf(x) {18pub fn ln(x: var) @TypeOf(x) {
21 const T = @TypeOf(x);19 const T = @TypeOf(x);
22 switch (@typeId(T)) {20 switch (@typeInfo(T)) {
23 TypeId.ComptimeFloat => {21 .ComptimeFloat => {
24 return @as(comptime_float, ln_64(x));22 return @as(comptime_float, ln_64(x));
25 },23 },
26 TypeId.Float => {24 .Float => {
27 return switch (T) {25 return switch (T) {
28 f32 => ln_32(x),26 f32 => ln_32(x),
29 f64 => ln_64(x),27 f64 => ln_64(x),
30 else => @compileError("ln not implemented for " ++ @typeName(T)),28 else => @compileError("ln not implemented for " ++ @typeName(T)),
31 };29 };
32 },30 },
33 TypeId.ComptimeInt => {31 .ComptimeInt => {
34 return @as(comptime_int, math.floor(ln_64(@as(f64, x))));32 return @as(comptime_int, math.floor(ln_64(@as(f64, x))));
35 },33 },
36 TypeId.Int => {34 .Int => {
37 return @as(T, math.floor(ln_64(@as(f64, x))));35 return @as(T, math.floor(ln_64(@as(f64, x))));
38 },36 },
39 else => @compileError("ln not implemented for " ++ @typeName(T)),37 else => @compileError("ln not implemented for " ++ @typeName(T)),
lib/std/math/log.zig+6-8
...@@ -6,8 +6,6 @@...@@ -6,8 +6,6 @@
66
7const std = @import("../std.zig");7const std = @import("../std.zig");
8const math = std.math;8const math = std.math;
9const builtin = @import("builtin");
10const TypeId = builtin.TypeId;
11const expect = std.testing.expect;9const expect = std.testing.expect;
1210
13/// Returns the logarithm of x for the provided base.11/// Returns the logarithm of x for the provided base.
...@@ -16,24 +14,24 @@ pub fn log(comptime T: type, base: T, x: T) T {...@@ -16,24 +14,24 @@ pub fn log(comptime T: type, base: T, x: T) T {
16 return math.log2(x);14 return math.log2(x);
17 } else if (base == 10) {15 } else if (base == 10) {
18 return math.log10(x);16 return math.log10(x);
19 } else if ((@typeId(T) == TypeId.Float or @typeId(T) == TypeId.ComptimeFloat) and base == math.e) {17 } else if ((@typeInfo(T) == .Float or @typeInfo(T) == .ComptimeFloat) and base == math.e) {
20 return math.ln(x);18 return math.ln(x);
21 }19 }
2220
23 const float_base = math.lossyCast(f64, base);21 const float_base = math.lossyCast(f64, base);
24 switch (@typeId(T)) {22 switch (@typeInfo(T)) {
25 TypeId.ComptimeFloat => {23 .ComptimeFloat => {
26 return @as(comptime_float, math.ln(@as(f64, x)) / math.ln(float_base));24 return @as(comptime_float, math.ln(@as(f64, x)) / math.ln(float_base));
27 },25 },
28 TypeId.ComptimeInt => {26 .ComptimeInt => {
29 return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base)));27 return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base)));
30 },28 },
31 builtin.TypeId.Int => {29 .Int => {
32 // TODO implement integer log without using float math30 // TODO implement integer log without using float math
33 return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base)));31 return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base)));
34 },32 },
3533
36 builtin.TypeId.Float => {34 .Float => {
37 switch (T) {35 switch (T) {
38 f32 => return @floatCast(f32, math.ln(@as(f64, x)) / math.ln(float_base)),36 f32 => return @floatCast(f32, math.ln(@as(f64, x)) / math.ln(float_base)),
39 f64 => return math.ln(x) / math.ln(float_base),37 f64 => return math.ln(x) / math.ln(float_base),
lib/std/math/log10.zig+5-7
...@@ -7,8 +7,6 @@...@@ -7,8 +7,6 @@
7const std = @import("../std.zig");7const std = @import("../std.zig");
8const math = std.math;8const math = std.math;
9const testing = std.testing;9const testing = std.testing;
10const builtin = @import("builtin");
11const TypeId = builtin.TypeId;
12const maxInt = std.math.maxInt;10const maxInt = std.math.maxInt;
1311
14/// Returns the base-10 logarithm of x.12/// Returns the base-10 logarithm of x.
...@@ -20,21 +18,21 @@ const maxInt = std.math.maxInt;...@@ -20,21 +18,21 @@ const maxInt = std.math.maxInt;
20/// - log10(nan) = nan18/// - log10(nan) = nan
21pub fn log10(x: var) @TypeOf(x) {19pub fn log10(x: var) @TypeOf(x) {
22 const T = @TypeOf(x);20 const T = @TypeOf(x);
23 switch (@typeId(T)) {21 switch (@typeInfo(T)) {
24 TypeId.ComptimeFloat => {22 .ComptimeFloat => {
25 return @as(comptime_float, log10_64(x));23 return @as(comptime_float, log10_64(x));
26 },24 },
27 TypeId.Float => {25 .Float => {
28 return switch (T) {26 return switch (T) {
29 f32 => log10_32(x),27 f32 => log10_32(x),
30 f64 => log10_64(x),28 f64 => log10_64(x),
31 else => @compileError("log10 not implemented for " ++ @typeName(T)),29 else => @compileError("log10 not implemented for " ++ @typeName(T)),
32 };30 };
33 },31 },
34 TypeId.ComptimeInt => {32 .ComptimeInt => {
35 return @as(comptime_int, math.floor(log10_64(@as(f64, x))));33 return @as(comptime_int, math.floor(log10_64(@as(f64, x))));
36 },34 },
37 TypeId.Int => {35 .Int => {
38 return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x))));36 return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x))));
39 },37 },
40 else => @compileError("log10 not implemented for " ++ @typeName(T)),38 else => @compileError("log10 not implemented for " ++ @typeName(T)),
lib/std/math/log2.zig+5-7
...@@ -7,8 +7,6 @@...@@ -7,8 +7,6 @@
7const std = @import("../std.zig");7const std = @import("../std.zig");
8const math = std.math;8const math = std.math;
9const expect = std.testing.expect;9const expect = std.testing.expect;
10const builtin = @import("builtin");
11const TypeId = builtin.TypeId;
12const maxInt = std.math.maxInt;10const maxInt = std.math.maxInt;
1311
14/// Returns the base-2 logarithm of x.12/// Returns the base-2 logarithm of x.
...@@ -20,18 +18,18 @@ const maxInt = std.math.maxInt;...@@ -20,18 +18,18 @@ const maxInt = std.math.maxInt;
20/// - log2(nan) = nan18/// - log2(nan) = nan
21pub fn log2(x: var) @TypeOf(x) {19pub fn log2(x: var) @TypeOf(x) {
22 const T = @TypeOf(x);20 const T = @TypeOf(x);
23 switch (@typeId(T)) {21 switch (@typeInfo(T)) {
24 TypeId.ComptimeFloat => {22 .ComptimeFloat => {
25 return @as(comptime_float, log2_64(x));23 return @as(comptime_float, log2_64(x));
26 },24 },
27 TypeId.Float => {25 .Float => {
28 return switch (T) {26 return switch (T) {
29 f32 => log2_32(x),27 f32 => log2_32(x),
30 f64 => log2_64(x),28 f64 => log2_64(x),
31 else => @compileError("log2 not implemented for " ++ @typeName(T)),29 else => @compileError("log2 not implemented for " ++ @typeName(T)),
32 };30 };
33 },31 },
34 TypeId.ComptimeInt => comptime {32 .ComptimeInt => comptime {
35 var result = 0;33 var result = 0;
36 var x_shifted = x;34 var x_shifted = x;
37 while (b: {35 while (b: {
...@@ -40,7 +38,7 @@ pub fn log2(x: var) @TypeOf(x) {...@@ -40,7 +38,7 @@ pub fn log2(x: var) @TypeOf(x) {
40 }) : (result += 1) {}38 }) : (result += 1) {}
41 return result;39 return result;
42 },40 },
43 TypeId.Int => {41 .Int => {
44 return math.log2_int(T, x);42 return math.log2_int(T, x);
45 },43 },
46 else => @compileError("log2 not implemented for " ++ @typeName(T)),44 else => @compileError("log2 not implemented for " ++ @typeName(T)),
lib/std/meta.zig+2-3
...@@ -536,9 +536,8 @@ test "intToEnum with error return" {...@@ -536,9 +536,8 @@ test "intToEnum with error return" {
536pub const IntToEnumError = error{InvalidEnumTag};536pub const IntToEnumError = error{InvalidEnumTag};
537537
538pub fn intToEnum(comptime Tag: type, tag_int: var) IntToEnumError!Tag {538pub fn intToEnum(comptime Tag: type, tag_int: var) IntToEnumError!Tag {
539 comptime var i = 0;539 inline for (@typeInfo(Tag).Enum.fields) |f| {
540 inline while (i != @memberCount(Tag)) : (i += 1) {540 const this_tag_value = @field(Tag, f.name);
541 const this_tag_value = @field(Tag, @memberName(Tag, i));
542 if (tag_int == @enumToInt(this_tag_value)) {541 if (tag_int == @enumToInt(this_tag_value)) {
543 return this_tag_value;542 return this_tag_value;
544 }543 }
lib/std/meta/trait.zig+7-7
...@@ -1,5 +1,5 @@...@@ -1,5 +1,5 @@
1const std = @import("../std.zig");1const std = @import("../std.zig");
2const builtin = @import("builtin");2const builtin = std.builtin;
3const mem = std.mem;3const mem = std.mem;
4const debug = std.debug;4const debug = std.debug;
5const testing = std.testing;5const testing = std.testing;
...@@ -54,7 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn {...@@ -54,7 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn {
54 if (!comptime isContainer(T)) return false;54 if (!comptime isContainer(T)) return false;
55 if (!comptime @hasDecl(T, name)) return false;55 if (!comptime @hasDecl(T, name)) return false;
56 const DeclType = @TypeOf(@field(T, name));56 const DeclType = @TypeOf(@field(T, name));
57 return @typeId(DeclType) == .Fn;57 return @typeInfo(DeclType) == .Fn;
58 }58 }
59 };59 };
60 return Closure.trait;60 return Closure.trait;
...@@ -105,7 +105,7 @@ test "std.meta.trait.hasField" {...@@ -105,7 +105,7 @@ test "std.meta.trait.hasField" {
105pub fn is(comptime id: builtin.TypeId) TraitFn {105pub fn is(comptime id: builtin.TypeId) TraitFn {
106 const Closure = struct {106 const Closure = struct {
107 pub fn trait(comptime T: type) bool {107 pub fn trait(comptime T: type) bool {
108 return id == @typeId(T);108 return id == @typeInfo(T);
109 }109 }
110 };110 };
111 return Closure.trait;111 return Closure.trait;
...@@ -123,7 +123,7 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {...@@ -123,7 +123,7 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {
123 const Closure = struct {123 const Closure = struct {
124 pub fn trait(comptime T: type) bool {124 pub fn trait(comptime T: type) bool {
125 if (!comptime isSingleItemPtr(T)) return false;125 if (!comptime isSingleItemPtr(T)) return false;
126 return id == @typeId(meta.Child(T));126 return id == @typeInfo(meta.Child(T));
127 }127 }
128 };128 };
129 return Closure.trait;129 return Closure.trait;
...@@ -139,7 +139,7 @@ pub fn isSliceOf(comptime id: builtin.TypeId) TraitFn {...@@ -139,7 +139,7 @@ pub fn isSliceOf(comptime id: builtin.TypeId) TraitFn {
139 const Closure = struct {139 const Closure = struct {
140 pub fn trait(comptime T: type) bool {140 pub fn trait(comptime T: type) bool {
141 if (!comptime isSlice(T)) return false;141 if (!comptime isSlice(T)) return false;
142 return id == @typeId(meta.Child(T));142 return id == @typeInfo(meta.Child(T));
143 }143 }
144 };144 };
145 return Closure.trait;145 return Closure.trait;
...@@ -285,7 +285,7 @@ test "std.meta.trait.isIndexable" {...@@ -285,7 +285,7 @@ test "std.meta.trait.isIndexable" {
285}285}
286286
287pub fn isNumber(comptime T: type) bool {287pub fn isNumber(comptime T: type) bool {
288 return switch (@typeId(T)) {288 return switch (@typeInfo(T)) {
289 .Int, .Float, .ComptimeInt, .ComptimeFloat => true,289 .Int, .Float, .ComptimeInt, .ComptimeFloat => true,
290 else => false,290 else => false,
291 };291 };
...@@ -320,7 +320,7 @@ test "std.meta.trait.isConstPtr" {...@@ -320,7 +320,7 @@ test "std.meta.trait.isConstPtr" {
320}320}
321321
322pub fn isContainer(comptime T: type) bool {322pub fn isContainer(comptime T: type) bool {
323 return switch (@typeId(T)) {323 return switch (@typeInfo(T)) {
324 .Struct, .Union, .Enum => true,324 .Struct, .Union, .Enum => true,
325 else => false,325 else => false,
326 };326 };
lib/std/special/build_runner.zig+1-1
...@@ -126,7 +126,7 @@ pub fn main() !void {...@@ -126,7 +126,7 @@ pub fn main() !void {
126}126}
127127
128fn runBuild(builder: *Builder) anyerror!void {128fn runBuild(builder: *Builder) anyerror!void {
129 switch (@typeId(@TypeOf(root.build).ReturnType)) {129 switch (@typeInfo(@TypeOf(root.build).ReturnType)) {
130 .Void => root.build(builder),130 .Void => root.build(builder),
131 .ErrorUnion => try root.build(builder),131 .ErrorUnion => try root.build(builder),
132 else => @compileError("expected return type of build to be 'void' or '!void'"),132 else => @compileError("expected return type of build to be 'void' or '!void'"),
lib/std/thread.zig+2-2
...@@ -158,7 +158,7 @@ pub const Thread = struct {...@@ -158,7 +158,7 @@ pub const Thread = struct {
158 };158 };
159 fn threadMain(raw_arg: windows.LPVOID) callconv(.C) windows.DWORD {159 fn threadMain(raw_arg: windows.LPVOID) callconv(.C) windows.DWORD {
160 const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), raw_arg)).*;160 const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), raw_arg)).*;
161 switch (@typeId(@TypeOf(startFn).ReturnType)) {161 switch (@typeInfo(@TypeOf(startFn).ReturnType)) {
162 .Int => {162 .Int => {
163 return startFn(arg);163 return startFn(arg);
164 },164 },
...@@ -201,7 +201,7 @@ pub const Thread = struct {...@@ -201,7 +201,7 @@ pub const Thread = struct {
201 fn linuxThreadMain(ctx_addr: usize) callconv(.C) u8 {201 fn linuxThreadMain(ctx_addr: usize) callconv(.C) u8 {
202 const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*;202 const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*;
203203
204 switch (@typeId(@TypeOf(startFn).ReturnType)) {204 switch (@typeInfo(@TypeOf(startFn).ReturnType)) {
205 .Int => {205 .Int => {
206 return startFn(arg);206 return startFn(arg);
207 },207 },
lib/std/zig/ast.zig+12-16
...@@ -457,10 +457,9 @@ pub const Node = struct {...@@ -457,10 +457,9 @@ pub const Node = struct {
457 }457 }
458458
459 pub fn iterate(base: *Node, index: usize) ?*Node {459 pub fn iterate(base: *Node, index: usize) ?*Node {
460 comptime var i = 0;460 inline for (@typeInfo(Id).Enum.fields) |f| {
461 inline while (i < @memberCount(Id)) : (i += 1) {461 if (base.id == @field(Id, f.name)) {
462 if (base.id == @field(Id, @memberName(Id, i))) {462 const T = @field(Node, f.name);
463 const T = @field(Node, @memberName(Id, i));
464 return @fieldParentPtr(T, "base", base).iterate(index);463 return @fieldParentPtr(T, "base", base).iterate(index);
465 }464 }
466 }465 }
...@@ -468,10 +467,9 @@ pub const Node = struct {...@@ -468,10 +467,9 @@ pub const Node = struct {
468 }467 }
469468
470 pub fn firstToken(base: *const Node) TokenIndex {469 pub fn firstToken(base: *const Node) TokenIndex {
471 comptime var i = 0;470 inline for (@typeInfo(Id).Enum.fields) |f| {
472 inline while (i < @memberCount(Id)) : (i += 1) {471 if (base.id == @field(Id, f.name)) {
473 if (base.id == @field(Id, @memberName(Id, i))) {472 const T = @field(Node, f.name);
474 const T = @field(Node, @memberName(Id, i));
475 return @fieldParentPtr(T, "base", base).firstToken();473 return @fieldParentPtr(T, "base", base).firstToken();
476 }474 }
477 }475 }
...@@ -479,10 +477,9 @@ pub const Node = struct {...@@ -479,10 +477,9 @@ pub const Node = struct {
479 }477 }
480478
481 pub fn lastToken(base: *const Node) TokenIndex {479 pub fn lastToken(base: *const Node) TokenIndex {
482 comptime var i = 0;480 inline for (@typeInfo(Id).Enum.fields) |f| {
483 inline while (i < @memberCount(Id)) : (i += 1) {481 if (base.id == @field(Id, f.name)) {
484 if (base.id == @field(Id, @memberName(Id, i))) {482 const T = @field(Node, f.name);
485 const T = @field(Node, @memberName(Id, i));
486 return @fieldParentPtr(T, "base", base).lastToken();483 return @fieldParentPtr(T, "base", base).lastToken();
487 }484 }
488 }485 }
...@@ -490,10 +487,9 @@ pub const Node = struct {...@@ -490,10 +487,9 @@ pub const Node = struct {
490 }487 }
491488
492 pub fn typeToId(comptime T: type) Id {489 pub fn typeToId(comptime T: type) Id {
493 comptime var i = 0;490 inline for (@typeInfo(Id).Enum.fields) |f| {
494 inline while (i < @memberCount(Id)) : (i += 1) {491 if (T == @field(Node, f.name)) {
495 if (T == @field(Node, @memberName(Id, i))) {492 return @field(Id, f.name);
496 return @field(Id, @memberName(Id, i));
497 }493 }
498 }494 }
499 unreachable;495 unreachable;
lib/std/zig/parser_test.zig+1-1
...@@ -1410,7 +1410,7 @@ test "zig fmt: same-line comment after non-block if expression" {...@@ -1410,7 +1410,7 @@ test "zig fmt: same-line comment after non-block if expression" {
1410test "zig fmt: same-line comment on comptime expression" {1410test "zig fmt: same-line comment on comptime expression" {
1411 try testCanonical(1411 try testCanonical(
1412 \\test "" {1412 \\test "" {
1413 \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt1413 \\ comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt
1414 \\}1414 \\}
1415 \\1415 \\
1416 );1416 );
lib/std/zig/render.zig+1-2
...@@ -1,5 +1,4 @@...@@ -1,5 +1,4 @@
1const std = @import("../std.zig");1const std = @import("../std.zig");
2const builtin = @import("builtin");
3const assert = std.debug.assert;2const assert = std.debug.assert;
4const mem = std.mem;3const mem = std.mem;
5const ast = std.zig.ast;4const ast = std.zig.ast;
...@@ -14,7 +13,7 @@ pub const Error = error{...@@ -14,7 +13,7 @@ pub const Error = error{
1413
15/// Returns whether anything changed14/// Returns whether anything changed
16pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(stream).Child.Error || Error)!bool {15pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(stream).Child.Error || Error)!bool {
17 comptime assert(@typeId(@TypeOf(stream)) == builtin.TypeId.Pointer);16 comptime assert(@typeInfo(@TypeOf(stream)) == .Pointer);
1817
19 var anything_changed: bool = false;18 var anything_changed: bool = false;
2019
src-self-hosted/ir.zig+17-22
...@@ -76,20 +76,18 @@ pub const Inst = struct {...@@ -76,20 +76,18 @@ pub const Inst = struct {
76 }76 }
7777
78 pub fn typeToId(comptime T: type) Id {78 pub fn typeToId(comptime T: type) Id {
79 comptime var i = 0;79 inline for (@typeInfo(Id).Enum.fields) |f| {
80 inline while (i < @memberCount(Id)) : (i += 1) {80 if (T == @field(Inst, f.name)) {
81 if (T == @field(Inst, @memberName(Id, i))) {81 return @field(Id, f.name);
82 return @field(Id, @memberName(Id, i));
83 }82 }
84 }83 }
85 unreachable;84 unreachable;
86 }85 }
8786
88 pub fn dump(base: *const Inst) void {87 pub fn dump(base: *const Inst) void {
89 comptime var i = 0;88 inline for (@typeInfo(Id).Enum.fields) |f| {
90 inline while (i < @memberCount(Id)) : (i += 1) {89 if (base.id == @field(Id, f.name)) {
91 if (base.id == @field(Id, @memberName(Id, i))) {90 const T = @field(Inst, f.name);
92 const T = @field(Inst, @memberName(Id, i));
93 std.debug.warn("#{} = {}(", .{ base.debug_id, @tagName(base.id) });91 std.debug.warn("#{} = {}(", .{ base.debug_id, @tagName(base.id) });
94 @fieldParentPtr(T, "base", base).dump();92 @fieldParentPtr(T, "base", base).dump();
95 std.debug.warn(")", .{});93 std.debug.warn(")", .{});
...@@ -100,10 +98,9 @@ pub const Inst = struct {...@@ -100,10 +98,9 @@ pub const Inst = struct {
100 }98 }
10199
102 pub fn hasSideEffects(base: *const Inst) bool {100 pub fn hasSideEffects(base: *const Inst) bool {
103 comptime var i = 0;101 inline for (@typeInfo(Id).Enum.fields) |f| {
104 inline while (i < @memberCount(Id)) : (i += 1) {102 if (base.id == @field(Id, f.name)) {
105 if (base.id == @field(Id, @memberName(Id, i))) {103 const T = @field(Inst, f.name);
106 const T = @field(Inst, @memberName(Id, i));
107 return @fieldParentPtr(T, "base", base).hasSideEffects();104 return @fieldParentPtr(T, "base", base).hasSideEffects();
108 }105 }
109 }106 }
...@@ -1805,21 +1802,19 @@ pub const Builder = struct {...@@ -1805,21 +1802,19 @@ pub const Builder = struct {
1805 };1802 };
18061803
1807 // Look at the params and ref() other instructions1804 // Look at the params and ref() other instructions
1808 comptime var i = 0;1805 inline for (@typeInfo(I.Params).Struct.fields) |f| {
1809 inline while (i < @memberCount(I.Params)) : (i += 1) {1806 switch (f.fiedl_type) {
1810 const FieldType = comptime @TypeOf(@field(@as(I.Params, undefined), @memberName(I.Params, i)));1807 *Inst => @field(inst.params, f.name).ref(self),
1811 switch (FieldType) {1808 *BasicBlock => @field(inst.params, f.name).ref(self),
1812 *Inst => @field(inst.params, @memberName(I.Params, i)).ref(self),1809 ?*Inst => if (@field(inst.params, f.name)) |other| other.ref(self),
1813 *BasicBlock => @field(inst.params, @memberName(I.Params, i)).ref(self),
1814 ?*Inst => if (@field(inst.params, @memberName(I.Params, i))) |other| other.ref(self),
1815 []*Inst => {1810 []*Inst => {
1816 // TODO https://github.com/ziglang/zig/issues/12691811 // TODO https://github.com/ziglang/zig/issues/1269
1817 for (@field(inst.params, @memberName(I.Params, i))) |other|1812 for (@field(inst.params, f.name)) |other|
1818 other.ref(self);1813 other.ref(self);
1819 },1814 },
1820 []*BasicBlock => {1815 []*BasicBlock => {
1821 // TODO https://github.com/ziglang/zig/issues/12691816 // TODO https://github.com/ziglang/zig/issues/1269
1822 for (@field(inst.params, @memberName(I.Params, i))) |other|1817 for (@field(inst.params, f.name)) |other|
1823 other.ref(self);1818 other.ref(self);
1824 },1819 },
1825 Type.Pointer.Mut,1820 Type.Pointer.Mut,
...@@ -1831,7 +1826,7 @@ pub const Builder = struct {...@@ -1831,7 +1826,7 @@ pub const Builder = struct {
1831 => {},1826 => {},
1832 // it's ok to add more types here, just make sure that1827 // it's ok to add more types here, just make sure that
1833 // any instructions and basic blocks are ref'd appropriately1828 // any instructions and basic blocks are ref'd appropriately
1834 else => @compileError("unrecognized type in Params: " ++ @typeName(FieldType)),1829 else => @compileError("unrecognized type in Params: " ++ @typeName(f.field_type)),
1835 }1830 }
1836 }1831 }
18371832
src-self-hosted/translate_c.zig+4-4
...@@ -5381,15 +5381,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5381,15 +5381,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5381 return error.ParseError;5381 return error.ParseError;
5382 }5382 }
53835383
5384 //if (@typeId(@TypeOf(x)) == .Pointer)5384 //if (@typeInfo(@TypeOf(x)) == .Pointer)
5385 // @ptrCast(dest, x)5385 // @ptrCast(dest, x)
5386 //else if (@typeId(@TypeOf(x)) == .Integer)5386 //else if (@typeInfo(@TypeOf(x)) == .Integer)
5387 // @intToPtr(dest, x)5387 // @intToPtr(dest, x)
5388 //else5388 //else
5389 // @as(dest, x)5389 // @as(dest, x)
53905390
5391 const if_1 = try transCreateNodeIf(c);5391 const if_1 = try transCreateNodeIf(c);
5392 const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeId");5392 const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
5393 const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");5393 const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
5394 try type_id_1.params.push(&type_of_1.base);5394 try type_id_1.params.push(&type_of_1.base);
5395 try type_of_1.params.push(node_to_cast);5395 try type_of_1.params.push(node_to_cast);
...@@ -5416,7 +5416,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,...@@ -5416,7 +5416,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
5416 if_1.@"else" = else_1;5416 if_1.@"else" = else_1;
54175417
5418 const if_2 = try transCreateNodeIf(c);5418 const if_2 = try transCreateNodeIf(c);
5419 const type_id_2 = try transCreateNodeBuiltinFnCall(c, "@typeId");5419 const type_id_2 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
5420 const type_of_2 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");5420 const type_of_2 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
5421 try type_id_2.params.push(&type_of_2.base);5421 try type_id_2.params.push(&type_of_2.base);
5422 try type_of_2.params.push(node_to_cast);5422 try type_of_2.params.push(node_to_cast);
test/compile_errors.zig-78
...@@ -2942,14 +2942,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -2942,14 +2942,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
2942 "tmp.zig:11:13: error: error.B not a member of error set 'Set2'",2942 "tmp.zig:11:13: error: error.B not a member of error set 'Set2'",
2943 });2943 });
29442944
2945 cases.add("@memberCount of error",
2946 \\comptime {
2947 \\ _ = @memberCount(anyerror);
2948 \\}
2949 , &[_][]const u8{
2950 "tmp.zig:2:9: error: global error set member count not available at comptime",
2951 });
2952
2953 cases.add("duplicate error value in error set",2945 cases.add("duplicate error value in error set",
2954 \\const Foo = error {2946 \\const Foo = error {
2955 \\ Bar,2947 \\ Bar,
...@@ -5964,76 +5956,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {...@@ -5964,76 +5956,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
5964 "tmp.zig:2:32: error: arg index 2 out of bounds; 'fn(i32, i32) i32' has 2 arguments",5956 "tmp.zig:2:32: error: arg index 2 out of bounds; 'fn(i32, i32) i32' has 2 arguments",
5965 });5957 });
59665958
5967 cases.add("@memberType on unsupported type",
5968 \\comptime {
5969 \\ _ = @memberType(i32, 0);
5970 \\}
5971 , &[_][]const u8{
5972 "tmp.zig:2:21: error: type 'i32' does not support @memberType",
5973 });
5974
5975 cases.add("@memberType on enum",
5976 \\comptime {
5977 \\ _ = @memberType(Foo, 0);
5978 \\}
5979 \\const Foo = enum {A,};
5980 , &[_][]const u8{
5981 "tmp.zig:2:21: error: type 'Foo' does not support @memberType",
5982 });
5983
5984 cases.add("@memberType struct out of bounds",
5985 \\comptime {
5986 \\ _ = @memberType(Foo, 0);
5987 \\}
5988 \\const Foo = struct {};
5989 , &[_][]const u8{
5990 "tmp.zig:2:26: error: member index 0 out of bounds; 'Foo' has 0 members",
5991 });
5992
5993 cases.add("@memberType union out of bounds",
5994 \\comptime {
5995 \\ _ = @memberType(Foo, 1);
5996 \\}
5997 \\const Foo = union {A: void,};
5998 , &[_][]const u8{
5999 "tmp.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members",
6000 });
6001
6002 cases.add("@memberName on unsupported type",
6003 \\comptime {
6004 \\ _ = @memberName(i32, 0);
6005 \\}
6006 , &[_][]const u8{
6007 "tmp.zig:2:21: error: type 'i32' does not support @memberName",
6008 });
6009
6010 cases.add("@memberName struct out of bounds",
6011 \\comptime {
6012 \\ _ = @memberName(Foo, 0);
6013 \\}
6014 \\const Foo = struct {};
6015 , &[_][]const u8{
6016 "tmp.zig:2:26: error: member index 0 out of bounds; 'Foo' has 0 members",
6017 });
6018
6019 cases.add("@memberName enum out of bounds",
6020 \\comptime {
6021 \\ _ = @memberName(Foo, 1);
6022 \\}
6023 \\const Foo = enum {A,};
6024 , &[_][]const u8{
6025 "tmp.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members",
6026 });
6027
6028 cases.add("@memberName union out of bounds",
6029 \\comptime {
6030 \\ _ = @memberName(Foo, 1);
6031 \\}
6032 \\const Foo = union {A:i32,};
6033 , &[_][]const u8{
6034 "tmp.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members",
6035 });
6036
6037 cases.add("calling var args extern function, passing array instead of pointer",5959 cases.add("calling var args extern function, passing array instead of pointer",
6038 \\export fn entry() void {5960 \\export fn entry() void {
6039 \\ foo("hello".*,);5961 \\ foo("hello".*,);
test/stage1/behavior/bugs/3742.zig+1-1
...@@ -17,7 +17,7 @@ pub const GET = struct {...@@ -17,7 +17,7 @@ pub const GET = struct {
17};17};
1818
19pub fn isCommand(comptime T: type) bool {19pub fn isCommand(comptime T: type) bool {
20 const tid = @typeId(T);20 const tid = @typeInfo(T);
21 return (tid == .Struct or tid == .Enum or tid == .Union) and21 return (tid == .Struct or tid == .Enum or tid == .Union) and
22 @hasDecl(T, "Redis") and @hasDecl(T.Redis, "Command");22 @hasDecl(T, "Redis") and @hasDecl(T.Redis, "Command");
23}23}
test/stage1/behavior/enum.zig+2-2
...@@ -96,8 +96,8 @@ test "enum type" {...@@ -96,8 +96,8 @@ test "enum type" {
96 const bar = Bar.B;96 const bar = Bar.B;
9797
98 expect(bar == Bar.B);98 expect(bar == Bar.B);
99 expect(@memberCount(Foo) == 3);99 expect(@typeInfo(Foo).Union.fields.len == 3);
100 expect(@memberCount(Bar) == 4);100 expect(@typeInfo(Bar).Enum.fields.len == 4);
101 expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));101 expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));
102 expect(@sizeOf(Bar) == 1);102 expect(@sizeOf(Bar) == 1);
103}103}
test/stage1/behavior/error.zig+3-4
...@@ -3,7 +3,6 @@ const expect = std.testing.expect;...@@ -3,7 +3,6 @@ const expect = std.testing.expect;
3const expectError = std.testing.expectError;3const expectError = std.testing.expectError;
4const expectEqual = std.testing.expectEqual;4const expectEqual = std.testing.expectEqual;
5const mem = std.mem;5const mem = std.mem;
6const builtin = @import("builtin");
76
8pub fn foo() anyerror!i32 {7pub fn foo() anyerror!i32 {
9 const x = try bar();8 const x = try bar();
...@@ -84,8 +83,8 @@ test "error union type " {...@@ -84,8 +83,8 @@ test "error union type " {
84fn testErrorUnionType() void {83fn testErrorUnionType() void {
85 const x: anyerror!i32 = 1234;84 const x: anyerror!i32 = 1234;
86 if (x) |value| expect(value == 1234) else |_| unreachable;85 if (x) |value| expect(value == 1234) else |_| unreachable;
87 expect(@typeId(@TypeOf(x)) == builtin.TypeId.ErrorUnion);86 expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);
88 expect(@typeId(@TypeOf(x).ErrorSet) == builtin.TypeId.ErrorSet);87 expect(@typeInfo(@TypeOf(x).ErrorSet) == .ErrorSet);
89 expect(@TypeOf(x).ErrorSet == anyerror);88 expect(@TypeOf(x).ErrorSet == anyerror);
90}89}
9190
...@@ -100,7 +99,7 @@ const MyErrSet = error{...@@ -100,7 +99,7 @@ const MyErrSet = error{
100};99};
101100
102fn testErrorSetType() void {101fn testErrorSetType() void {
103 expect(@memberCount(MyErrSet) == 2);102 expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2);
104103
105 const a: MyErrSet!i32 = 5678;104 const a: MyErrSet!i32 = 5678;
106 const b: MyErrSet!i32 = MyErrSet.OutOfMemory;105 const b: MyErrSet!i32 = MyErrSet.OutOfMemory;
test/stage1/behavior/eval.zig+2-2
...@@ -654,8 +654,8 @@ test "call method with comptime pass-by-non-copying-value self parameter" {...@@ -654,8 +654,8 @@ test "call method with comptime pass-by-non-copying-value self parameter" {
654 expect(b == 2);654 expect(b == 2);
655}655}
656656
657test "@tagName of @typeId" {657test "@tagName of @typeInfo" {
658 const str = @tagName(@typeId(u8));658 const str = @tagName(@typeInfo(u8));
659 expect(std.mem.eql(u8, str, "Int"));659 expect(std.mem.eql(u8, str, "Int"));
660}660}
661661
test/stage1/behavior/misc.zig-35
...@@ -407,7 +407,6 @@ fn testArray2DConstDoublePtr(ptr: *const f32) void {...@@ -407,7 +407,6 @@ fn testArray2DConstDoublePtr(ptr: *const f32) void {
407 expect(ptr2[1] == 2.0);407 expect(ptr2[1] == 2.0);
408}408}
409409
410const Tid = builtin.TypeId;
411const AStruct = struct {410const AStruct = struct {
412 x: i32,411 x: i32,
413};412};
...@@ -424,40 +423,6 @@ const AUnion = union {...@@ -424,40 +423,6 @@ const AUnion = union {
424 Two: void,423 Two: void,
425};424};
426425
427test "@typeId" {
428 comptime {
429 expect(@typeId(type) == Tid.Type);
430 expect(@typeId(void) == Tid.Void);
431 expect(@typeId(bool) == Tid.Bool);
432 expect(@typeId(noreturn) == Tid.NoReturn);
433 expect(@typeId(i8) == Tid.Int);
434 expect(@typeId(u8) == Tid.Int);
435 expect(@typeId(i64) == Tid.Int);
436 expect(@typeId(u64) == Tid.Int);
437 expect(@typeId(f32) == Tid.Float);
438 expect(@typeId(f64) == Tid.Float);
439 expect(@typeId(*f32) == Tid.Pointer);
440 expect(@typeId([2]u8) == Tid.Array);
441 expect(@typeId(AStruct) == Tid.Struct);
442 expect(@typeId(@TypeOf(1)) == Tid.ComptimeInt);
443 expect(@typeId(@TypeOf(1.0)) == Tid.ComptimeFloat);
444 expect(@typeId(@TypeOf(undefined)) == Tid.Undefined);
445 expect(@typeId(@TypeOf(null)) == Tid.Null);
446 expect(@typeId(?i32) == Tid.Optional);
447 expect(@typeId(anyerror!i32) == Tid.ErrorUnion);
448 expect(@typeId(anyerror) == Tid.ErrorSet);
449 expect(@typeId(AnEnum) == Tid.Enum);
450 expect(@typeId(@TypeOf(AUnionEnum.One)) == Tid.Enum);
451 expect(@typeId(AUnionEnum) == Tid.Union);
452 expect(@typeId(AUnion) == Tid.Union);
453 expect(@typeId(fn () void) == Tid.Fn);
454 expect(@typeId(@TypeOf(builtin)) == Tid.Type);
455 // TODO bound fn
456 // TODO arg tuple
457 // TODO opaque
458 }
459}
460
461test "@typeName" {426test "@typeName" {
462 const Struct = struct {};427 const Struct = struct {};
463 const Union = union {428 const Union = union {
test/stage1/behavior/reflection.zig-30
...@@ -26,36 +26,6 @@ fn dummy(a: bool, b: i32, c: f32) i32 {...@@ -26,36 +26,6 @@ fn dummy(a: bool, b: i32, c: f32) i32 {
26 return 1234;26 return 1234;
27}27}
2828
29test "reflection: struct member types and names" {
30 comptime {
31 expect(@memberCount(Foo) == 3);
32
33 expect(@memberType(Foo, 0) == i32);
34 expect(@memberType(Foo, 1) == bool);
35 expect(@memberType(Foo, 2) == void);
36
37 expect(mem.eql(u8, @memberName(Foo, 0), "one"));
38 expect(mem.eql(u8, @memberName(Foo, 1), "two"));
39 expect(mem.eql(u8, @memberName(Foo, 2), "three"));
40 }
41}
42
43test "reflection: enum member types and names" {
44 comptime {
45 expect(@memberCount(Bar) == 4);
46
47 expect(@memberType(Bar, 0) == void);
48 expect(@memberType(Bar, 1) == i32);
49 expect(@memberType(Bar, 2) == bool);
50 expect(@memberType(Bar, 3) == f64);
51
52 expect(mem.eql(u8, @memberName(Bar, 0), "One"));
53 expect(mem.eql(u8, @memberName(Bar, 1), "Two"));
54 expect(mem.eql(u8, @memberName(Bar, 2), "Three"));
55 expect(mem.eql(u8, @memberName(Bar, 3), "Four"));
56 }
57}
58
59test "reflection: @field" {29test "reflection: @field" {
60 var f = Foo{30 var f = Foo{
61 .one = 42,31 .one = 42,
test/stage1/behavior/union.zig+1-1
...@@ -531,7 +531,7 @@ var glbl: Foo1 = undefined;...@@ -531,7 +531,7 @@ var glbl: Foo1 = undefined;
531531
532test "global union with single field is correctly initialized" {532test "global union with single field is correctly initialized" {
533 glbl = Foo1{533 glbl = Foo1{
534 .f = @memberType(Foo1, 0){ .x = 123 },534 .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },
535 };535 };
536 expect(glbl.f.x == 123);536 expect(glbl.f.x == 123);
537}537}
test/translate_c.zig+4-4
...@@ -1354,7 +1354,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -1354,7 +1354,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
1354 cases.add("macro pointer cast",1354 cases.add("macro pointer cast",
1355 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)1355 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
1356 , &[_][]const u8{1356 , &[_][]const u8{
1357 \\pub const NRF_GPIO = if (@typeId(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeId(@TypeOf(NRF_GPIO_BASE)) == .Int) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE);1357 \\pub const NRF_GPIO = if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Pointer) @ptrCast([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else if (@typeInfo(@TypeOf(NRF_GPIO_BASE)) == .Int) @intToPtr([*c]NRF_GPIO_Type, NRF_GPIO_BASE) else @as([*c]NRF_GPIO_Type, NRF_GPIO_BASE);
1358 });1358 });
13591359
1360 cases.add("basic macro function",1360 cases.add("basic macro function",
...@@ -2538,11 +2538,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {...@@ -2538,11 +2538,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
2538 \\#define FOO(bar) baz((void *)(baz))2538 \\#define FOO(bar) baz((void *)(baz))
2539 \\#define BAR (void*) a2539 \\#define BAR (void*) a
2540 , &[_][]const u8{2540 , &[_][]const u8{
2541 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz))) {2541 \\pub inline fn FOO(bar: var) @TypeOf(baz(if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz))) {
2542 \\ return baz(if (@typeId(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeId(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz));2542 \\ return baz(if (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz));
2543 \\}2543 \\}
2544 ,2544 ,
2545 \\pub const BAR = if (@typeId(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeId(@TypeOf(a)) == .Int) @intToPtr(*c_void, a) else @as(*c_void, a);2545 \\pub const BAR = if (@typeInfo(@TypeOf(a)) == .Pointer) @ptrCast(*c_void, a) else if (@typeInfo(@TypeOf(a)) == .Int) @intToPtr(*c_void, a) else @as(*c_void, a);
2546 });2546 });
25472547
2548 cases.add("macro conditional operator",2548 cases.add("macro conditional operator",