From 45da72c5b64069b7d5238465130a50f96678a148 Mon Sep 17 00:00:00 2001 From: Vexu Date: Mon, 24 Feb 2020 23:03:30 +0200 Subject: [PATCH 1/4] remove usages of `@typeId`, `@memberCount`, `@memberName` and `@memberType` --- lib/std/build.zig | 22 ++++---- lib/std/fmt.zig | 15 +++--- lib/std/io.zig | 46 ++++++++--------- lib/std/math.zig | 32 ++++++------ lib/std/math/big/int.zig | 11 ++-- lib/std/math/big/rational.zig | 9 ++-- lib/std/math/ln.zig | 12 ++--- lib/std/math/log.zig | 14 +++--- lib/std/math/log10.zig | 12 ++--- lib/std/math/log2.zig | 12 ++--- lib/std/meta.zig | 5 +- lib/std/meta/trait.zig | 14 +++--- lib/std/special/build_runner.zig | 2 +- lib/std/thread.zig | 4 +- lib/std/zig/ast.zig | 28 +++++------ lib/std/zig/parser_test.zig | 2 +- lib/std/zig/render.zig | 3 +- src-self-hosted/ir.zig | 39 +++++++-------- src-self-hosted/translate_c.zig | 8 +-- test/compile_errors.zig | 78 ----------------------------- test/stage1/behavior/bugs/3742.zig | 2 +- test/stage1/behavior/enum.zig | 4 +- test/stage1/behavior/error.zig | 7 ++- test/stage1/behavior/eval.zig | 4 +- test/stage1/behavior/misc.zig | 35 ------------- test/stage1/behavior/reflection.zig | 30 ----------- test/stage1/behavior/union.zig | 2 +- test/translate_c.zig | 8 +-- 28 files changed, 145 insertions(+), 315 deletions(-) diff --git a/lib/std/build.zig b/lib/std/build.zig index 6e9031ee31..143d7ce6c0 100644 --- a/lib/std/build.zig +++ b/lib/std/build.zig @@ -607,13 +607,13 @@ pub const Builder = struct { } fn typeToEnum(comptime T: type) TypeId { - return switch (@typeId(T)) { - builtin.TypeId.Int => TypeId.Int, - builtin.TypeId.Float => TypeId.Float, - builtin.TypeId.Bool => TypeId.Bool, + return switch (@typeInfo(T)) { + .Int => .Int, + .Float => .Float, + .Bool => .Bool, else => switch (T) { - []const u8 => TypeId.String, - []const []const u8 => TypeId.List, + []const u8 => .String, + []const []const u8 => .List, else => @compileError("Unsupported type: " ++ @typeName(T)), }, }; @@ -625,11 +625,11 @@ pub const Builder = struct { pub fn typeIdName(id: TypeId) []const u8 { return switch (id) { - TypeId.Bool => "bool", - TypeId.Int => "int", - TypeId.Float => "float", - TypeId.String => "string", - TypeId.List => "list", + .Bool => "bool", + .Int => "int", + .Float => "float", + .String => "string", + .List => "list", }; } diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 0fe096a015..5c73ed1252 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -405,7 +405,7 @@ pub fn formatType( try format(context, Errors, output, "@{x}", .{@ptrToInt(&value)}); } }, - .Struct => { + .Struct => |StructT| { if (comptime std.meta.trait.hasFn("format")(T)) { return value.format(fmt, options, context, Errors, output); } @@ -416,27 +416,28 @@ pub fn formatType( } comptime var field_i = 0; try output(context, "{"); - inline while (field_i < @memberCount(T)) : (field_i += 1) { + inline for (StructT.fields) |f| { if (field_i == 0) { try output(context, " ."); } else { try output(context, ", ."); } - try output(context, @memberName(T, field_i)); + try output(context, f.name); try output(context, " = "); - try formatType(@field(value, @memberName(T, field_i)), fmt, options, context, Errors, output, max_depth - 1); + try formatType(@field(value, f.name), fmt, options, context, Errors, output, max_depth - 1); + field_i += 1; } try output(context, " }"); }, .Pointer => |ptr_info| switch (ptr_info.size) { .One => switch (@typeInfo(ptr_info.child)) { - builtin.TypeId.Array => |info| { + .Array => |info| { if (info.child == u8) { return formatText(value, fmt, options, context, Errors, output); } return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }); }, - builtin.TypeId.Enum, builtin.TypeId.Union, builtin.TypeId.Struct => { + .Enum, .Union, .Struct => { return formatType(value.*, fmt, options, context, Errors, output, max_depth); }, else => return format(context, Errors, output, "{}@{x}", .{ @typeName(T.Child), @ptrToInt(value) }), @@ -509,7 +510,7 @@ fn formatValue( } const T = @TypeOf(value); - switch (@typeId(T)) { + switch (@typeInfo(T)) { .Float => return formatFloatValue(value, fmt, options, context, Errors, output), .Int, .ComptimeInt => return formatIntValue(value, fmt, options, context, Errors, output), .Bool => return output(context, if (value) "true" else "false"), diff --git a/lib/std/io.zig b/lib/std/io.zig index b6e1b12534..943db1a155 100644 --- a/lib/std/io.zig +++ b/lib/std/io.zig @@ -831,7 +831,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, //@BUG: inferred error issue. See: #1386 fn deserializeInt(self: *Self, comptime T: type) (Error || error{EndOfStream})!T { - comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T)); + comptime assert(trait.is(.Int)(T) or trait.is(.Float)(T)); const u8_bit_count = 8; const t_bit_count = comptime meta.bitCount(T); @@ -880,9 +880,9 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, /// Deserializes data into the type pointed to by `ptr` pub fn deserializeInto(self: *Self, ptr: var) !void { const T = @TypeOf(ptr); - comptime assert(trait.is(builtin.TypeId.Pointer)(T)); + comptime assert(trait.is(.Pointer)(T)); - if (comptime trait.isSlice(T) or comptime trait.isPtrTo(builtin.TypeId.Array)(T)) { + if (comptime trait.isSlice(T) or comptime trait.isPtrTo(.Array)(T)) { for (ptr) |*v| try self.deserializeInto(v); return; @@ -891,7 +891,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, comptime assert(trait.isSingleItemPtr(T)); const C = comptime meta.Child(T); - const child_type_id = @typeId(C); + const child_type_id = @typeInfo(C); //custom deserializer: fn(self: *Self, deserializer: var) !void 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, } switch (child_type_id) { - builtin.TypeId.Void => return, - builtin.TypeId.Bool => ptr.* = (try self.deserializeInt(u1)) > 0, - builtin.TypeId.Float, builtin.TypeId.Int => ptr.* = try self.deserializeInt(C), - builtin.TypeId.Struct => { + .Void => return, + .Bool => ptr.* = (try self.deserializeInt(u1)) > 0, + .Float, .Int => ptr.* = try self.deserializeInt(C), + .Struct => { const info = @typeInfo(C).Struct; inline for (info.fields) |*field_info| { @@ -915,7 +915,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, if (FieldType == void or FieldType == u0) continue; //it doesn't make any sense to read pointers - if (comptime trait.is(builtin.TypeId.Pointer)(FieldType)) { + if (comptime trait.is(.Pointer)(FieldType)) { @compileError("Will not " ++ "read field " ++ name ++ " of struct " ++ @typeName(C) ++ " because it " ++ "is of pointer-type " ++ @typeName(FieldType) ++ "."); @@ -924,7 +924,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, try self.deserializeInto(&@field(ptr, name)); } }, - builtin.TypeId.Union => { + .Union => { const info = @typeInfo(C).Union; if (info.tag_type) |TagType| { //we avoid duplicate iteration over the enum tags @@ -948,7 +948,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, @compileError("Cannot meaningfully deserialize " ++ @typeName(C) ++ " because it is an untagged union. Use a custom deserialize()."); }, - builtin.TypeId.Optional => { + .Optional => { const OC = comptime meta.Child(C); const exists = (try self.deserializeInt(u1)) > 0; if (!exists) { @@ -960,7 +960,7 @@ pub fn Deserializer(comptime endian: builtin.Endian, comptime packing: Packing, const val_ptr = &ptr.*.?; try self.deserializeInto(val_ptr); }, - builtin.TypeId.Enum => { + .Enum => { var value = try self.deserializeInt(@TagType(C)); ptr.* = try meta.intToEnum(C, value); }, @@ -1009,7 +1009,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co fn serializeInt(self: *Self, value: var) Error!void { const T = @TypeOf(value); - comptime assert(trait.is(builtin.TypeId.Int)(T) or trait.is(builtin.TypeId.Float)(T)); + comptime assert(trait.is(.Int)(T) or trait.is(.Float)(T)); const t_bit_count = comptime meta.bitCount(T); const u8_bit_count = comptime meta.bitCount(u8); @@ -1058,11 +1058,11 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co return; } - switch (@typeId(T)) { - builtin.TypeId.Void => return, - builtin.TypeId.Bool => try self.serializeInt(@as(u1, @boolToInt(value))), - builtin.TypeId.Float, builtin.TypeId.Int => try self.serializeInt(value), - builtin.TypeId.Struct => { + switch (@typeInfo(T)) { + .Void => return, + .Bool => try self.serializeInt(@as(u1, @boolToInt(value))), + .Float, .Int => try self.serializeInt(value), + .Struct => { const info = @typeInfo(T); inline for (info.Struct.fields) |*field_info| { @@ -1072,7 +1072,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co if (FieldType == void or FieldType == u0) continue; //It doesn't make sense to write pointers - if (comptime trait.is(builtin.TypeId.Pointer)(FieldType)) { + if (comptime trait.is(.Pointer)(FieldType)) { @compileError("Will not " ++ "serialize field " ++ name ++ " of struct " ++ @typeName(T) ++ " because it " ++ "is of pointer-type " ++ @typeName(FieldType) ++ "."); @@ -1080,7 +1080,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co try self.serialize(@field(value, name)); } }, - builtin.TypeId.Union => { + .Union => { const info = @typeInfo(T).Union; if (info.tag_type) |TagType| { const active_tag = meta.activeTag(value); @@ -1101,7 +1101,7 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co @compileError("Cannot meaningfully serialize " ++ @typeName(T) ++ " because it is an untagged union. Use a custom serialize()."); }, - builtin.TypeId.Optional => { + .Optional => { if (value == null) { try self.serializeInt(@as(u1, @boolToInt(false))); return; @@ -1112,10 +1112,10 @@ pub fn Serializer(comptime endian: builtin.Endian, comptime packing: Packing, co const val_ptr = &value.?; try self.serialize(val_ptr.*); }, - builtin.TypeId.Enum => { + .Enum => { try self.serializeInt(@enumToInt(value)); }, - else => @compileError("Cannot serialize " ++ @tagName(@typeId(T)) ++ " types (unimplemented)."), + else => @compileError("Cannot serialize " ++ @tagName(@typeInfo(T)) ++ " types (unimplemented)."), } } }; diff --git a/lib/std/math.zig b/lib/std/math.zig index 5885964c34..322fdc7755 100644 --- a/lib/std/math.zig +++ b/lib/std/math.zig @@ -1,6 +1,4 @@ -const builtin = @import("builtin"); const std = @import("std.zig"); -const TypeId = builtin.TypeId; const assert = std.debug.assert; const testing = std.testing; @@ -89,7 +87,7 @@ pub const snan = @import("math/nan.zig").snan; pub const inf = @import("math/inf.zig").inf; pub fn approxEq(comptime T: type, x: T, y: T, epsilon: T) bool { - assert(@typeId(T) == TypeId.Float); + assert(@typeInfo(T) == .Float); return fabs(x - y) < epsilon; } @@ -198,7 +196,7 @@ test "" { } pub fn floatMantissaBits(comptime T: type) comptime_int { - assert(@typeId(T) == builtin.TypeId.Float); + assert(@typeInfo(T) == .Float); return switch (T.bit_count) { 16 => 10, @@ -211,7 +209,7 @@ pub fn floatMantissaBits(comptime T: type) comptime_int { } pub fn floatExponentBits(comptime T: type) comptime_int { - assert(@typeId(T) == builtin.TypeId.Float); + assert(@typeInfo(T) == .Float); return switch (T.bit_count) { 16 => 5, @@ -526,7 +524,7 @@ fn testOverflow() void { pub fn absInt(x: var) !@TypeOf(x) { const T = @TypeOf(x); - comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt + comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt comptime assert(T.is_signed); // must pass a signed integer to absInt if (x == minInt(@TypeOf(x))) { @@ -560,7 +558,7 @@ fn testAbsFloat() void { pub fn divTrunc(comptime T: type, numerator: T, denominator: T) !T { @setRuntimeSafety(false); if (denominator == 0) return error.DivisionByZero; - if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; + if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; return @divTrunc(numerator, denominator); } @@ -581,7 +579,7 @@ fn testDivTrunc() void { pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T { @setRuntimeSafety(false); if (denominator == 0) return error.DivisionByZero; - if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; + if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; return @divFloor(numerator, denominator); } @@ -602,7 +600,7 @@ fn testDivFloor() void { pub fn divExact(comptime T: type, numerator: T, denominator: T) !T { @setRuntimeSafety(false); if (denominator == 0) return error.DivisionByZero; - if (@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; + if (@typeInfo(T) == .Int and T.is_signed and numerator == minInt(T) and denominator == -1) return error.Overflow; const result = @divTrunc(numerator, denominator); if (result * denominator != numerator) return error.UnexpectedRemainder; return result; @@ -727,8 +725,8 @@ test "math.negateCast" { /// Cast an integer to a different integer type. If the value doesn't fit, /// return an error. pub fn cast(comptime T: type, x: var) (error{Overflow}!T) { - comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer - comptime assert(@typeId(@TypeOf(x)) == builtin.TypeId.Int); // must pass an integer + comptime assert(@typeInfo(T) == .Int); // must pass an integer + comptime assert(@typeInfo(@TypeOf(x)) == .Int); // must pass an integer if (maxInt(@TypeOf(x)) > maxInt(T) and x > maxInt(T)) { return error.Overflow; } else if (minInt(@TypeOf(x)) < minInt(T) and x < minInt(T)) { @@ -793,7 +791,7 @@ fn testFloorPowerOfTwo() void { /// Only unsigned integers can be used. Zero is not an allowed input. /// Result is a type with 1 more bit than the input type. pub fn ceilPowerOfTwoPromote(comptime T: type, value: T) @IntType(T.is_signed, T.bit_count + 1) { - comptime assert(@typeId(T) == builtin.TypeId.Int); + comptime assert(@typeInfo(T) == .Int); comptime assert(!T.is_signed); assert(value != 0); 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 /// Only unsigned integers can be used. Zero is not an allowed input. /// If the value doesn't fit, returns an error. pub fn ceilPowerOfTwo(comptime T: type, value: T) (error{Overflow}!T) { - comptime assert(@typeId(T) == builtin.TypeId.Int); + comptime assert(@typeInfo(T) == .Int); comptime assert(!T.is_signed); comptime const PromotedType = @IntType(T.is_signed, T.bit_count + 1); comptime const overflowBit = @as(PromotedType, 1) << T.bit_count; @@ -878,10 +876,10 @@ test "std.math.log2_int_ceil" { pub fn lossyCast(comptime T: type, value: var) T { switch (@typeInfo(@TypeOf(value))) { - builtin.TypeId.Int => return @intToFloat(T, value), - builtin.TypeId.Float => return @floatCast(T, value), - builtin.TypeId.ComptimeInt => return @as(T, value), - builtin.TypeId.ComptimeFloat => return @as(T, value), + .Int => return @intToFloat(T, value), + .Float => return @floatCast(T, value), + .ComptimeInt => return @as(T, value), + .ComptimeFloat => return @as(T, value), else => @compileError("bad type"), } } diff --git a/lib/std/math/big/int.zig b/lib/std/math/big/int.zig index d42d9fc676..0ba3382641 100644 --- a/lib/std/math/big/int.zig +++ b/lib/std/math/big/int.zig @@ -1,5 +1,4 @@ const std = @import("../../std.zig"); -const builtin = @import("builtin"); const debug = std.debug; const testing = std.testing; const math = std.math; @@ -9,8 +8,6 @@ const ArrayList = std.ArrayList; const maxInt = std.math.maxInt; const minInt = std.math.minInt; -const TypeId = builtin.TypeId; - pub const Limb = usize; pub const DoubleLimb = @IntType(false, 2 * Limb.bit_count); pub const Log2Limb = math.Log2Int(Limb); @@ -270,7 +267,7 @@ pub const Int = struct { const T = @TypeOf(value); switch (@typeInfo(T)) { - TypeId.Int => |info| { + .Int => |info| { const UT = if (T.is_signed) @IntType(false, T.bit_count - 1) else T; try self.ensureCapacity(@sizeOf(UT) / @sizeOf(Limb)); @@ -294,7 +291,7 @@ pub const Int = struct { } } }, - TypeId.ComptimeInt => { + .ComptimeInt => { comptime var w_value = if (value < 0) -value else value; const req_limbs = @divFloor(math.log2(w_value), Limb.bit_count) + 1; @@ -332,8 +329,8 @@ pub const Int = struct { /// /// Returns an error if self cannot be narrowed into the requested type without truncation. pub fn to(self: Int, comptime T: type) ConvertError!T { - switch (@typeId(T)) { - TypeId.Int => { + switch (@typeInfo(T)) { + .Int => { const UT = @IntType(false, T.bit_count); if (self.bitCountTwosComp() > T.bit_count) { diff --git a/lib/std/math/big/rational.zig b/lib/std/math/big/rational.zig index 438c6c94fc..f54a234075 100644 --- a/lib/std/math/big/rational.zig +++ b/lib/std/math/big/rational.zig @@ -1,5 +1,4 @@ const std = @import("../../std.zig"); -const builtin = @import("builtin"); const debug = std.debug; const math = std.math; const mem = std.mem; @@ -7,8 +6,6 @@ const testing = std.testing; const Allocator = mem.Allocator; const ArrayList = std.ArrayList; -const TypeId = builtin.TypeId; - const bn = @import("int.zig"); const Limb = bn.Limb; const DoubleLimb = bn.DoubleLimb; @@ -129,7 +126,7 @@ pub const Rational = struct { /// completely represent the provided float. pub fn setFloat(self: *Rational, comptime T: type, f: T) !void { // Translated from golang.go/src/math/big/rat.go. - debug.assert(@typeId(T) == builtin.TypeId.Float); + debug.assert(@typeInfo(T) == .Float); const UnsignedIntType = @IntType(false, T.bit_count); const f_bits = @bitCast(UnsignedIntType, f); @@ -187,7 +184,7 @@ pub const Rational = struct { pub fn toFloat(self: Rational, comptime T: type) !T { // Translated from golang.go/src/math/big/rat.go. // TODO: Indicate whether the result is not exact. - debug.assert(@typeId(T) == builtin.TypeId.Float); + debug.assert(@typeInfo(T) == .Float); const fsize = T.bit_count; const BitReprType = @IntType(false, T.bit_count); @@ -653,7 +650,7 @@ test "big.rational gcd one large" { } fn extractLowBits(a: Int, comptime T: type) T { - testing.expect(@typeId(T) == builtin.TypeId.Int); + testing.expect(@typeInfo(T) == .Int); if (T.bit_count <= Limb.bit_count) { return @truncate(T, a.limbs[0]); diff --git a/lib/std/math/ln.zig b/lib/std/math/ln.zig index da3e60202d..555a786907 100644 --- a/lib/std/math/ln.zig +++ b/lib/std/math/ln.zig @@ -7,8 +7,6 @@ const std = @import("../std.zig"); const math = std.math; const expect = std.testing.expect; -const builtin = @import("builtin"); -const TypeId = builtin.TypeId; /// Returns the natural logarithm of x. /// @@ -19,21 +17,21 @@ const TypeId = builtin.TypeId; /// - ln(nan) = nan pub fn ln(x: var) @TypeOf(x) { const T = @TypeOf(x); - switch (@typeId(T)) { - TypeId.ComptimeFloat => { + switch (@typeInfo(T)) { + .ComptimeFloat => { return @as(comptime_float, ln_64(x)); }, - TypeId.Float => { + .Float => { return switch (T) { f32 => ln_32(x), f64 => ln_64(x), else => @compileError("ln not implemented for " ++ @typeName(T)), }; }, - TypeId.ComptimeInt => { + .ComptimeInt => { return @as(comptime_int, math.floor(ln_64(@as(f64, x)))); }, - TypeId.Int => { + .Int => { return @as(T, math.floor(ln_64(@as(f64, x)))); }, else => @compileError("ln not implemented for " ++ @typeName(T)), diff --git a/lib/std/math/log.zig b/lib/std/math/log.zig index 9d77397e17..6f5025cd50 100644 --- a/lib/std/math/log.zig +++ b/lib/std/math/log.zig @@ -6,8 +6,6 @@ const std = @import("../std.zig"); const math = std.math; -const builtin = @import("builtin"); -const TypeId = builtin.TypeId; const expect = std.testing.expect; /// Returns the logarithm of x for the provided base. @@ -16,24 +14,24 @@ pub fn log(comptime T: type, base: T, x: T) T { return math.log2(x); } else if (base == 10) { return math.log10(x); - } else if ((@typeId(T) == TypeId.Float or @typeId(T) == TypeId.ComptimeFloat) and base == math.e) { + } else if ((@typeInfo(T) == .Float or @typeInfo(T) == .ComptimeFloat) and base == math.e) { return math.ln(x); } const float_base = math.lossyCast(f64, base); - switch (@typeId(T)) { - TypeId.ComptimeFloat => { + switch (@typeInfo(T)) { + .ComptimeFloat => { return @as(comptime_float, math.ln(@as(f64, x)) / math.ln(float_base)); }, - TypeId.ComptimeInt => { + .ComptimeInt => { return @as(comptime_int, math.floor(math.ln(@as(f64, x)) / math.ln(float_base))); }, - builtin.TypeId.Int => { + .Int => { // TODO implement integer log without using float math return @floatToInt(T, math.floor(math.ln(@intToFloat(f64, x)) / math.ln(float_base))); }, - builtin.TypeId.Float => { + .Float => { switch (T) { f32 => return @floatCast(f32, math.ln(@as(f64, x)) / math.ln(float_base)), f64 => return math.ln(x) / math.ln(float_base), diff --git a/lib/std/math/log10.zig b/lib/std/math/log10.zig index a0deee69d1..7367af28c6 100644 --- a/lib/std/math/log10.zig +++ b/lib/std/math/log10.zig @@ -7,8 +7,6 @@ const std = @import("../std.zig"); const math = std.math; const testing = std.testing; -const builtin = @import("builtin"); -const TypeId = builtin.TypeId; const maxInt = std.math.maxInt; /// Returns the base-10 logarithm of x. @@ -20,21 +18,21 @@ const maxInt = std.math.maxInt; /// - log10(nan) = nan pub fn log10(x: var) @TypeOf(x) { const T = @TypeOf(x); - switch (@typeId(T)) { - TypeId.ComptimeFloat => { + switch (@typeInfo(T)) { + .ComptimeFloat => { return @as(comptime_float, log10_64(x)); }, - TypeId.Float => { + .Float => { return switch (T) { f32 => log10_32(x), f64 => log10_64(x), else => @compileError("log10 not implemented for " ++ @typeName(T)), }; }, - TypeId.ComptimeInt => { + .ComptimeInt => { return @as(comptime_int, math.floor(log10_64(@as(f64, x)))); }, - TypeId.Int => { + .Int => { return @floatToInt(T, math.floor(log10_64(@intToFloat(f64, x)))); }, else => @compileError("log10 not implemented for " ++ @typeName(T)), diff --git a/lib/std/math/log2.zig b/lib/std/math/log2.zig index d3b655a56a..54f8bc2baa 100644 --- a/lib/std/math/log2.zig +++ b/lib/std/math/log2.zig @@ -7,8 +7,6 @@ const std = @import("../std.zig"); const math = std.math; const expect = std.testing.expect; -const builtin = @import("builtin"); -const TypeId = builtin.TypeId; const maxInt = std.math.maxInt; /// Returns the base-2 logarithm of x. @@ -20,18 +18,18 @@ const maxInt = std.math.maxInt; /// - log2(nan) = nan pub fn log2(x: var) @TypeOf(x) { const T = @TypeOf(x); - switch (@typeId(T)) { - TypeId.ComptimeFloat => { + switch (@typeInfo(T)) { + .ComptimeFloat => { return @as(comptime_float, log2_64(x)); }, - TypeId.Float => { + .Float => { return switch (T) { f32 => log2_32(x), f64 => log2_64(x), else => @compileError("log2 not implemented for " ++ @typeName(T)), }; }, - TypeId.ComptimeInt => comptime { + .ComptimeInt => comptime { var result = 0; var x_shifted = x; while (b: { @@ -40,7 +38,7 @@ pub fn log2(x: var) @TypeOf(x) { }) : (result += 1) {} return result; }, - TypeId.Int => { + .Int => { return math.log2_int(T, x); }, else => @compileError("log2 not implemented for " ++ @typeName(T)), diff --git a/lib/std/meta.zig b/lib/std/meta.zig index 300bca3d0e..e302a80ef1 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -536,9 +536,8 @@ test "intToEnum with error return" { pub const IntToEnumError = error{InvalidEnumTag}; pub fn intToEnum(comptime Tag: type, tag_int: var) IntToEnumError!Tag { - comptime var i = 0; - inline while (i != @memberCount(Tag)) : (i += 1) { - const this_tag_value = @field(Tag, @memberName(Tag, i)); + inline for (@typeInfo(Tag).Enum.fields) |f| { + const this_tag_value = @field(Tag, f.name); if (tag_int == @enumToInt(this_tag_value)) { return this_tag_value; } diff --git a/lib/std/meta/trait.zig b/lib/std/meta/trait.zig index b17a4a2a93..c0fb8c5025 100644 --- a/lib/std/meta/trait.zig +++ b/lib/std/meta/trait.zig @@ -1,5 +1,5 @@ const std = @import("../std.zig"); -const builtin = @import("builtin"); +const builtin = std.builtin; const mem = std.mem; const debug = std.debug; const testing = std.testing; @@ -54,7 +54,7 @@ pub fn hasFn(comptime name: []const u8) TraitFn { if (!comptime isContainer(T)) return false; if (!comptime @hasDecl(T, name)) return false; const DeclType = @TypeOf(@field(T, name)); - return @typeId(DeclType) == .Fn; + return @typeInfo(DeclType) == .Fn; } }; return Closure.trait; @@ -105,7 +105,7 @@ test "std.meta.trait.hasField" { pub fn is(comptime id: builtin.TypeId) TraitFn { const Closure = struct { pub fn trait(comptime T: type) bool { - return id == @typeId(T); + return id == @typeInfo(T); } }; return Closure.trait; @@ -123,7 +123,7 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn { const Closure = struct { pub fn trait(comptime T: type) bool { if (!comptime isSingleItemPtr(T)) return false; - return id == @typeId(meta.Child(T)); + return id == @typeInfo(meta.Child(T)); } }; return Closure.trait; @@ -139,7 +139,7 @@ pub fn isSliceOf(comptime id: builtin.TypeId) TraitFn { const Closure = struct { pub fn trait(comptime T: type) bool { if (!comptime isSlice(T)) return false; - return id == @typeId(meta.Child(T)); + return id == @typeInfo(meta.Child(T)); } }; return Closure.trait; @@ -285,7 +285,7 @@ test "std.meta.trait.isIndexable" { } pub fn isNumber(comptime T: type) bool { - return switch (@typeId(T)) { + return switch (@typeInfo(T)) { .Int, .Float, .ComptimeInt, .ComptimeFloat => true, else => false, }; @@ -320,7 +320,7 @@ test "std.meta.trait.isConstPtr" { } pub fn isContainer(comptime T: type) bool { - return switch (@typeId(T)) { + return switch (@typeInfo(T)) { .Struct, .Union, .Enum => true, else => false, }; diff --git a/lib/std/special/build_runner.zig b/lib/std/special/build_runner.zig index 45dea79469..e917dcd8af 100644 --- a/lib/std/special/build_runner.zig +++ b/lib/std/special/build_runner.zig @@ -126,7 +126,7 @@ pub fn main() !void { } fn runBuild(builder: *Builder) anyerror!void { - switch (@typeId(@TypeOf(root.build).ReturnType)) { + switch (@typeInfo(@TypeOf(root.build).ReturnType)) { .Void => root.build(builder), .ErrorUnion => try root.build(builder), else => @compileError("expected return type of build to be 'void' or '!void'"), diff --git a/lib/std/thread.zig b/lib/std/thread.zig index c482405c44..500c94c044 100644 --- a/lib/std/thread.zig +++ b/lib/std/thread.zig @@ -158,7 +158,7 @@ pub const Thread = struct { }; fn threadMain(raw_arg: windows.LPVOID) callconv(.C) windows.DWORD { const arg = if (@sizeOf(Context) == 0) {} else @ptrCast(*Context, @alignCast(@alignOf(Context), raw_arg)).*; - switch (@typeId(@TypeOf(startFn).ReturnType)) { + switch (@typeInfo(@TypeOf(startFn).ReturnType)) { .Int => { return startFn(arg); }, @@ -201,7 +201,7 @@ pub const Thread = struct { fn linuxThreadMain(ctx_addr: usize) callconv(.C) u8 { const arg = if (@sizeOf(Context) == 0) {} else @intToPtr(*const Context, ctx_addr).*; - switch (@typeId(@TypeOf(startFn).ReturnType)) { + switch (@typeInfo(@TypeOf(startFn).ReturnType)) { .Int => { return startFn(arg); }, diff --git a/lib/std/zig/ast.zig b/lib/std/zig/ast.zig index f96428a6bc..bc4f6350d6 100644 --- a/lib/std/zig/ast.zig +++ b/lib/std/zig/ast.zig @@ -457,10 +457,9 @@ pub const Node = struct { } pub fn iterate(base: *Node, index: usize) ?*Node { - comptime var i = 0; - inline while (i < @memberCount(Id)) : (i += 1) { - if (base.id == @field(Id, @memberName(Id, i))) { - const T = @field(Node, @memberName(Id, i)); + inline for (@typeInfo(Id).Enum.fields) |f| { + if (base.id == @field(Id, f.name)) { + const T = @field(Node, f.name); return @fieldParentPtr(T, "base", base).iterate(index); } } @@ -468,10 +467,9 @@ pub const Node = struct { } pub fn firstToken(base: *const Node) TokenIndex { - comptime var i = 0; - inline while (i < @memberCount(Id)) : (i += 1) { - if (base.id == @field(Id, @memberName(Id, i))) { - const T = @field(Node, @memberName(Id, i)); + inline for (@typeInfo(Id).Enum.fields) |f| { + if (base.id == @field(Id, f.name)) { + const T = @field(Node, f.name); return @fieldParentPtr(T, "base", base).firstToken(); } } @@ -479,10 +477,9 @@ pub const Node = struct { } pub fn lastToken(base: *const Node) TokenIndex { - comptime var i = 0; - inline while (i < @memberCount(Id)) : (i += 1) { - if (base.id == @field(Id, @memberName(Id, i))) { - const T = @field(Node, @memberName(Id, i)); + inline for (@typeInfo(Id).Enum.fields) |f| { + if (base.id == @field(Id, f.name)) { + const T = @field(Node, f.name); return @fieldParentPtr(T, "base", base).lastToken(); } } @@ -490,10 +487,9 @@ pub const Node = struct { } pub fn typeToId(comptime T: type) Id { - comptime var i = 0; - inline while (i < @memberCount(Id)) : (i += 1) { - if (T == @field(Node, @memberName(Id, i))) { - return @field(Id, @memberName(Id, i)); + inline for (@typeInfo(Id).Enum.fields) |f| { + if (T == @field(Node, f.name)) { + return @field(Id, f.name); } } unreachable; diff --git a/lib/std/zig/parser_test.zig b/lib/std/zig/parser_test.zig index 5f28dd3b06..9a023bb525 100644 --- a/lib/std/zig/parser_test.zig +++ b/lib/std/zig/parser_test.zig @@ -1410,7 +1410,7 @@ test "zig fmt: same-line comment after non-block if expression" { test "zig fmt: same-line comment on comptime expression" { try testCanonical( \\test "" { - \\ comptime assert(@typeId(T) == builtin.TypeId.Int); // must pass an integer to absInt + \\ comptime assert(@typeInfo(T) == .Int); // must pass an integer to absInt \\} \\ ); diff --git a/lib/std/zig/render.zig b/lib/std/zig/render.zig index 069635d42e..ee6ee37d5d 100644 --- a/lib/std/zig/render.zig +++ b/lib/std/zig/render.zig @@ -1,5 +1,4 @@ const std = @import("../std.zig"); -const builtin = @import("builtin"); const assert = std.debug.assert; const mem = std.mem; const ast = std.zig.ast; @@ -14,7 +13,7 @@ pub const Error = error{ /// Returns whether anything changed pub fn render(allocator: *mem.Allocator, stream: var, tree: *ast.Tree) (@TypeOf(stream).Child.Error || Error)!bool { - comptime assert(@typeId(@TypeOf(stream)) == builtin.TypeId.Pointer); + comptime assert(@typeInfo(@TypeOf(stream)) == .Pointer); var anything_changed: bool = false; diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig index a8f4980fa8..576294a7d7 100644 --- a/src-self-hosted/ir.zig +++ b/src-self-hosted/ir.zig @@ -76,20 +76,18 @@ pub const Inst = struct { } pub fn typeToId(comptime T: type) Id { - comptime var i = 0; - inline while (i < @memberCount(Id)) : (i += 1) { - if (T == @field(Inst, @memberName(Id, i))) { - return @field(Id, @memberName(Id, i)); + inline for (@typeInfo(Id).Enum.fields) |f| { + if (T == @field(Inst, f.name)) { + return @field(Id, f.name); } } unreachable; } pub fn dump(base: *const Inst) void { - comptime var i = 0; - inline while (i < @memberCount(Id)) : (i += 1) { - if (base.id == @field(Id, @memberName(Id, i))) { - const T = @field(Inst, @memberName(Id, i)); + inline for (@typeInfo(Id).Enum.fields) |f| { + if (base.id == @field(Id, f.name)) { + const T = @field(Inst, f.name); std.debug.warn("#{} = {}(", .{ base.debug_id, @tagName(base.id) }); @fieldParentPtr(T, "base", base).dump(); std.debug.warn(")", .{}); @@ -100,10 +98,9 @@ pub const Inst = struct { } pub fn hasSideEffects(base: *const Inst) bool { - comptime var i = 0; - inline while (i < @memberCount(Id)) : (i += 1) { - if (base.id == @field(Id, @memberName(Id, i))) { - const T = @field(Inst, @memberName(Id, i)); + inline for (@typeInfo(Id).Enum.fields) |f| { + if (base.id == @field(Id, f.name)) { + const T = @field(Inst, f.name); return @fieldParentPtr(T, "base", base).hasSideEffects(); } } @@ -1805,21 +1802,19 @@ pub const Builder = struct { }; // Look at the params and ref() other instructions - comptime var i = 0; - inline while (i < @memberCount(I.Params)) : (i += 1) { - const FieldType = comptime @TypeOf(@field(@as(I.Params, undefined), @memberName(I.Params, i))); - switch (FieldType) { - *Inst => @field(inst.params, @memberName(I.Params, i)).ref(self), - *BasicBlock => @field(inst.params, @memberName(I.Params, i)).ref(self), - ?*Inst => if (@field(inst.params, @memberName(I.Params, i))) |other| other.ref(self), + inline for (@typeInfo(I.Params).Struct.fields) |f| { + switch (f.fiedl_type) { + *Inst => @field(inst.params, f.name).ref(self), + *BasicBlock => @field(inst.params, f.name).ref(self), + ?*Inst => if (@field(inst.params, f.name)) |other| other.ref(self), []*Inst => { // TODO https://github.com/ziglang/zig/issues/1269 - for (@field(inst.params, @memberName(I.Params, i))) |other| + for (@field(inst.params, f.name)) |other| other.ref(self); }, []*BasicBlock => { // TODO https://github.com/ziglang/zig/issues/1269 - for (@field(inst.params, @memberName(I.Params, i))) |other| + for (@field(inst.params, f.name)) |other| other.ref(self); }, Type.Pointer.Mut, @@ -1831,7 +1826,7 @@ pub const Builder = struct { => {}, // it's ok to add more types here, just make sure that // any instructions and basic blocks are ref'd appropriately - else => @compileError("unrecognized type in Params: " ++ @typeName(FieldType)), + else => @compileError("unrecognized type in Params: " ++ @typeName(f.field_type)), } } diff --git a/src-self-hosted/translate_c.zig b/src-self-hosted/translate_c.zig index cd8d0b9d63..b33c758c38 100644 --- a/src-self-hosted/translate_c.zig +++ b/src-self-hosted/translate_c.zig @@ -5381,15 +5381,15 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, return error.ParseError; } - //if (@typeId(@TypeOf(x)) == .Pointer) + //if (@typeInfo(@TypeOf(x)) == .Pointer) // @ptrCast(dest, x) - //else if (@typeId(@TypeOf(x)) == .Integer) + //else if (@typeInfo(@TypeOf(x)) == .Integer) // @intToPtr(dest, x) //else // @as(dest, x) const if_1 = try transCreateNodeIf(c); - const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeId"); + const type_id_1 = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); const type_of_1 = try transCreateNodeBuiltinFnCall(c, "@TypeOf"); try type_id_1.params.push(&type_of_1.base); try type_of_1.params.push(node_to_cast); @@ -5416,7 +5416,7 @@ fn parseCPrimaryExpr(c: *Context, it: *CTokenList.Iterator, source: []const u8, if_1.@"else" = else_1; const if_2 = try transCreateNodeIf(c); - const type_id_2 = try transCreateNodeBuiltinFnCall(c, "@typeId"); + const type_id_2 = try transCreateNodeBuiltinFnCall(c, "@typeInfo"); const type_of_2 = try transCreateNodeBuiltinFnCall(c, "@TypeOf"); try type_id_2.params.push(&type_of_2.base); try type_of_2.params.push(node_to_cast); diff --git a/test/compile_errors.zig b/test/compile_errors.zig index 39c83c4403..a4bb333873 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2942,14 +2942,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:11:13: error: error.B not a member of error set 'Set2'", }); - cases.add("@memberCount of error", - \\comptime { - \\ _ = @memberCount(anyerror); - \\} - , &[_][]const u8{ - "tmp.zig:2:9: error: global error set member count not available at comptime", - }); - cases.add("duplicate error value in error set", \\const Foo = error { \\ Bar, @@ -5964,76 +5956,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { "tmp.zig:2:32: error: arg index 2 out of bounds; 'fn(i32, i32) i32' has 2 arguments", }); - cases.add("@memberType on unsupported type", - \\comptime { - \\ _ = @memberType(i32, 0); - \\} - , &[_][]const u8{ - "tmp.zig:2:21: error: type 'i32' does not support @memberType", - }); - - cases.add("@memberType on enum", - \\comptime { - \\ _ = @memberType(Foo, 0); - \\} - \\const Foo = enum {A,}; - , &[_][]const u8{ - "tmp.zig:2:21: error: type 'Foo' does not support @memberType", - }); - - cases.add("@memberType struct out of bounds", - \\comptime { - \\ _ = @memberType(Foo, 0); - \\} - \\const Foo = struct {}; - , &[_][]const u8{ - "tmp.zig:2:26: error: member index 0 out of bounds; 'Foo' has 0 members", - }); - - cases.add("@memberType union out of bounds", - \\comptime { - \\ _ = @memberType(Foo, 1); - \\} - \\const Foo = union {A: void,}; - , &[_][]const u8{ - "tmp.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members", - }); - - cases.add("@memberName on unsupported type", - \\comptime { - \\ _ = @memberName(i32, 0); - \\} - , &[_][]const u8{ - "tmp.zig:2:21: error: type 'i32' does not support @memberName", - }); - - cases.add("@memberName struct out of bounds", - \\comptime { - \\ _ = @memberName(Foo, 0); - \\} - \\const Foo = struct {}; - , &[_][]const u8{ - "tmp.zig:2:26: error: member index 0 out of bounds; 'Foo' has 0 members", - }); - - cases.add("@memberName enum out of bounds", - \\comptime { - \\ _ = @memberName(Foo, 1); - \\} - \\const Foo = enum {A,}; - , &[_][]const u8{ - "tmp.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members", - }); - - cases.add("@memberName union out of bounds", - \\comptime { - \\ _ = @memberName(Foo, 1); - \\} - \\const Foo = union {A:i32,}; - , &[_][]const u8{ - "tmp.zig:2:26: error: member index 1 out of bounds; 'Foo' has 1 members", - }); - cases.add("calling var args extern function, passing array instead of pointer", \\export fn entry() void { \\ foo("hello".*,); diff --git a/test/stage1/behavior/bugs/3742.zig b/test/stage1/behavior/bugs/3742.zig index 01bcb49f3c..f09127a66f 100644 --- a/test/stage1/behavior/bugs/3742.zig +++ b/test/stage1/behavior/bugs/3742.zig @@ -17,7 +17,7 @@ pub const GET = struct { }; pub fn isCommand(comptime T: type) bool { - const tid = @typeId(T); + const tid = @typeInfo(T); return (tid == .Struct or tid == .Enum or tid == .Union) and @hasDecl(T, "Redis") and @hasDecl(T.Redis, "Command"); } diff --git a/test/stage1/behavior/enum.zig b/test/stage1/behavior/enum.zig index 6187cee431..18489ba24b 100644 --- a/test/stage1/behavior/enum.zig +++ b/test/stage1/behavior/enum.zig @@ -96,8 +96,8 @@ test "enum type" { const bar = Bar.B; expect(bar == Bar.B); - expect(@memberCount(Foo) == 3); - expect(@memberCount(Bar) == 4); + expect(@typeInfo(Foo).Union.fields.len == 3); + expect(@typeInfo(Bar).Enum.fields.len == 4); expect(@sizeOf(Foo) == @sizeOf(FooNoVoid)); expect(@sizeOf(Bar) == 1); } diff --git a/test/stage1/behavior/error.zig b/test/stage1/behavior/error.zig index d4f64b7cff..def7fd679d 100644 --- a/test/stage1/behavior/error.zig +++ b/test/stage1/behavior/error.zig @@ -3,7 +3,6 @@ const expect = std.testing.expect; const expectError = std.testing.expectError; const expectEqual = std.testing.expectEqual; const mem = std.mem; -const builtin = @import("builtin"); pub fn foo() anyerror!i32 { const x = try bar(); @@ -84,8 +83,8 @@ test "error union type " { fn testErrorUnionType() void { const x: anyerror!i32 = 1234; if (x) |value| expect(value == 1234) else |_| unreachable; - expect(@typeId(@TypeOf(x)) == builtin.TypeId.ErrorUnion); - expect(@typeId(@TypeOf(x).ErrorSet) == builtin.TypeId.ErrorSet); + expect(@typeInfo(@TypeOf(x)) == .ErrorUnion); + expect(@typeInfo(@TypeOf(x).ErrorSet) == .ErrorSet); expect(@TypeOf(x).ErrorSet == anyerror); } @@ -100,7 +99,7 @@ const MyErrSet = error{ }; fn testErrorSetType() void { - expect(@memberCount(MyErrSet) == 2); + expect(@typeInfo(MyErrSet).ErrorSet.?.len == 2); const a: MyErrSet!i32 = 5678; const b: MyErrSet!i32 = MyErrSet.OutOfMemory; diff --git a/test/stage1/behavior/eval.zig b/test/stage1/behavior/eval.zig index 099434a869..8e70e97aaa 100644 --- a/test/stage1/behavior/eval.zig +++ b/test/stage1/behavior/eval.zig @@ -654,8 +654,8 @@ test "call method with comptime pass-by-non-copying-value self parameter" { expect(b == 2); } -test "@tagName of @typeId" { - const str = @tagName(@typeId(u8)); +test "@tagName of @typeInfo" { + const str = @tagName(@typeInfo(u8)); expect(std.mem.eql(u8, str, "Int")); } diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index a3a008c1d2..032f2429ed 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -407,7 +407,6 @@ fn testArray2DConstDoublePtr(ptr: *const f32) void { expect(ptr2[1] == 2.0); } -const Tid = builtin.TypeId; const AStruct = struct { x: i32, }; @@ -424,40 +423,6 @@ const AUnion = union { Two: void, }; -test "@typeId" { - comptime { - expect(@typeId(type) == Tid.Type); - expect(@typeId(void) == Tid.Void); - expect(@typeId(bool) == Tid.Bool); - expect(@typeId(noreturn) == Tid.NoReturn); - expect(@typeId(i8) == Tid.Int); - expect(@typeId(u8) == Tid.Int); - expect(@typeId(i64) == Tid.Int); - expect(@typeId(u64) == Tid.Int); - expect(@typeId(f32) == Tid.Float); - expect(@typeId(f64) == Tid.Float); - expect(@typeId(*f32) == Tid.Pointer); - expect(@typeId([2]u8) == Tid.Array); - expect(@typeId(AStruct) == Tid.Struct); - expect(@typeId(@TypeOf(1)) == Tid.ComptimeInt); - expect(@typeId(@TypeOf(1.0)) == Tid.ComptimeFloat); - expect(@typeId(@TypeOf(undefined)) == Tid.Undefined); - expect(@typeId(@TypeOf(null)) == Tid.Null); - expect(@typeId(?i32) == Tid.Optional); - expect(@typeId(anyerror!i32) == Tid.ErrorUnion); - expect(@typeId(anyerror) == Tid.ErrorSet); - expect(@typeId(AnEnum) == Tid.Enum); - expect(@typeId(@TypeOf(AUnionEnum.One)) == Tid.Enum); - expect(@typeId(AUnionEnum) == Tid.Union); - expect(@typeId(AUnion) == Tid.Union); - expect(@typeId(fn () void) == Tid.Fn); - expect(@typeId(@TypeOf(builtin)) == Tid.Type); - // TODO bound fn - // TODO arg tuple - // TODO opaque - } -} - test "@typeName" { const Struct = struct {}; const Union = union { diff --git a/test/stage1/behavior/reflection.zig b/test/stage1/behavior/reflection.zig index 37bf2a582c..a32f97e8ac 100644 --- a/test/stage1/behavior/reflection.zig +++ b/test/stage1/behavior/reflection.zig @@ -26,36 +26,6 @@ fn dummy(a: bool, b: i32, c: f32) i32 { return 1234; } -test "reflection: struct member types and names" { - comptime { - expect(@memberCount(Foo) == 3); - - expect(@memberType(Foo, 0) == i32); - expect(@memberType(Foo, 1) == bool); - expect(@memberType(Foo, 2) == void); - - expect(mem.eql(u8, @memberName(Foo, 0), "one")); - expect(mem.eql(u8, @memberName(Foo, 1), "two")); - expect(mem.eql(u8, @memberName(Foo, 2), "three")); - } -} - -test "reflection: enum member types and names" { - comptime { - expect(@memberCount(Bar) == 4); - - expect(@memberType(Bar, 0) == void); - expect(@memberType(Bar, 1) == i32); - expect(@memberType(Bar, 2) == bool); - expect(@memberType(Bar, 3) == f64); - - expect(mem.eql(u8, @memberName(Bar, 0), "One")); - expect(mem.eql(u8, @memberName(Bar, 1), "Two")); - expect(mem.eql(u8, @memberName(Bar, 2), "Three")); - expect(mem.eql(u8, @memberName(Bar, 3), "Four")); - } -} - test "reflection: @field" { var f = Foo{ .one = 42, diff --git a/test/stage1/behavior/union.zig b/test/stage1/behavior/union.zig index 4625b7573a..7f49049f58 100644 --- a/test/stage1/behavior/union.zig +++ b/test/stage1/behavior/union.zig @@ -531,7 +531,7 @@ var glbl: Foo1 = undefined; test "global union with single field is correctly initialized" { glbl = Foo1{ - .f = @memberType(Foo1, 0){ .x = 123 }, + .f = @typeInfo(Foo1).Union.fields[0].field_type{ .x = 123 }, }; expect(glbl.f.x == 123); } diff --git a/test/translate_c.zig b/test/translate_c.zig index e3c68a6d53..701513153c 100644 --- a/test/translate_c.zig +++ b/test/translate_c.zig @@ -1354,7 +1354,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void { cases.add("macro pointer cast", \\#define NRF_GPIO ((NRF_GPIO_Type *) NRF_GPIO_BASE) , &[_][]const u8{ - \\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); + \\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); }); cases.add("basic macro function", @@ -2538,11 +2538,11 @@ pub fn addCases(cases: *tests.TranslateCContext) void { \\#define FOO(bar) baz((void *)(baz)) \\#define BAR (void*) a , &[_][]const u8{ - \\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))) { - \\ 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)); + \\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))) { + \\ 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)); \\} , - \\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); + \\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); }); cases.add("macro conditional operator", From 3458fb891d8c7072a9bff6a32db9f3105ab72aa4 Mon Sep 17 00:00:00 2001 From: Vexu Date: Mon, 24 Feb 2020 23:21:11 +0200 Subject: [PATCH 2/4] remove `@typeId`, `@memberCount`, `@memberName` and `@memberType` from the compiler --- doc/langref.html.in | 84 ++----------- src/all_types.hpp | 34 ------ src/codegen.cpp | 4 - src/ir.cpp | 282 -------------------------------------------- src/ir_print.cpp | 48 -------- 5 files changed, 8 insertions(+), 444 deletions(-) diff --git a/doc/langref.html.in b/doc/langref.html.in index 4ac9cf47e0..d186e68451 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -2810,14 +2810,10 @@ test "@TagType" { assert(@TagType(Small) == u2); } -// @memberCount tells how many fields an enum has: -test "@memberCount" { - assert(@memberCount(Small) == 4); -} - -// @memberName tells the name of a field in an enum: -test "@memberName" { - assert(mem.eql(u8, @memberName(Small, 1), "Two")); +// @typeInfo tells us the field count and the fields name: +test "@typeInfo" { + assert(@typeInfo(Small).Enum.fields.len == 4); + assert(mem.eql(u8, @typeInfo(Small).Enum.fields[0].name, "Two")); } // @tagName gives a []const u8 representation of an enum value: @@ -2825,7 +2821,7 @@ test "@tagName" { assert(mem.eql(u8, @tagName(Small.Three), "Three")); } {#code_end#} - {#see_also|@memberName|@memberCount|@tagName|@sizeOf#} + {#see_also|@typeInfo|@tagName|@sizeOf#} {#header_open|extern enum#}

@@ -6816,7 +6812,7 @@ async fn func(y: *i32) void { Asserts that {#syntax#}@sizeOf(@TypeOf(value)) == @sizeOf(DestType){#endsyntax#}.

- Asserts that {#syntax#}@typeId(DestType) != @import("builtin").TypeId.Pointer{#endsyntax#}. Use {#syntax#}@ptrCast{#endsyntax#} or {#syntax#}@intToPtr{#endsyntax#} if you need this. + Asserts that {#syntax#}@typeInfo(DestType) != .Pointer{#endsyntax#}. Use {#syntax#}@ptrCast{#endsyntax#} or {#syntax#}@intToPtr{#endsyntax#} if you need this.

Can be used for these things for example: @@ -7269,7 +7265,7 @@ test "main" {

Floored division. Rounds toward negative infinity. For unsigned integers it is the same as {#syntax#}numerator / denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator != 0{#endsyntax#} and - {#syntax#}!(@typeId(T) == builtin.TypeId.Int and T.is_signed and numerator == std.math.minInt(T) and denominator == -1){#endsyntax#}. + {#syntax#}!(@typeInfo(T) == .Int and T.is_signed and numerator == std.math.minInt(T) and denominator == -1){#endsyntax#}.