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 {
607607 }
608608
609609 fn typeToEnum(comptime T: type) TypeId {
610 return switch (@typeId(T)) {
611 builtin.TypeId.Int => TypeId.Int,
612 builtin.TypeId.Float => TypeId.Float,
613 builtin.TypeId.Bool => TypeId.Bool,
610 return switch (@typeInfo(T)) {
611 .Int => .Int,
612 .Float => .Float,
613 .Bool => .Bool,
614614 else => switch (T) {
615 []const u8 => TypeId.String,
616 []const []const u8 => TypeId.List,
615 []const u8 => .String,
616 []const []const u8 => .List,
617617 else => @compileError("Unsupported type: " ++ @typeName(T)),
618618 },
619619 };
......@@ -625,11 +625,11 @@ pub const Builder = struct {
625625
626626 pub fn typeIdName(id: TypeId) []const u8 {
627627 return switch (id) {
628 TypeId.Bool => "bool",
629 TypeId.Int => "int",
630 TypeId.Float => "float",
631 TypeId.String => "string",
632 TypeId.List => "list",
628 .Bool => "bool",
629 .Int => "int",
630 .Float => "float",
631 .String => "string",
632 .List => "list",
633633 };
634634 }
635635
lib/std/fmt.zig+8-7
......@@ -405,7 +405,7 @@ pub fn formatType(
405405 try format(context, Errors, output, "@{x}", .{@ptrToInt(&value)});
406406 }
407407 },
408 .Struct => {
408 .Struct => |StructT| {
409409 if (comptime std.meta.trait.hasFn("format")(T)) {
410410 return value.format(fmt, options, context, Errors, output);
411411 }
......@@ -416,27 +416,28 @@ pub fn formatType(
416416 }
417417 comptime var field_i = 0;
418418 try output(context, "{");
419 inline while (field_i < @memberCount(T)) : (field_i += 1) {
419 inline for (StructT.fields) |f| {
420420 if (field_i == 0) {
421421 try output(context, " .");
422422 } else {
423423 try output(context, ", .");
424424 }
425 try output(context, @memberName(T, field_i));
425 try output(context, f.name);
426426 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;
428429 }
429430 try output(context, " }");
430431 },
431432 .Pointer => |ptr_info| switch (ptr_info.size) {
432433 .One => switch (@typeInfo(ptr_info.child)) {
433 builtin.TypeId.Array => |info| {
434 .Array => |info| {
434435 if (info.child == u8) {
435436 return formatText(value, fmt, options, context, Errors, output);
436437 }
437438 return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) });
438439 },
439 builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => {
440 .Enum, .Union, .Struct => {
440441 return formatType(value.*, fmt, options, context, Errors, output, max_depth);
441442 },
442443 else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }),
......@@ -509,7 +510,7 @@ fn formatValue(
509510 }
510511
511512 const T = @TypeOf(value);
512 switch (@typeId(T)) {
513 switch (@typeInfo(T)) {
513514 .Float => return formatFloatValue(value, fmt, options, context, Errors, output),
514515 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output),
515516 .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,
831831
832832 //@BUG: inferred error issue. See: #1386
833833 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
836836 const u8_bit_count = 8;
837837 const t_bit_count = comptime meta.bitCount(T);
......@@ -880,9 +880,9 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
880880 /// Deserializes data into the type pointed to by `ptr`
881881 pub fn deserializeInto(self: *Self, ptr: var) !void {
882882 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)) {
886886 for (ptr) |*v|
887887 try self.deserializeInto(v);
888888 return;
......@@ -891,7 +891,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
891891 comptime assert(trait.isSingleItemPtr(T));
892892
893893 const C = comptime meta.Child(T);
894 const child_type_id = @typeId(C);
894 const child_type_id = @typeInfo(C);
895895
896896 //custom deserializer: fn(self: *Self, deserializer: var) !void
897897 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,
902902 }
903903
904904 switch (child_type_id) {
905 builtin.TypeId.Void => return,
906 builtin.TypeId.Bool => ptr.* = (try self.deserializeInt(u1)) > 0,
907 builtin.TypeId.Float, builtin.TypeId.Int => ptr.* = try self.deserializeInt(C),
908 builtin.TypeId.Struct => {
905 .Void => return,
906 .Bool => ptr.* = (try self.deserializeInt(u1)) > 0,
907 .Float, .Int => ptr.* = try self.deserializeInt(C),
908 .Struct => {
909909 const info = @typeInfo(C).Struct;
910910
911911 inline for (info.fields) |*field_info| {
......@@ -915,7 +915,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
915915 if (FieldType == void or FieldType == u0) continue;
916916
917917 //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)) {
919919 @compileError("Will not " ++ "read field " ++ name ++ " of struct " ++
920920 @typeName(C) ++ " because it " ++ "is of pointer-type " ++
921921 @typeName(FieldType) ++ ".");
......@@ -924,7 +924,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
924924 try self.deserializeInto(&@field(ptr, name));
925925 }
926926 },
927 builtin.TypeId.Union => {
927 .Union => {
928928 const info = @typeInfo(C).Union;
929929 if (info.tag_type) |TagType| {
930930 //we avoid duplicate iteration over the enum tags
......@@ -948,7 +948,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
948948 @compileError("Cannot meaningfully deserialize " ++ @typeName(C) ++
949949 " because it is an untagged union. Use a custom deserialize().");
950950 },
951 builtin.TypeId.Optional => {
951 .Optional => {
952952 const OC = comptime meta.Child(C);
953953 const exists = (try self.deserializeInt(u1)) > 0;
954954 if (!exists) {
......@@ -960,7 +960,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing,
960960 const val_ptr = &ptr.*.?;
961961 try self.deserializeInto(val_ptr);
962962 },
963 builtin.TypeId.Enum => {
963 .Enum => {
964964 var value = try self.deserializeInt(@TagType(C));
965965 ptr.* = try meta.intToEnum(C, value);
966966 },
......@@ -1009,7 +1009,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
10091009
10101010 fn serializeInt(self: *Self, value: var) Error!void {
10111011 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
10141014 const t_bit_count = comptime meta.bitCount(T);
10151015 const u8_bit_count = comptime meta.bitCount(u8);
......@@ -1058,11 +1058,11 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
10581058 return;
10591059 }
10601060
1061 switch (@typeId(T)) {
1062 builtin.TypeId.Void => return,
1063 builtin.TypeId.Bool => try self.serializeInt(@as(u1, @boolToInt(value))),
1064 builtin.TypeId.Float, builtin.TypeId.Int => try self.serializeInt(value),
1065 builtin.TypeId.Struct => {
1061 switch (@typeInfo(T)) {
1062 .Void => return,
1063 .Bool => try self.serializeInt(@as(u1, @boolToInt(value))),
1064 .Float, .Int => try self.serializeInt(value),
1065 .Struct => {
10661066 const info = @typeInfo(T);
10671067
10681068 inline for (info.Struct.fields) |*field_info| {
......@@ -1072,7 +1072,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
10721072 if (FieldType == void or FieldType == u0) continue;
10731073
10741074 //It doesn't make sense to write pointers
1075 if (comptime trait.is(builtin.TypeId.Pointer)(FieldType)) {
1075 if (comptime trait.is(.Pointer)(FieldType)) {
10761076 @compileError("Will not " ++ "serialize field " ++ name ++
10771077 " of struct " ++ @typeName(T) ++ " because it " ++
10781078 "is of pointer-type " ++ @typeName(FieldType) ++ ".");
......@@ -1080,7 +1080,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
10801080 try self.serialize(@field(value, name));
10811081 }
10821082 },
1083 builtin.TypeId.Union => {
1083 .Union => {
10841084 const info = @typeInfo(T).Union;
10851085 if (info.tag_type) |TagType| {
10861086 const active_tag = meta.activeTag(value);
......@@ -1101,7 +1101,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
11011101 @compileError("Cannot meaningfully serialize " ++ @typeName(T) ++
11021102 " because it is an untagged union. Use a custom serialize().");
11031103 },
1104 builtin.TypeId.Optional => {
1104 .Optional => {
11051105 if (value == null) {
11061106 try self.serializeInt(@as(u1, @boolToInt(false)));
11071107 return;
......@@ -1112,10 +1112,10 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co
11121112 const val_ptr = &value.?;
11131113 try self.serialize(val_ptr.*);
11141114 },
1115 builtin.TypeId.Enum => {
1115 .Enum => {
11161116 try self.serializeInt(@enumToInt(value));
11171117 },
1118 else => @compileError("Cannot serialize " ++ @tagName(@typeId(T)) ++ " types (unimplemented)."),
1118 else => @compileError("Cannot serialize " ++ @tagName(@typeInfo(T)) ++ " types (unimplemented)."),
11191119 }
11201120 }
11211121 };
lib/std/math.zig+15-17
......@@ -1,6 +1,4 @@
1const builtin = @import("builtin");
21const std = @import("std.zig");
3const TypeId = builtin.TypeId;
42const assert = std.debug.assert;
53const testing = std.testing;
64
......@@ -89,7 +87,7 @@ pub const snan = @import("math/nan.zig").snan;
8987pub const inf = @import("math/inf.zig").inf;
9088
9189pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool {
92 assert(@typeId(T) == TypeId.Float);
90 assert(@typeInfo(T) == .Float);
9391 return fabs(x - y) < epsilon;
9492}
9593
......@@ -198,7 +196,7 @@ test "" {
198196}
199197
200198pub fn floatMantissaBits(comptime T: type) comptime_int {
201 assert(@typeId(T) == builtin.TypeId.Float);
199 assert(@typeInfo(T) == .Float);
202200
203201 return switch (T.bit_count) {
204202 16 => 10,
......@@ -211,7 +209,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int {
211209}
212210
213211pub fn floatExponentBits(comptime T: type) comptime_int {
214 assert(@typeId(T) == builtin.TypeId.Float);
212 assert(@typeInfo(T) == .Float);
215213
216214 return switch (T.bit_count) {
217215 16 => 5,
......@@ -526,7 +524,7 @@ fn testOverflow() void {
526524
527525pub fn absInt(x: var) !@TypeOf(x) {
528526 const T = @TypeOf(x);
529 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
527 comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt
530528 comptime assert(T.is_signed); // must pass a signed integer to absInt
531529
532530 if (x == minInt(@TypeOf(x))) {
......@@ -560,7 +558,7 @@ fn testAbsFloat() void {
560558pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T {
561559 @setRuntimeSafety(false);
562560 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;
564562 return @divTrunc(numerator, denominator);
565563}
566564
......@@ -581,7 +579,7 @@ fn testDivTrunc() void {
581579pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T {
582580 @setRuntimeSafety(false);
583581 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;
585583 return @divFloor(numerator, denominator);
586584}
587585
......@@ -602,7 +600,7 @@ fn testDivFloor() void {
602600pub fn divExact(comptime T: type, numerator: T, denominator: T) !T {
603601 @setRuntimeSafety(false);
604602 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;
606604 const result = @divTrunc(numerator, denominator);
607605 if (result * denominator != numerator) return error.UnexpectedRemainder;
608606 return result;
......@@ -727,8 +725,8 @@ test "math.negateCast" {
727725/// Cast an integer to a different integer type. If the value doesn't fit,
728726/// return an error.
729727pub fn cast(comptime T: type, x: var) (error{Overflow}!T) {
730 comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer
731 comptime assert(@typeId(@TypeOf(x)) == builtin.TypeId.Int); // must pass an integer
728 comptime assert(@typeInfo(T) == .Int); // must pass an integer
729 comptime assert(@typeInfo(@TypeOf(x)) == .Int); // must pass an integer
732730 if (maxInt(@TypeOf(x)) > maxInt(T) and x > maxInt(T)) {
733731 return error.Overflow;
734732 } else if (minInt(@TypeOf(x)) < minInt(T) and x < minInt(T)) {
......@@ -793,7 +791,7 @@ fn testFloorPowerOfTwo() void {
793791/// Only unsigned integers can be used. Zero is not an allowed input.
794792/// Result is a type with 1 more bit than the input type.
795793pub 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);
797795 comptime assert(!T.is_signed);
798796 assert(value != 0);
799797 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
805803/// Only unsigned integers can be used. Zero is not an allowed input.
806804/// If the value doesn't fit, returns an error.
807805pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) {
808 comptime assert(@typeId(T) == builtin.TypeId.Int);
806 comptime assert(@typeInfo(T) == .Int);
809807 comptime assert(!T.is_signed);
810808 comptime const PromotedType = @IntType(T.is_signed, T.bit_count + 1);
811809 comptime const overflowBit = @as(PromotedType, 1) << T.bit_count;
......@@ -878,10 +876,10 @@ test "std.math.log2_int_ceil" {
878876
879877pub fn lossyCast(comptime T: type, value: var) T {
880878 switch (@typeInfo(@TypeOf(value))) {
881 builtin.TypeId.Int => return @intToFloat(T, value),
882 builtin.TypeId.Float => return @floatCast(T, value),
883 builtin.TypeId.ComptimeInt => return @as(T, value),
884 builtin.TypeId.ComptimeFloat => return @as(T, value),
879 .Int => return @intToFloat(T, value),
880 .Float => return @floatCast(T, value),
881 .ComptimeInt => return @as(T, value),
882 .ComptimeFloat => return @as(T, value),
885883 else => @compileError("bad type"),
886884 }
887885}
lib/std/math/big/int.zig+4-7
......@@ -1,5 +1,4 @@
11const std = @import("../../std.zig");
2const builtin = @import("builtin");
32const debug = std.debug;
43const testing = std.testing;
54const math = std.math;
......@@ -9,8 +8,6 @@ const ArrayList = std.ArrayList;
98const maxInt = std.math.maxInt;
109const minInt = std.math.minInt;
1110
12const TypeId = builtin.TypeId;
13
1411pub const Limb = usize;
1512pub const DoubleLimb = @IntType(false, 2 * Limb.bit_count);
1613pub const Log2Limb = math.Log2Int(Limb);
......@@ -270,7 +267,7 @@ pub const Int = struct {
270267 const T = @TypeOf(value);
271268
272269 switch (@typeInfo(T)) {
273 TypeId.Int => |info| {
270 .Int => |info| {
274271 const UT = if (T.is_signed) @IntType(false, T.bit_count - 1) else T;
275272
276273 try self.ensureCapacity(@sizeOf(UT) / @sizeOf(Limb));
......@@ -294,7 +291,7 @@ pub const Int = struct {
294291 }
295292 }
296293 },
297 TypeId.ComptimeInt => {
294 .ComptimeInt => {
298295 comptime var w_value = if (value < 0) -value else value;
299296
300297 const req_limbs = @divFloor(math.log2(w_value), Limb.bit_count) + 1;
......@@ -332,8 +329,8 @@ pub const Int = struct {
332329 ///
333330 /// Returns an error if self cannot be narrowed into the requested type without truncation.
334331 pub fn to(self: Int, comptime T: type) ConvertError!T {
335 switch (@typeId(T)) {
336 TypeId.Int => {
332 switch (@typeInfo(T)) {
333 .Int => {
337334 const UT = @IntType(false, T.bit_count);
338335
339336 if (self.bitCountTwosComp() > T.bit_count) {
lib/std/math/big/rational.zig+3-6
......@@ -1,5 +1,4 @@
11const std = @import("../../std.zig");
2const builtin = @import("builtin");
32const debug = std.debug;
43const math = std.math;
54const mem = std.mem;
......@@ -7,8 +6,6 @@ const testing = std.testing;
76const Allocator = mem.Allocator;
87const ArrayList = std.ArrayList;
98
10const TypeId = builtin.TypeId;
11
129const bn = @import("int.zig");
1310const Limb = bn.Limb;
1411const DoubleLimb = bn.DoubleLimb;
......@@ -129,7 +126,7 @@ pub const Rational = struct {
129126 /// completely represent the provided float.
130127 pub fn setFloat(self: *Rational, comptime T: type, f: T) !void {
131128 // Translated from golang.go/src/math/big/rat.go.
132 debug.assert(@typeId(T) == builtin.TypeId.Float);
129 debug.assert(@typeInfo(T) == .Float);
133130
134131 const UnsignedIntType = @IntType(false, T.bit_count);
135132 const f_bits = @bitCast(UnsignedIntType, f);
......@@ -187,7 +184,7 @@ pub const Rational = struct {
187184 pub fn toFloat(self: Rational, comptime T: type) !T {
188185 // Translated from golang.go/src/math/big/rat.go.
189186 // TODO: Indicate whether the result is not exact.
190 debug.assert(@typeId(T) == builtin.TypeId.Float);
187 debug.assert(@typeInfo(T) == .Float);
191188
192189 const fsize = T.bit_count;
193190 const BitReprType = @IntType(false, T.bit_count);
......@@ -653,7 +650,7 @@ test "big.rational gcd one large" {
653650}
654651
655652fn extractLowBits(a: Int, comptime T: type) T {
656 testing.expect(@typeId(T) == builtin.TypeId.Int);
653 testing.expect(@typeInfo(T) == .Int);
657654
658655 if (T.bit_count <= Limb.bit_count) {
659656 return @truncate(T, a.limbs[0]);
lib/std/math/ln.zig+5-7
......@@ -7,8 +7,6 @@
77const std = @import("../std.zig");
88const math = std.math;
99const expect = std.testing.expect;
10const builtin = @import("builtin");
11const TypeId = builtin.TypeId;
1210
1311/// Returns the natural logarithm of x.
1412///
......@@ -19,21 +17,21 @@ const TypeId = builtin.TypeId;
1917/// - ln(nan) = nan
2018pub fn ln(x: var) @TypeOf(x) {
2119 const T = @TypeOf(x);
22 switch (@typeId(T)) {
23 TypeId.ComptimeFloat => {
20 switch (@typeInfo(T)) {
21 .ComptimeFloat => {
2422 return @as(comptime_float, ln_64(x));
2523 },
26 TypeId.Float => {
24 .Float => {
2725 return switch (T) {
2826 f32 => ln_32(x),
2927 f64 => ln_64(x),
3028 else => @compileError("ln not implemented for " ++ @typeName(T)),
3129 };
3230 },
33 TypeId.ComptimeInt => {
31 .ComptimeInt => {
3432 return @as(comptime_int, math.floor(ln_64(@as(f64, x))));
3533 },
36 TypeId.Int => {
34 .Int => {
3735 return @as(T, math.floor(ln_64(@as(f64, x))));
3836 },
3937 else => @compileError("ln not implemented for " ++ @typeName(T)),
lib/std/math/log.zig+6-8
......@@ -6,8 +6,6 @@
66
77const std = @import("../std.zig");
88const math = std.math;
9const builtin = @import("builtin");
10const TypeId = builtin.TypeId;
119const expect = std.testing.expect;
1210
1311/// Returns the logarithm of x for the provided base.
......@@ -16,24 +14,24 @@ pub fn log(comptime T: type, base: T, x: T) T {
1614 return math.log2(x);
1715 } else if (base == 10) {
1816 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) {
2018 return math.ln(x);
2119 }
2220
2321 const float_base = math.lossyCast(f64, base);
24 switch (@typeId(T)) {
25 TypeId.ComptimeFloat => {
22 switch (@typeInfo(T)) {
23 .ComptimeFloat => {
2624 return @as(comptime_float, math.ln(@as(f64, x)) / math.ln(float_base));
2725 },
28 TypeId.ComptimeInt => {
26 .ComptimeInt => {
2927 return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base)));
3028 },
31 builtin.TypeId.Int => {
29 .Int => {
3230 // TODO implement integer log without using float math
3331 return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base)));
3432 },
3533
36 builtin.TypeId.Float => {
34 .Float => {
3735 switch (T) {
3836 f32 => return @floatCast(f32, math.ln(@as(f64, x)) / math.ln(float_base)),
3937 f64 => return math.ln(x) / math.ln(float_base),
lib/std/math/log10.zig+5-7
......@@ -7,8 +7,6 @@
77const std = @import("../std.zig");
88const math = std.math;
99const testing = std.testing;
10const builtin = @import("builtin");
11const TypeId = builtin.TypeId;
1210const maxInt = std.math.maxInt;
1311
1412/// Returns the base-10 logarithm of x.
......@@ -20,21 +18,21 @@ const maxInt = std.math.maxInt;
2018/// - log10(nan) = nan
2119pub fn log10(x: var) @TypeOf(x) {
2220 const T = @TypeOf(x);
23 switch (@typeId(T)) {
24 TypeId.ComptimeFloat => {
21 switch (@typeInfo(T)) {
22 .ComptimeFloat => {
2523 return @as(comptime_float, log10_64(x));
2624 },
27 TypeId.Float => {
25 .Float => {
2826 return switch (T) {
2927 f32 => log10_32(x),
3028 f64 => log10_64(x),
3129 else => @compileError("log10 not implemented for " ++ @typeName(T)),
3230 };
3331 },
34 TypeId.ComptimeInt => {
32 .ComptimeInt => {
3533 return @as(comptime_int, math.floor(log10_64(@as(f64, x))));
3634 },
37 TypeId.Int => {
35 .Int => {
3836 return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x))));
3937 },
4038 else => @compileError("log10 not implemented for " ++ @typeName(T)),
lib/std/math/log2.zig+5-7
......@@ -7,8 +7,6 @@
77const std = @import("../std.zig");
88const math = std.math;
99const expect = std.testing.expect;
10const builtin = @import("builtin");
11const TypeId = builtin.TypeId;
1210const maxInt = std.math.maxInt;
1311
1412/// Returns the base-2 logarithm of x.
......@@ -20,18 +18,18 @@ const maxInt = std.math.maxInt;
2018/// - log2(nan) = nan
2119pub fn log2(x: var) @TypeOf(x) {
2220 const T = @TypeOf(x);
23 switch (@typeId(T)) {
24 TypeId.ComptimeFloat => {
21 switch (@typeInfo(T)) {
22 .ComptimeFloat => {
2523 return @as(comptime_float, log2_64(x));
2624 },
27 TypeId.Float => {
25 .Float => {
2826 return switch (T) {
2927 f32 => log2_32(x),
3028 f64 => log2_64(x),
3129 else => @compileError("log2 not implemented for " ++ @typeName(T)),
3230 };
3331 },
34 TypeId.ComptimeInt => comptime {
32 .ComptimeInt => comptime {
3533 var result = 0;
3634 var x_shifted = x;
3735 while (b: {
......@@ -40,7 +38,7 @@ pub fn log2(x: var) @TypeOf(x) {
4038 }) : (result += 1) {}
4139 return result;
4240 },
43 TypeId.Int => {
41 .Int => {
4442 return math.log2_int(T, x);
4543 },
4644 else => @compileError("log2 not implemented for " ++ @typeName(T)),
lib/std/meta.zig+2-3
......@@ -536,9 +536,8 @@ test "intToEnum with error return" {
536536pub const IntToEnumError = error{InvalidEnumTag};
537537
538538pub fn intToEnum(comptime Tag: type, tag_int: var) IntToEnumError!Tag {
539 comptime var i = 0;
540 inline while (i != @memberCount(Tag)) : (i += 1) {
541 const this_tag_value = @field(Tag, @memberName(Tag, i));
539 inline for (@typeInfo(Tag).Enum.fields) |f| {
540 const this_tag_value = @field(Tag, f.name);
542541 if (tag_int == @enumToInt(this_tag_value)) {
543542 return this_tag_value;
544543 }
lib/std/meta/trait.zig+7-7
......@@ -1,5 +1,5 @@
11const std = @import("../std.zig");
2const builtin = @import("builtin");
2const builtin = std.builtin;
33const mem = std.mem;
44const debug = std.debug;
55const testing = std.testing;
......@@ -54,7 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn {
5454 if (!comptime isContainer(T)) return false;
5555 if (!comptime @hasDecl(T, name)) return false;
5656 const DeclType = @TypeOf(@field(T, name));
57 return @typeId(DeclType) == .Fn;
57 return @typeInfo(DeclType) == .Fn;
5858 }
5959 };
6060 return Closure.trait;
......@@ -105,7 +105,7 @@ test "std.meta.trait.hasField" {
105105pub fn is(comptime id: builtin.TypeId) TraitFn {
106106 const Closure = struct {
107107 pub fn trait(comptime T: type) bool {
108 return id == @typeId(T);
108 return id == @typeInfo(T);
109109 }
110110 };
111111 return Closure.trait;
......@@ -123,7 +123,7 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn {
123123 const Closure = struct {
124124 pub fn trait(comptime T: type) bool {
125125 if (!comptime isSingleItemPtr(T)) return false;
126 return id == @typeId(meta.Child(T));
126 return id == @typeInfo(meta.Child(T));
127127 }
128128 };
129129 return Closure.trait;
......@@ -139,7 +139,7 @@ pub fn isSliceOf(comptime id: builtin.TypeId) TraitFn {
139139 const Closure = struct {
140140 pub fn trait(comptime T: type) bool {
141141 if (!comptime isSlice(T)) return false;
142 return id == @typeId(meta.Child(T));
142 return id == @typeInfo(meta.Child(T));
143143 }
144144 };
145145 return Closure.trait;
......@@ -285,7 +285,7 @@ test "std.meta.trait.isIndexable" {
285285}
286286
287287pub fn isNumber(comptime T: type) bool {
288 return switch (@typeId(T)) {
288 return switch (@typeInfo(T)) {
289289 .Int, .Float, .ComptimeInt, .ComptimeFloat => true,
290290 else => false,
291291 };
......@@ -320,7 +320,7 @@ test "std.meta.trait.isConstPtr" {
320320}
321321
322322pub fn isContainer(comptime T: type) bool {
323 return switch (@typeId(T)) {
323 return switch (@typeInfo(T)) {
324324 .Struct, .Union, .Enum => true,
325325 else => false,
326326 };
lib/std/special/build_runner.zig+1-1
......@@ -126,7 +126,7 @@ pub fn main() !void {
126126}
127127
128128fn runBuild(builder: *Builder) anyerror!void {
129 switch (@typeId(@TypeOf(root.build).ReturnType)) {
129 switch (@typeInfo(@TypeOf(root.build).ReturnType)) {
130130 .Void => root.build(builder),
131131 .ErrorUnion => try root.build(builder),
132132 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 {
158158 };
159159 fn threadMain(raw_arg: windows.LPVOID) callconv(.C) windows.DWORD {
160160 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)) {
162162 .Int => {
163163 return startFn(arg);
164164 },
......@@ -201,7 +201,7 @@ pub const Thread = struct {
201201 fn linuxThreadMain(ctx_addr: usize) callconv(.C) u8 {
202202 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)) {
205205 .Int => {
206206 return startFn(arg);
207207 },
lib/std/zig/ast.zig+12-16
......@@ -457,10 +457,9 @@ pub const Node = struct {
457457 }
458458
459459 pub fn iterate(base: *Node, index: usize) ?*Node {
460 comptime var i = 0;
461 inline while (i < @memberCount(Id)) : (i += 1) {
462 if (base.id == @field(Id, @memberName(Id, i))) {
463 const T = @field(Node, @memberName(Id, i));
460 inline for (@typeInfo(Id).Enum.fields) |f| {
461 if (base.id == @field(Id, f.name)) {
462 const T = @field(Node, f.name);
464463 return @fieldParentPtr(T, "base", base).iterate(index);
465464 }
466465 }
......@@ -468,10 +467,9 @@ pub const Node = struct {
468467 }
469468
470469 pub fn firstToken(base: *const Node) TokenIndex {
471 comptime var i = 0;
472 inline while (i < @memberCount(Id)) : (i += 1) {
473 if (base.id == @field(Id, @memberName(Id, i))) {
474 const T = @field(Node, @memberName(Id, i));
470 inline for (@typeInfo(Id).Enum.fields) |f| {
471 if (base.id == @field(Id, f.name)) {
472 const T = @field(Node, f.name);
475473 return @fieldParentPtr(T, "base", base).firstToken();
476474 }
477475 }
......@@ -479,10 +477,9 @@ pub const Node = struct {
479477 }
480478
481479 pub fn lastToken(base: *const Node) TokenIndex {
482 comptime var i = 0;
483 inline while (i < @memberCount(Id)) : (i += 1) {
484 if (base.id == @field(Id, @memberName(Id, i))) {
485 const T = @field(Node, @memberName(Id, i));
480 inline for (@typeInfo(Id).Enum.fields) |f| {
481 if (base.id == @field(Id, f.name)) {
482 const T = @field(Node, f.name);
486483 return @fieldParentPtr(T, "base", base).lastToken();
487484 }
488485 }
......@@ -490,10 +487,9 @@ pub const Node = struct {
490487 }
491488
492489 pub fn typeToId(comptime T: type) Id {
493 comptime var i = 0;
494 inline while (i < @memberCount(Id)) : (i += 1) {
495 if (T == @field(Node, @memberName(Id, i))) {
496 return @field(Id, @memberName(Id, i));
490 inline for (@typeInfo(Id).Enum.fields) |f| {
491 if (T == @field(Node, f.name)) {
492 return @field(Id, f.name);
497493 }
498494 }
499495 unreachable;
lib/std/zig/parser_test.zig+1-1
......@@ -1410,7 +1410,7 @@ test "zig fmt: same-line comment after non-block if expression" {
14101410test "zig fmt: same-line comment on comptime expression" {
14111411 try testCanonical(
14121412 \\test "" {
1413 \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt
1413 \\ comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt
14141414 \\}
14151415 \\
14161416 );
lib/std/zig/render.zig+1-2
......@@ -1,5 +1,4 @@
11const std = @import("../std.zig");
2const builtin = @import("builtin");
32const assert = std.debug.assert;
43const mem = std.mem;
54const ast = std.zig.ast;
......@@ -14,7 +13,7 @@ pub const Error = error{
1413
1514/// Returns whether anything changed
1615pub 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
1918 var anything_changed: bool = false;
2019
src-self-hosted/ir.zig+17-22
......@@ -76,20 +76,18 @@ pub const Inst = struct {
7676 }
7777
7878 pub fn typeToId(comptime T: type) Id {
79 comptime var i = 0;
80 inline while (i < @memberCount(Id)) : (i += 1) {
81 if (T == @field(Inst, @memberName(Id, i))) {
82 return @field(Id, @memberName(Id, i));
79 inline for (@typeInfo(Id).Enum.fields) |f| {
80 if (T == @field(Inst, f.name)) {
81 return @field(Id, f.name);
8382 }
8483 }
8584 unreachable;
8685 }
8786
8887 pub fn dump(base: *const Inst) void {
89 comptime var i = 0;
90 inline while (i < @memberCount(Id)) : (i += 1) {
91 if (base.id == @field(Id, @memberName(Id, i))) {
92 const T = @field(Inst, @memberName(Id, i));
88 inline for (@typeInfo(Id).Enum.fields) |f| {
89 if (base.id == @field(Id, f.name)) {
90 const T = @field(Inst, f.name);
9391 std.debug.warn("#{} = {}(", .{ base.debug_id, @tagName(base.id) });
9492 @fieldParentPtr(T, "base", base).dump();
9593 std.debug.warn(")", .{});
......@@ -100,10 +98,9 @@ pub const Inst = struct {
10098 }
10199
102100 pub fn hasSideEffects(base: *const Inst) bool {
103 comptime var i = 0;
104 inline while (i < @memberCount(Id)) : (i += 1) {
105 if (base.id == @field(Id, @memberName(Id, i))) {
106 const T = @field(Inst, @memberName(Id, i));
101 inline for (@typeInfo(Id).Enum.fields) |f| {
102 if (base.id == @field(Id, f.name)) {
103 const T = @field(Inst, f.name);
107104 return @fieldParentPtr(T, "base", base).hasSideEffects();
108105 }
109106 }
......@@ -1805,21 +1802,19 @@ pub const Builder = struct {
18051802 };
18061803
18071804 // Look at the params and ref() other instructions
1808 comptime var i = 0;
1809 inline while (i < @memberCount(I.Params)) : (i += 1) {
1810 const FieldType = comptime @TypeOf(@field(@as(I.Params, undefined), @memberName(I.Params, i)));
1811 switch (FieldType) {
1812 *Inst => @field(inst.params, @memberName(I.Params, i)).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),
1805 inline for (@typeInfo(I.Params).Struct.fields) |f| {
1806 switch (f.fiedl_type) {
1807 *Inst => @field(inst.params, f.name).ref(self),
1808 *BasicBlock => @field(inst.params, f.name).ref(self),
1809 ?*Inst => if (@field(inst.params, f.name)) |other| other.ref(self),
18151810 []*Inst => {
18161811 // 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|
18181813 other.ref(self);
18191814 },
18201815 []*BasicBlock => {
18211816 // 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|
18231818 other.ref(self);
18241819 },
18251820 Type.Pointer.Mut,
......@@ -1831,7 +1826,7 @@ pub const Builder = struct {
18311826 => {},
18321827 // it's ok to add more types here, just make sure that
18331828 // 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)),
18351830 }
18361831 }
18371832
src-self-hosted/translate_c.zig+4-4
......@@ -5381,15 +5381,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
53815381 return error.ParseError;
53825382 }
53835383
5384 //if (@typeId(@TypeOf(x)) == .Pointer)
5384 //if (@typeInfo(@TypeOf(x)) == .Pointer)
53855385 // @ptrCast(dest, x)
5386 //else if (@typeId(@TypeOf(x)) == .Integer)
5386 //else if (@typeInfo(@TypeOf(x)) == .Integer)
53875387 // @intToPtr(dest, x)
53885388 //else
53895389 // @as(dest, x)
53905390
53915391 const if_1 = try transCreateNodeIf(c);
5392 const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeId");
5392 const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
53935393 const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
53945394 try type_id_1.params.push(&type_of_1.base);
53955395 try type_of_1.params.push(node_to_cast);
......@@ -5416,7 +5416,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8,
54165416 if_1.@"else" = else_1;
54175417
54185418 const if_2 = try transCreateNodeIf(c);
5419 const type_id_2 = try transCreateNodeBuiltinFnCall(c, "@typeId");
5419 const type_id_2 = try transCreateNodeBuiltinFnCall(c, "@typeInfo");
54205420 const type_of_2 = try transCreateNodeBuiltinFnCall(c, "@TypeOf");
54215421 try type_id_2.params.push(&type_of_2.base);
54225422 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 {
29422942 "tmp.zig:11:13: error: error.B not a member of error set 'Set2'",
29432943 });
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
29532945 cases.add("duplicate error value in error set",
29542946 \\const Foo = error {
29552947 \\ Bar,
......@@ -5964,76 +5956,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
59645956 "tmp.zig:2:32: error: arg index 2 out of bounds; 'fn(i32, i32) i32' has 2 arguments",
59655957 });
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
60375959 cases.add("calling var args extern function, passing array instead of pointer",
60385960 \\export fn entry() void {
60395961 \\ foo("hello".*,);
test/stage1/behavior/bugs/3742.zig+1-1
......@@ -17,7 +17,7 @@ pub const GET = struct {
1717};
1818
1919pub fn isCommand(comptime T: type) bool {
20 const tid = @typeId(T);
20 const tid = @typeInfo(T);
2121 return (tid == .Struct or tid == .Enum or tid == .Union) and
2222 @hasDecl(T, "Redis") and @hasDecl(T.Redis, "Command");
2323}
test/stage1/behavior/enum.zig+2-2
......@@ -96,8 +96,8 @@ test "enum type" {
9696 const bar = Bar.B;
9797
9898 expect(bar == Bar.B);
99 expect(@memberCount(Foo) == 3);
100 expect(@memberCount(Bar) == 4);
99 expect(@typeInfo(Foo).Union.fields.len == 3);
100 expect(@typeInfo(Bar).Enum.fields.len == 4);
101101 expect(@sizeOf(Foo) == @sizeOf(FooNoVoid));
102102 expect(@sizeOf(Bar) == 1);
103103}
test/stage1/behavior/error.zig+3-4
......@@ -3,7 +3,6 @@ const expect = std.testing.expect;
33const expectError = std.testing.expectError;
44const expectEqual = std.testing.expectEqual;
55const mem = std.mem;
6const builtin = @import("builtin");
76
87pub fn foo() anyerror!i32 {
98 const x = try bar();
......@@ -84,8 +83,8 @@ test "error union type " {
8483fn testErrorUnionType() void {
8584 const x: anyerror!i32 = 1234;
8685 if (x) |value| expect(value == 1234) else |_| unreachable;
87 expect(@typeId(@TypeOf(x)) == builtin.TypeId.ErrorUnion);
88 expect(@typeId(@TypeOf(x).ErrorSet) == builtin.TypeId.ErrorSet);
86 expect(@typeInfo(@TypeOf(x)) == .ErrorUnion);
87 expect(@typeInfo(@TypeOf(x).ErrorSet) == .ErrorSet);
8988 expect(@TypeOf(x).ErrorSet == anyerror);
9089}
9190
......@@ -100,7 +99,7 @@ const MyErrSet = error{
10099};
101100
102101fn testErrorSetType() void {
103 expect(@memberCount(MyErrSet) == 2);
102 expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2);
104103
105104 const a: MyErrSet!i32 = 5678;
106105 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" {
654654 expect(b == 2);
655655}
656656
657test "@tagName of @typeId" {
658 const str = @tagName(@typeId(u8));
657test "@tagName of @typeInfo" {
658 const str = @tagName(@typeInfo(u8));
659659 expect(std.mem.eql(u8, str, "Int"));
660660}
661661
test/stage1/behavior/misc.zig-35
......@@ -407,7 +407,6 @@ fn testArray2DConstDoublePtr(ptr: *const f32) void {
407407 expect(ptr2[1] == 2.0);
408408}
409409
410const Tid = builtin.TypeId;
411410const AStruct = struct {
412411 x: i32,
413412};
......@@ -424,40 +423,6 @@ const AUnion = union {
424423 Two: void,
425424};
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
461426test "@typeName" {
462427 const Struct = struct {};
463428 const Union = union {
test/stage1/behavior/reflection.zig-30
......@@ -26,36 +26,6 @@ fn dummy(a: bool, b: i32, c: f32) i32 {
2626 return 1234;
2727}
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
5929test "reflection: @field" {
6030 var f = Foo{
6131 .one = 42,
test/stage1/behavior/union.zig+1-1
......@@ -531,7 +531,7 @@ var glbl: Foo1 = undefined;
531531
532532test "global union with single field is correctly initialized" {
533533 glbl = Foo1{
534 .f = @memberType(Foo1, 0){ .x = 123 },
534 .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 },
535535 };
536536 expect(glbl.f.x == 123);
537537}
test/translate_c.zig+4-4
......@@ -1354,7 +1354,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
13541354 cases.add("macro pointer cast",
13551355 \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE)
13561356 , &[_][]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);
13581358 });
13591359
13601360 cases.add("basic macro function",
......@@ -2538,11 +2538,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
25382538 \\#define FOO(bar) baz((void *)(baz))
25392539 \\#define BAR (void*) a
25402540 , &[_][]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))) {
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));
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 (@typeInfo(@TypeOf(baz)) == .Pointer) @ptrCast(*c_void, baz) else if (@typeInfo(@TypeOf(baz)) == .Int) @intToPtr(*c_void, baz) else @as(*c_void, baz));
25432543 \\}
25442544 ,
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);
25462546 });
25472547
25482548 cases.add("macro conditional operator",