diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2024-08-28 02:35:53 +0100 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2024-08-28 08:39:59 +0100 |
| commit | 0fe3fd01ddc2cd49c6a2b939577d16b9d2c65ea9 (patch) | |
| tree | 2c07fddf2b6230360fe618c4de192bc2d24eeaf7 /lib/std/meta.zig | |
| parent | 1a178d499537b922ff05c5d0186ed5a00dbb1a9b (diff) | |
| download | zig-0fe3fd01ddc2cd49c6a2b939577d16b9d2c65ea9.tar.gz zig-0fe3fd01ddc2cd49c6a2b939577d16b9d2c65ea9.zip | |
std: update `std.builtin.Type` fields to follow naming conventions
The compiler actually doesn't need any functional changes for this: Sema
does reification based on the tag indices of `std.builtin.Type` already!
So, no zig1.wasm update is necessary.
This change is necessary to disallow name clashes between fields and
decls on a type, which is a prerequisite of #9938.
Diffstat (limited to 'lib/std/meta.zig')
| -rw-r--r-- | lib/std/meta.zig | 190 |
1 files changed, 95 insertions, 95 deletions
diff --git a/lib/std/meta.zig b/lib/std/meta.zig index a06674f086..3d3bfca1f9 100644 --- a/lib/std/meta.zig +++ b/lib/std/meta.zig @@ -21,11 +21,11 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T { // TODO The '100' here is arbitrary and should be increased when possible: // - https://github.com/ziglang/zig/issues/4055 // - https://github.com/ziglang/zig/issues/3863 - if (@typeInfo(T).Enum.fields.len <= 100) { + if (@typeInfo(T).@"enum".fields.len <= 100) { const kvs = comptime build_kvs: { const EnumKV = struct { []const u8, T }; - var kvs_array: [@typeInfo(T).Enum.fields.len]EnumKV = undefined; - for (@typeInfo(T).Enum.fields, 0..) |enumField, i| { + var kvs_array: [@typeInfo(T).@"enum".fields.len]EnumKV = undefined; + for (@typeInfo(T).@"enum".fields, 0..) |enumField, i| { kvs_array[i] = .{ enumField.name, @field(T, enumField.name) }; } break :build_kvs kvs_array[0..]; @@ -33,7 +33,7 @@ pub fn stringToEnum(comptime T: type, str: []const u8) ?T { const map = std.StaticStringMap(T).initComptime(kvs); return map.get(str); } else { - inline for (@typeInfo(T).Enum.fields) |enumField| { + inline for (@typeInfo(T).@"enum".fields) |enumField| { if (mem.eql(u8, str, enumField.name)) { return @field(T, enumField.name); } @@ -58,11 +58,11 @@ test stringToEnum { /// If T is a pointer type the alignment of the type it points to is returned. pub fn alignment(comptime T: type) comptime_int { return switch (@typeInfo(T)) { - .Optional => |info| switch (@typeInfo(info.child)) { - .Pointer, .Fn => alignment(info.child), + .optional => |info| switch (@typeInfo(info.child)) { + .pointer, .@"fn" => alignment(info.child), else => @alignOf(T), }, - .Pointer => |info| info.alignment, + .pointer => |info| info.alignment, else => @alignOf(T), }; } @@ -81,10 +81,10 @@ test alignment { /// Given a parameterized type (array, vector, pointer, optional), returns the "child type". pub fn Child(comptime T: type) type { return switch (@typeInfo(T)) { - .Array => |info| info.child, - .Vector => |info| info.child, - .Pointer => |info| info.child, - .Optional => |info| info.child, + .array => |info| info.child, + .vector => |info| info.child, + .pointer => |info| info.child, + .optional => |info| info.child, else => @compileError("Expected pointer, optional, array or vector type, found '" ++ @typeName(T) ++ "'"), }; } @@ -100,17 +100,17 @@ test Child { /// Given a "memory span" type (array, slice, vector, or pointer to such), returns the "element type". pub fn Elem(comptime T: type) type { switch (@typeInfo(T)) { - .Array => |info| return info.child, - .Vector => |info| return info.child, - .Pointer => |info| switch (info.size) { + .array => |info| return info.child, + .vector => |info| return info.child, + .pointer => |info| switch (info.size) { .One => switch (@typeInfo(info.child)) { - .Array => |array_info| return array_info.child, - .Vector => |vector_info| return vector_info.child, + .array => |array_info| return array_info.child, + .vector => |vector_info| return vector_info.child, else => {}, }, .Many, .C, .Slice => return info.child, }, - .Optional => |info| return Elem(info.child), + .optional => |info| return Elem(info.child), else => {}, } @compileError("Expected pointer, slice, array or vector type, found '" ++ @typeName(T) ++ "'"); @@ -132,18 +132,18 @@ test Elem { /// Result is always comptime-known. pub inline fn sentinel(comptime T: type) ?Elem(T) { switch (@typeInfo(T)) { - .Array => |info| { + .array => |info| { const sentinel_ptr = info.sentinel orelse return null; return @as(*const info.child, @ptrCast(sentinel_ptr)).*; }, - .Pointer => |info| { + .pointer => |info| { switch (info.size) { .Many, .Slice => { const sentinel_ptr = info.sentinel orelse return null; return @as(*align(1) const info.child, @ptrCast(sentinel_ptr)).*; }, .One => switch (@typeInfo(info.child)) { - .Array => |array_info| { + .array => |array_info| { const sentinel_ptr = array_info.sentinel orelse return null; return @as(*align(1) const array_info.child, @ptrCast(sentinel_ptr)).*; }, @@ -177,17 +177,17 @@ fn testSentinel() !void { /// Given a "memory span" type, returns the same type except with the given sentinel value. pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { switch (@typeInfo(T)) { - .Pointer => |info| switch (info.size) { + .pointer => |info| switch (info.size) { .One => switch (@typeInfo(info.child)) { - .Array => |array_info| return @Type(.{ - .Pointer = .{ + .array => |array_info| return @Type(.{ + .pointer = .{ .size = info.size, .is_const = info.is_const, .is_volatile = info.is_volatile, .alignment = info.alignment, .address_space = info.address_space, .child = @Type(.{ - .Array = .{ + .array = .{ .len = array_info.len, .child = array_info.child, .sentinel = @as(?*const anyopaque, @ptrCast(&sentinel_val)), @@ -200,7 +200,7 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { else => {}, }, .Many, .Slice => return @Type(.{ - .Pointer = .{ + .pointer = .{ .size = info.size, .is_const = info.is_const, .is_volatile = info.is_volatile, @@ -213,12 +213,12 @@ pub fn Sentinel(comptime T: type, comptime sentinel_val: Elem(T)) type { }), else => {}, }, - .Optional => |info| switch (@typeInfo(info.child)) { - .Pointer => |ptr_info| switch (ptr_info.size) { + .optional => |info| switch (@typeInfo(info.child)) { + .pointer => |ptr_info| switch (ptr_info.size) { .Many => return @Type(.{ - .Optional = .{ + .optional = .{ .child = @Type(.{ - .Pointer = .{ + .pointer = .{ .size = ptr_info.size, .is_const = ptr_info.is_const, .is_volatile = ptr_info.is_volatile, @@ -244,8 +244,8 @@ pub const assumeSentinel = @compileError("This function has been removed, consid pub fn containerLayout(comptime T: type) Type.ContainerLayout { return switch (@typeInfo(T)) { - .Struct => |info| info.layout, - .Union => |info| info.layout, + .@"struct" => |info| info.layout, + .@"union" => |info| info.layout, else => @compileError("expected struct or union type, found '" ++ @typeName(T) ++ "'"), }; } @@ -276,10 +276,10 @@ test containerLayout { /// directly when you know what kind of type it is. pub fn declarations(comptime T: type) []const Type.Declaration { return switch (@typeInfo(T)) { - .Struct => |info| info.decls, - .Enum => |info| info.decls, - .Union => |info| info.decls, - .Opaque => |info| info.decls, + .@"struct" => |info| info.decls, + .@"enum" => |info| info.decls, + .@"union" => |info| info.decls, + .@"opaque" => |info| info.decls, else => @compileError("Expected struct, enum, union, or opaque type, found '" ++ @typeName(T) ++ "'"), }; } @@ -350,17 +350,17 @@ test declarationInfo { } } pub fn fields(comptime T: type) switch (@typeInfo(T)) { - .Struct => []const Type.StructField, - .Union => []const Type.UnionField, - .ErrorSet => []const Type.Error, - .Enum => []const Type.EnumField, + .@"struct" => []const Type.StructField, + .@"union" => []const Type.UnionField, + .@"enum" => []const Type.EnumField, + .error_set => []const Type.Error, else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), } { return switch (@typeInfo(T)) { - .Struct => |info| info.fields, - .Union => |info| info.fields, - .Enum => |info| info.fields, - .ErrorSet => |errors| errors.?, // must be non global error set + .@"struct" => |info| info.fields, + .@"union" => |info| info.fields, + .@"enum" => |info| info.fields, + .error_set => |errors| errors.?, // must be non global error set else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), }; } @@ -395,10 +395,10 @@ test fields { } pub fn fieldInfo(comptime T: type, comptime field: FieldEnum(T)) switch (@typeInfo(T)) { - .Struct => Type.StructField, - .Union => Type.UnionField, - .ErrorSet => Type.Error, - .Enum => Type.EnumField, + .@"struct" => Type.StructField, + .@"union" => Type.UnionField, + .@"enum" => Type.EnumField, + .error_set => Type.Error, else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"), } { return fields(T)[@intFromEnum(field)]; @@ -430,7 +430,7 @@ test fieldInfo { } pub fn FieldType(comptime T: type, comptime field: FieldEnum(T)) type { - if (@typeInfo(T) != .Struct and @typeInfo(T) != .Union) { + if (@typeInfo(T) != .@"struct" and @typeInfo(T) != .@"union") { @compileError("Expected struct or union, found '" ++ @typeName(T) ++ "'"); } @@ -528,7 +528,7 @@ pub fn FieldEnum(comptime T: type) type { if (field_infos.len == 0) { return @Type(.{ - .Enum = .{ + .@"enum" = .{ .tag_type = u0, .fields = &.{}, .decls = &.{}, @@ -537,8 +537,8 @@ pub fn FieldEnum(comptime T: type) type { }); } - if (@typeInfo(T) == .Union) { - if (@typeInfo(T).Union.tag_type) |tag_type| { + if (@typeInfo(T) == .@"union") { + if (@typeInfo(T).@"union".tag_type) |tag_type| { for (std.enums.values(tag_type), 0..) |v, i| { if (@intFromEnum(v) != i) break; // enum values not consecutive if (!std.mem.eql(u8, @tagName(v), field_infos[i].name)) break; // fields out of order @@ -557,7 +557,7 @@ pub fn FieldEnum(comptime T: type) type { }; } return @Type(.{ - .Enum = .{ + .@"enum" = .{ .tag_type = std.math.IntFittingRange(0, field_infos.len - 1), .fields = &enumFields, .decls = &decls, @@ -568,17 +568,17 @@ pub fn FieldEnum(comptime T: type) type { fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void { // TODO: https://github.com/ziglang/zig/issues/7419 - // testing.expectEqual(@typeInfo(expected).Enum, @typeInfo(actual).Enum); + // testing.expectEqual(@typeInfo(expected).@"enum", @typeInfo(actual).@"enum"); try testing.expectEqual( - @typeInfo(expected).Enum.tag_type, - @typeInfo(actual).Enum.tag_type, + @typeInfo(expected).@"enum".tag_type, + @typeInfo(actual).@"enum".tag_type, ); // For comparing decls and fields, we cannot use the meta eql function here // because the language does not guarantee that the slice pointers for field names // and decl names will be the same. comptime { - const expected_fields = @typeInfo(expected).Enum.fields; - const actual_fields = @typeInfo(actual).Enum.fields; + const expected_fields = @typeInfo(expected).@"enum".fields; + const actual_fields = @typeInfo(actual).@"enum".fields; if (expected_fields.len != actual_fields.len) return error.FailedTest; for (expected_fields, 0..) |expected_field, i| { const actual_field = actual_fields[i]; @@ -587,8 +587,8 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void { } } comptime { - const expected_decls = @typeInfo(expected).Enum.decls; - const actual_decls = @typeInfo(actual).Enum.decls; + const expected_decls = @typeInfo(expected).@"enum".decls; + const actual_decls = @typeInfo(actual).@"enum".decls; if (expected_decls.len != actual_decls.len) return error.FailedTest; for (expected_decls, 0..) |expected_decl, i| { const actual_decl = actual_decls[i]; @@ -596,8 +596,8 @@ fn expectEqualEnum(expected: anytype, actual: @TypeOf(expected)) !void { } } try testing.expectEqual( - @typeInfo(expected).Enum.is_exhaustive, - @typeInfo(actual).Enum.is_exhaustive, + @typeInfo(expected).@"enum".is_exhaustive, + @typeInfo(actual).@"enum".is_exhaustive, ); } @@ -627,7 +627,7 @@ pub fn DeclEnum(comptime T: type) type { enumDecls[i] = .{ .name = field.name ++ "", .value = i }; } return @Type(.{ - .Enum = .{ + .@"enum" = .{ .tag_type = std.math.IntFittingRange(0, fieldInfos.len - 1), .fields = &enumDecls, .decls = &decls, @@ -661,8 +661,8 @@ test DeclEnum { pub fn Tag(comptime T: type) type { return switch (@typeInfo(T)) { - .Enum => |info| info.tag_type, - .Union => |info| info.tag_type orelse @compileError(@typeName(T) ++ " has no tag type"), + .@"enum" => |info| info.tag_type, + .@"union" => |info| info.tag_type orelse @compileError(@typeName(T) ++ " has no tag type"), else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"), }; } @@ -708,7 +708,7 @@ test activeTag { const TagPayloadType = TagPayload; pub fn TagPayloadByName(comptime U: type, comptime tag_name: []const u8) type { - const info = @typeInfo(U).Union; + const info = @typeInfo(U).@"union"; inline for (info.fields) |field_info| { if (comptime mem.eql(u8, field_info.name, tag_name)) @@ -742,20 +742,20 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool { const T = @TypeOf(a); switch (@typeInfo(T)) { - .Struct => |info| { + .@"struct" => |info| { inline for (info.fields) |field_info| { if (!eql(@field(a, field_info.name), @field(b, field_info.name))) return false; } return true; }, - .ErrorUnion => { + .error_union => { if (a) |a_p| { if (b) |b_p| return eql(a_p, b_p) else |_| return false; } else |a_e| { if (b) |_| return false else |b_e| return a_e == b_e; } }, - .Union => |info| { + .@"union" => |info| { if (info.tag_type) |UnionTag| { const tag_a: UnionTag = a; const tag_b: UnionTag = b; @@ -768,26 +768,26 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool { @compileError("cannot compare untagged union type " ++ @typeName(T)); }, - .Array => { + .array => { if (a.len != b.len) return false; for (a, 0..) |e, i| if (!eql(e, b[i])) return false; return true; }, - .Vector => |info| { + .vector => |info| { var i: usize = 0; while (i < info.len) : (i += 1) { if (!eql(a[i], b[i])) return false; } return true; }, - .Pointer => |info| { + .pointer => |info| { return switch (info.size) { .One, .Many, .C => a == b, .Slice => a.ptr == b.ptr and a.len == b.len, }; }, - .Optional => { + .optional => { if (a == null and b == null) return true; if (a == null or b == null) return false; return eql(a.?, b.?); @@ -893,7 +893,7 @@ test intToEnum { pub const IntToEnumError = error{InvalidEnumTag}; pub fn intToEnum(comptime EnumTag: type, tag_int: anytype) IntToEnumError!EnumTag { - const enum_info = @typeInfo(EnumTag).Enum; + const enum_info = @typeInfo(EnumTag).@"enum"; if (!enum_info.is_exhaustive) { if (std.math.cast(enum_info.tag_type, tag_int)) |tag| { @@ -955,7 +955,7 @@ pub const IntType = @compileError("replaced by std.meta.Int"); pub fn Int(comptime signedness: std.builtin.Signedness, comptime bit_count: u16) type { return @Type(.{ - .Int = .{ + .int = .{ .signedness = signedness, .bits = bit_count, }, @@ -964,7 +964,7 @@ pub fn Int(comptime signedness: std.builtin.Signedness, comptime bit_count: u16) pub fn Float(comptime bit_count: u8) type { return @Type(.{ - .Float = .{ .bits = bit_count }, + .float = .{ .bits = bit_count }, }); } @@ -984,10 +984,10 @@ test Float { /// - `ArgsTuple(fn (a: u32, b: f16) noreturn)` ⇒ `tuple { u32, f16 }` pub fn ArgsTuple(comptime Function: type) type { const info = @typeInfo(Function); - if (info != .Fn) + if (info != .@"fn") @compileError("ArgsTuple expects a function type"); - const function_info = info.Fn; + const function_info = info.@"fn"; if (function_info.is_var_args) @compileError("Cannot create ArgsTuple for variadic function"); @@ -1026,7 +1026,7 @@ fn CreateUniqueTuple(comptime N: comptime_int, comptime types: [N]type) type { } return @Type(.{ - .Struct = .{ + .@"struct" = .{ .is_tuple = true, .layout = .auto, .decls = &.{}, @@ -1043,9 +1043,9 @@ const TupleTester = struct { fn assertTuple(comptime expected: anytype, comptime Actual: type) void { const info = @typeInfo(Actual); - if (info != .Struct) + if (info != .@"struct") @compileError("Expected struct type"); - if (!info.Struct.is_tuple) + if (!info.@"struct".is_tuple) @compileError("Struct type must be a tuple type"); const fields_list = std.meta.fields(Actual); @@ -1115,13 +1115,13 @@ test isError { /// `false` otherwise. Result is always comptime-known. pub inline fn hasFn(comptime T: type, comptime name: []const u8) bool { switch (@typeInfo(T)) { - .Struct, .Union, .Enum, .Opaque => {}, + .@"struct", .@"union", .@"enum", .@"opaque" => {}, else => return false, } if (!@hasDecl(T, name)) return false; - return @typeInfo(@TypeOf(@field(T, name))) == .Fn; + return @typeInfo(@TypeOf(@field(T, name))) == .@"fn"; } test hasFn { @@ -1144,7 +1144,7 @@ test hasFn { /// Result is always comptime-known. pub inline fn hasMethod(comptime T: type, comptime name: []const u8) bool { return switch (@typeInfo(T)) { - .Pointer => |P| switch (P.size) { + .pointer => |P| switch (P.size) { .One => hasFn(P.child, name), .Many, .Slice, .C => false, }, @@ -1191,29 +1191,29 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool { return switch (@typeInfo(T)) { else => false, // TODO can we know if it's true for some of these types ? - .AnyFrame, - .Enum, - .ErrorSet, - .Fn, + .@"anyframe", + .@"enum", + .error_set, + .@"fn", => true, - .Bool => false, + .bool => false, - .Int => |info| @sizeOf(T) * 8 == info.bits, + .int => |info| @sizeOf(T) * 8 == info.bits, - .Pointer => |info| info.size != .Slice, + .pointer => |info| info.size != .Slice, - .Optional => |info| switch (@typeInfo(info.child)) { - .Pointer => |ptr| !ptr.is_allowzero and switch (ptr.size) { + .optional => |info| switch (@typeInfo(info.child)) { + .pointer => |ptr| !ptr.is_allowzero and switch (ptr.size) { .Slice, .C => false, .One, .Many => true, }, else => false, }, - .Array => |info| hasUniqueRepresentation(info.child), + .array => |info| hasUniqueRepresentation(info.child), - .Struct => |info| { + .@"struct" => |info| { if (info.layout == .@"packed") return @sizeOf(T) * 8 == @bitSizeOf(T); var sum_size = @as(usize, 0); @@ -1226,7 +1226,7 @@ pub inline fn hasUniqueRepresentation(comptime T: type) bool { return @sizeOf(T) == sum_size; }, - .Vector => |info| hasUniqueRepresentation(info.child) and + .vector => |info| hasUniqueRepresentation(info.child) and @sizeOf(T) == @sizeOf(info.child) * info.len, }; } |
