Merge pull request #7750 from tadeokondrak/6609-tagtype-tag

Remove @TagType; std.meta.TagType -> std.meta.Tag
This commit is contained in:
Veikka Tuominen 2021-01-31 12:37:12 +02:00 committed by GitHub
commit fdc875ed00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 96 additions and 187 deletions

View File

@ -2909,15 +2909,15 @@ test "enum variant switch" {
expect(mem.eql(u8, what_is_it, "this is a number"));
}
// @TagType can be used to access the integer tag type of an enum.
// @typeInfo can be used to access the integer tag type of an enum.
const Small = enum {
one,
two,
three,
four,
};
test "@TagType" {
expect(@TagType(Small) == u2);
test "std.meta.Tag" {
expect(@typeInfo(Small).Enum.tag_type == u2);
}
// @typeInfo tells us the field count and the fields names:
@ -3092,8 +3092,7 @@ test "simple union" {
{#header_open|Tagged union#}
<p>Unions can be declared with an enum tag type.
This turns the union into a <em>tagged</em> union, which makes it eligible
to use with {#link|switch#} expressions. One can use {#link|@TagType#} to
obtain the enum type from the union type.
to use with {#link|switch#} expressions.
Tagged unions coerce to their tag type: {#link|Type Coercion: unions and enums#}.
</p>
{#code_begin|test#}
@ -3119,8 +3118,8 @@ test "switch on tagged union" {
}
}
test "@TagType" {
expect(@TagType(ComplexType) == ComplexTypeTag);
test "get tag type" {
expect(std.meta.Tag(ComplexType) == ComplexTypeTag);
}
test "coerce to enum" {
@ -7740,7 +7739,7 @@ test "@hasDecl" {
{#header_close#}
{#header_open|@intToEnum#}
<pre>{#syntax#}@intToEnum(comptime DestType: type, int_value: @TagType(DestType)) DestType{#endsyntax#}</pre>
<pre>{#syntax#}@intToEnum(comptime DestType: type, int_value: std.meta.Tag(DestType)) DestType{#endsyntax#}</pre>
<p>
Converts an integer into an {#link|enum#} value.
</p>
@ -8435,16 +8434,6 @@ fn doTheTest() void {
</p>
{#header_close#}
{#header_open|@TagType#}
<pre>{#syntax#}@TagType(T: type) type{#endsyntax#}</pre>
<p>
For an enum, returns the integer type that is used to store the enumeration value.
</p>
<p>
For a union, returns the enum type that is used to store the tag value.
</p>
{#header_close#}
{#header_open|@This#}
<pre>{#syntax#}@This() type{#endsyntax#}</pre>
<p>

View File

@ -175,7 +175,7 @@ pub const SourceLocation = struct {
column: u32,
};
pub const TypeId = @TagType(TypeInfo);
pub const TypeId = std.meta.Tag(TypeInfo);
/// This data structure is used by the Zig language code generation and
/// therefore must be kept in sync with the compiler implementation.

View File

@ -110,7 +110,7 @@ pub const Error = union(enum) {
pub const ExpectedToken = struct {
token: TokenIndex,
expected_id: @TagType(Token.Id),
expected_id: std.meta.Tag(Token.Id),
pub fn render(self: *const ExpectedToken, tree: *Tree, stream: anytype) !void {
const found_token = tree.tokens.at(self.token);

View File

@ -26,7 +26,7 @@ pub const Options = struct {
None,
/// Some warnings are errors
Some: []@TagType(ast.Error),
Some: []std.meta.Tag(ast.Error),
/// All warnings are errors
All,
@ -1363,7 +1363,7 @@ const Parser = struct {
return &node.base;
}
fn eatToken(parser: *Parser, id: @TagType(Token.Id)) ?TokenIndex {
fn eatToken(parser: *Parser, id: std.meta.Tag(Token.Id)) ?TokenIndex {
while (true) {
switch ((parser.it.next() orelse return null).id) {
.LineComment, .MultiLineComment, .Nl => continue,
@ -1377,7 +1377,7 @@ const Parser = struct {
}
}
fn expectToken(parser: *Parser, id: @TagType(Token.Id)) Error!TokenIndex {
fn expectToken(parser: *Parser, id: std.meta.Tag(Token.Id)) Error!TokenIndex {
while (true) {
switch ((parser.it.next() orelse return error.ParseError).id) {
.LineComment, .MultiLineComment, .Nl => continue,

View File

@ -131,7 +131,7 @@ pub const Token = struct {
Keyword_error,
Keyword_pragma,
pub fn symbol(id: @TagType(Id)) []const u8 {
pub fn symbol(id: std.meta.TagType(Id)) []const u8 {
return switch (id) {
.Invalid => "Invalid",
.Eof => "Eof",
@ -347,7 +347,7 @@ pub const Token = struct {
pub const Tokenizer = struct {
buffer: []const u8,
index: usize = 0,
prev_tok_id: @TagType(Token.Id) = .Invalid,
prev_tok_id: std.meta.TagType(Token.Id) = .Invalid,
pp_directive: bool = false,
pub fn next(self: *Tokenizer) Token {

View File

@ -239,7 +239,7 @@ fn testHashDeepRecursive(key: anytype) u64 {
test "typeContainsSlice" {
comptime {
testing.expect(!typeContainsSlice(@TagType(std.builtin.TypeInfo)));
testing.expect(!typeContainsSlice(meta.Tag(std.builtin.TypeInfo)));
testing.expect(typeContainsSlice([]const u8));
testing.expect(!typeContainsSlice(u8));

View File

@ -246,7 +246,7 @@ pub const StreamingParser = struct {
// Only call this function to generate array/object final state.
pub fn fromInt(x: anytype) State {
debug.assert(x == 0 or x == 1);
const T = @TagType(State);
const T = std.meta.Tag(State);
return @intToEnum(State, @intCast(T, x));
}
};
@ -1138,7 +1138,7 @@ pub const TokenStream = struct {
}
};
fn checkNext(p: *TokenStream, id: std.meta.TagType(Token)) void {
fn checkNext(p: *TokenStream, id: std.meta.Tag(Token)) void {
const token = (p.next() catch unreachable).?;
debug.assert(std.meta.activeTag(token) == id);
}
@ -1782,7 +1782,7 @@ test "parseFree descends into tagged union" {
};
// use a string with unicode escape so we know result can't be a reference to global constant
const r = try parse(T, &TokenStream.init("\"with\\u0105unicode\""), options);
testing.expectEqual(@TagType(T).string, @as(@TagType(T), r));
testing.expectEqual(std.meta.Tag(T).string, @as(std.meta.Tag(T), r));
testing.expectEqualSlices(u8, "withąunicode", r.string);
testing.expectEqual(@as(usize, 0), fail_alloc.deallocations);
parseFree(T, r, options);
@ -2077,7 +2077,7 @@ pub const Parser = struct {
}
}
fn parseString(p: *Parser, allocator: *Allocator, s: std.meta.TagPayloadType(Token, Token.String), input: []const u8, i: usize) !Value {
fn parseString(p: *Parser, allocator: *Allocator, s: std.meta.TagPayload(Token, Token.String), input: []const u8, i: usize) !Value {
const slice = s.slice(input, i);
switch (s.escapes) {
.None => return Value{ .String = if (p.copy_strings) try allocator.dupe(u8, slice) else slice },
@ -2090,7 +2090,7 @@ pub const Parser = struct {
}
}
fn parseNumber(p: *Parser, n: std.meta.TagPayloadType(Token, Token.Number), input: []const u8, i: usize) !Value {
fn parseNumber(p: *Parser, n: std.meta.TagPayload(Token, Token.Number), input: []const u8, i: usize) !Value {
return if (n.is_integer)
Value{ .Integer = try std.fmt.parseInt(i64, n.slice(input, i), 10) }
else

View File

@ -600,15 +600,18 @@ test "std.meta.FieldEnum" {
expectEqualEnum(enum { a, b, c }, FieldEnum(union { a: u8, b: void, c: f32 }));
}
pub fn TagType(comptime T: type) type {
// Deprecated: use Tag
pub const TagType = Tag;
pub fn Tag(comptime T: type) type {
return switch (@typeInfo(T)) {
.Enum => |info| info.tag_type,
.Union => |info| if (info.tag_type) |Tag| Tag else null,
.Union => |info| info.tag_type orelse @compileError(@typeName(T) ++ " has no tag type"),
else => @compileError("expected enum or union type, found '" ++ @typeName(T) ++ "'"),
};
}
test "std.meta.TagType" {
test "std.meta.Tag" {
const E = enum(u8) {
C = 33,
D,
@ -618,14 +621,14 @@ test "std.meta.TagType" {
D: u16,
};
testing.expect(TagType(E) == u8);
testing.expect(TagType(U) == E);
testing.expect(Tag(E) == u8);
testing.expect(Tag(U) == E);
}
///Returns the active tag of a tagged union
pub fn activeTag(u: anytype) @TagType(@TypeOf(u)) {
pub fn activeTag(u: anytype) Tag(@TypeOf(u)) {
const T = @TypeOf(u);
return @as(@TagType(T), u);
return @as(Tag(T), u);
}
test "std.meta.activeTag" {
@ -646,13 +649,15 @@ test "std.meta.activeTag" {
testing.expect(activeTag(u) == UE.Float);
}
const TagPayloadType = TagPayload;
///Given a tagged union type, and an enum, return the type of the union
/// field corresponding to the enum tag.
pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {
pub fn TagPayload(comptime U: type, tag: Tag(U)) type {
testing.expect(trait.is(.Union)(U));
const info = @typeInfo(U).Union;
const tag_info = @typeInfo(@TagType(U)).Enum;
const tag_info = @typeInfo(Tag(U)).Enum;
inline for (info.fields) |field_info| {
if (comptime mem.eql(u8, field_info.name, @tagName(tag)))
@ -662,14 +667,14 @@ pub fn TagPayloadType(comptime U: type, tag: @TagType(U)) type {
unreachable;
}
test "std.meta.TagPayloadType" {
test "std.meta.TagPayload" {
const Event = union(enum) {
Moved: struct {
from: i32,
to: i32,
},
};
const MovedEvent = TagPayloadType(Event, Event.Moved);
const MovedEvent = TagPayload(Event, Event.Moved);
var e: Event = undefined;
testing.expect(MovedEvent == @TypeOf(e.Moved));
}
@ -694,13 +699,13 @@ pub fn eql(a: anytype, b: @TypeOf(a)) bool {
}
},
.Union => |info| {
if (info.tag_type) |Tag| {
if (info.tag_type) |UnionTag| {
const tag_a = activeTag(a);
const tag_b = activeTag(b);
if (tag_a != tag_b) return false;
inline for (info.fields) |field_info| {
if (@field(Tag, field_info.name) == tag_a) {
if (@field(UnionTag, field_info.name) == tag_a) {
return eql(@field(a, field_info.name), @field(b, field_info.name));
}
}
@ -822,9 +827,9 @@ test "intToEnum with error return" {
pub const IntToEnumError = error{InvalidEnumTag};
pub fn intToEnum(comptime Tag: type, tag_int: anytype) IntToEnumError!Tag {
inline for (@typeInfo(Tag).Enum.fields) |f| {
const this_tag_value = @field(Tag, f.name);
pub fn intToEnum(comptime EnumTag: type, tag_int: anytype) IntToEnumError!EnumTag {
inline for (@typeInfo(EnumTag).Enum.fields) |f| {
const this_tag_value = @field(EnumTag, f.name);
if (tag_int == @enumToInt(this_tag_value)) {
return this_tag_value;
}

View File

@ -146,7 +146,7 @@ test "TrailerFlags" {
b: bool,
c: u64,
});
testing.expectEqual(u2, @TagType(Flags.FieldEnum));
testing.expectEqual(u2, meta.Tag(Flags.FieldEnum));
var flags = Flags.init(.{
.b = true,

View File

@ -119,10 +119,10 @@ pub fn expectEqual(expected: anytype, actual: @TypeOf(expected)) void {
@compileError("Unable to compare untagged union values");
}
const TagType = @TagType(@TypeOf(expected));
const Tag = std.meta.Tag(@TypeOf(expected));
const expectedTag = @as(TagType, expected);
const actualTag = @as(TagType, actual);
const expectedTag = @as(Tag, expected);
const actualTag = @as(Tag, actual);
expectEqual(expectedTag, actualTag);

View File

@ -3822,7 +3822,7 @@ fn testCanonical(source: []const u8) !void {
return testTransform(source, source);
}
const Error = @TagType(std.zig.ast.Error);
const Error = std.meta.Tag(std.zig.ast.Error);
fn testError(source: []const u8, expected_errors: []const Error) !void {
const tree = try std.zig.parse(std.testing.allocator, source);

View File

@ -266,11 +266,11 @@ pub fn next(self: *Tokenizer) ?Token {
unreachable;
}
fn errorPosition(comptime id: @TagType(Token), index: usize, bytes: []const u8) Token {
fn errorPosition(comptime id: std.meta.Tag(Token), index: usize, bytes: []const u8) Token {
return @unionInit(Token, @tagName(id), .{ .index = index, .bytes = bytes });
}
fn errorIllegalChar(comptime id: @TagType(Token), index: usize, char: u8) Token {
fn errorIllegalChar(comptime id: std.meta.Tag(Token), index: usize, char: u8) Token {
return @unionInit(Token, @tagName(id), .{ .index = index, .char = char });
}

View File

@ -3077,7 +3077,6 @@ fn nodeMayNeedMemoryLocation(start_node: *ast.Node, scope: *Scope) bool {
.{ "@round", false },
.{ "@subWithOverflow", false },
.{ "@tagName", false },
.{ "@TagType", false },
.{ "@This", false },
.{ "@truncate", false },
.{ "@Type", false },

View File

@ -140,7 +140,7 @@ pub const LoadCommand = union(enum) {
}
fn eql(self: LoadCommand, other: LoadCommand) bool {
if (@as(@TagType(LoadCommand), self) != @as(@TagType(LoadCommand), other)) return false;
if (@as(meta.Tag(LoadCommand), self) != @as(meta.Tag(LoadCommand), other)) return false;
return switch (self) {
.DyldInfoOnly => |x| meta.eql(x, other.DyldInfoOnly),
.Symtab => |x| meta.eql(x, other.Symtab),

View File

@ -1811,7 +1811,6 @@ enum BuiltinFnId {
BuiltinFnIdIntToPtr,
BuiltinFnIdPtrToInt,
BuiltinFnIdTagName,
BuiltinFnIdTagType,
BuiltinFnIdFieldParentPtr,
BuiltinFnIdByteOffsetOf,
BuiltinFnIdBitOffsetOf,
@ -2623,7 +2622,6 @@ enum IrInstSrcId {
IrInstSrcIdDeclRef,
IrInstSrcIdPanic,
IrInstSrcIdTagName,
IrInstSrcIdTagType,
IrInstSrcIdFieldParentPtr,
IrInstSrcIdByteOffsetOf,
IrInstSrcIdBitOffsetOf,
@ -4074,12 +4072,6 @@ struct IrInstGenTagName {
IrInstGen *target;
};
struct IrInstSrcTagType {
IrInstSrc base;
IrInstSrc *target;
};
struct IrInstSrcFieldParentPtr {
IrInstSrc base;

View File

@ -3267,7 +3267,7 @@ static Error resolve_union_zero_bits(CodeGen *g, ZigType *union_type) {
tag_type = new_type_table_entry(ZigTypeIdEnum);
buf_resize(&tag_type->name, 0);
buf_appendf(&tag_type->name, "@TagType(%s)", buf_ptr(&union_type->name));
buf_appendf(&tag_type->name, "@typeInfo(%s).Union.tag_type.?", buf_ptr(&union_type->name));
tag_type->llvm_type = tag_int_type->llvm_type;
tag_type->llvm_di_type = tag_int_type->llvm_di_type;
tag_type->abi_size = tag_int_type->abi_size;

View File

@ -8842,7 +8842,6 @@ static void define_builtin_fns(CodeGen *g) {
create_builtin_fn(g, BuiltinFnIdIntToPtr, "intToPtr", 2);
create_builtin_fn(g, BuiltinFnIdPtrToInt, "ptrToInt", 1);
create_builtin_fn(g, BuiltinFnIdTagName, "tagName", 1);
create_builtin_fn(g, BuiltinFnIdTagType, "TagType", 1);
create_builtin_fn(g, BuiltinFnIdFieldParentPtr, "fieldParentPtr", 3);
create_builtin_fn(g, BuiltinFnIdByteOffsetOf, "byteOffsetOf", 2);
create_builtin_fn(g, BuiltinFnIdBitOffsetOf, "bitOffsetOf", 2);

View File

@ -516,8 +516,6 @@ static void destroy_instruction_src(IrInstSrc *inst) {
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcSetAlignStack *>(inst));
case IrInstSrcIdArgType:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcArgType *>(inst));
case IrInstSrcIdTagType:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcTagType *>(inst));
case IrInstSrcIdExport:
return heap::c_allocator.destroy(reinterpret_cast<IrInstSrcExport *>(inst));
case IrInstSrcIdExtern:
@ -1496,10 +1494,6 @@ static constexpr IrInstSrcId ir_inst_id(IrInstSrcTagName *) {
return IrInstSrcIdTagName;
}
static constexpr IrInstSrcId ir_inst_id(IrInstSrcTagType *) {
return IrInstSrcIdTagType;
}
static constexpr IrInstSrcId ir_inst_id(IrInstSrcFieldParentPtr *) {
return IrInstSrcIdFieldParentPtr;
}
@ -4450,17 +4444,6 @@ static IrInstGen *ir_build_tag_name_gen(IrAnalyze *ira, IrInst *source_instr, Ir
return &instruction->base;
}
static IrInstSrc *ir_build_tag_type(IrBuilderSrc *irb, Scope *scope, AstNode *source_node,
IrInstSrc *target)
{
IrInstSrcTagType *instruction = ir_build_instruction<IrInstSrcTagType>(irb, scope, source_node);
instruction->target = target;
ir_ref_instruction(target, irb->current_basic_block);
return &instruction->base;
}
static IrInstSrc *ir_build_field_parent_ptr_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node,
IrInstSrc *type_value, IrInstSrc *field_name, IrInstSrc *field_ptr)
{
@ -7202,16 +7185,6 @@ static IrInstSrc *ir_gen_builtin_fn_call(IrBuilderSrc *irb, Scope *scope, AstNod
IrInstSrc *tag_name = ir_build_tag_name_src(irb, scope, node, arg0_value);
return ir_lval_wrap(irb, scope, tag_name, lval, result_loc);
}
case BuiltinFnIdTagType:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
IrInstSrc *arg0_value = ir_gen_node(irb, arg0_node, scope);
if (arg0_value == irb->codegen->invalid_inst_src)
return arg0_value;
IrInstSrc *tag_type = ir_build_tag_type(irb, scope, node, arg0_value);
return ir_lval_wrap(irb, scope, tag_type, lval, result_loc);
}
case BuiltinFnIdFieldParentPtr:
{
AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
@ -31051,30 +31024,6 @@ static IrInstGen *ir_analyze_instruction_arg_type(IrAnalyze *ira, IrInstSrcArgTy
return ir_const_type(ira, &instruction->base.base, result_type);
}
static IrInstGen *ir_analyze_instruction_tag_type(IrAnalyze *ira, IrInstSrcTagType *instruction) {
Error err;
IrInstGen *target_inst = instruction->target->child;
ZigType *enum_type = ir_resolve_type(ira, target_inst);
if (type_is_invalid(enum_type))
return ira->codegen->invalid_inst_gen;
if (enum_type->id == ZigTypeIdEnum) {
if ((err = type_resolve(ira->codegen, enum_type, ResolveStatusSizeKnown)))
return ira->codegen->invalid_inst_gen;
return ir_const_type(ira, &instruction->base.base, enum_type->data.enumeration.tag_int_type);
} else if (enum_type->id == ZigTypeIdUnion) {
ZigType *tag_type = ir_resolve_union_tag_type(ira, instruction->target->base.source_node, enum_type);
if (type_is_invalid(tag_type))
return ira->codegen->invalid_inst_gen;
return ir_const_type(ira, &instruction->base.base, tag_type);
} else {
ir_add_error(ira, &target_inst->base, buf_sprintf("expected enum or union, found '%s'",
buf_ptr(&enum_type->name)));
return ira->codegen->invalid_inst_gen;
}
}
static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstGen *op) {
ZigType *operand_type = ir_resolve_type(ira, op);
if (type_is_invalid(operand_type))
@ -32435,8 +32384,6 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
return ir_analyze_instruction_set_align_stack(ira, (IrInstSrcSetAlignStack *)instruction);
case IrInstSrcIdArgType:
return ir_analyze_instruction_arg_type(ira, (IrInstSrcArgType *)instruction);
case IrInstSrcIdTagType:
return ir_analyze_instruction_tag_type(ira, (IrInstSrcTagType *)instruction);
case IrInstSrcIdExport:
return ir_analyze_instruction_export(ira, (IrInstSrcExport *)instruction);
case IrInstSrcIdExtern:
@ -32879,7 +32826,6 @@ bool ir_inst_src_has_side_effects(IrInstSrc *instruction) {
case IrInstSrcIdImplicitCast:
case IrInstSrcIdResolveResult:
case IrInstSrcIdArgType:
case IrInstSrcIdTagType:
case IrInstSrcIdErrorReturnTrace:
case IrInstSrcIdErrorUnion:
case IrInstSrcIdFloatOp:

View File

@ -282,8 +282,6 @@ const char* ir_inst_src_type_str(IrInstSrcId id) {
return "SrcPanic";
case IrInstSrcIdTagName:
return "SrcTagName";
case IrInstSrcIdTagType:
return "SrcTagType";
case IrInstSrcIdFieldParentPtr:
return "SrcFieldParentPtr";
case IrInstSrcIdByteOffsetOf:
@ -2354,12 +2352,6 @@ static void ir_print_arg_type(IrPrintSrc *irp, IrInstSrcArgType *instruction) {
fprintf(irp->f, ")");
}
static void ir_print_enum_tag_type(IrPrintSrc *irp, IrInstSrcTagType *instruction) {
fprintf(irp->f, "@TagType(");
ir_print_other_inst_src(irp, instruction->target);
fprintf(irp->f, ")");
}
static void ir_print_export(IrPrintSrc *irp, IrInstSrcExport *instruction) {
fprintf(irp->f, "@export(");
ir_print_other_inst_src(irp, instruction->target);
@ -2953,9 +2945,6 @@ static void ir_print_inst_src(IrPrintSrc *irp, IrInstSrc *instruction, bool trai
case IrInstSrcIdArgType:
ir_print_arg_type(irp, (IrInstSrcArgType *)instruction);
break;
case IrInstSrcIdTagType:
ir_print_enum_tag_type(irp, (IrInstSrcTagType *)instruction);
break;
case IrInstSrcIdExport:
ir_print_export(irp, (IrInstSrcExport *)instruction);
break;

View File

@ -750,7 +750,7 @@ pub const TestContext = struct {
for (actual_errors.list) |actual_error| {
for (case_error_list) |case_msg, i| {
const ex_tag: @TagType(@TypeOf(case_msg)) = case_msg;
const ex_tag: std.meta.Tag(@TypeOf(case_msg)) = case_msg;
switch (actual_error) {
.src => |actual_msg| {
for (actual_msg.notes) |*note| {
@ -789,7 +789,7 @@ pub const TestContext = struct {
}
while (notes_to_check.popOrNull()) |note| {
for (case_error_list) |case_msg, i| {
const ex_tag: @TagType(@TypeOf(case_msg)) = case_msg;
const ex_tag: std.meta.Tag(@TypeOf(case_msg)) = case_msg;
switch (note.*) {
.src => |actual_msg| {
for (actual_msg.notes) |*sub_note| {

View File

@ -3288,7 +3288,7 @@ const ClangFunctionType = union(enum) {
NoProto: *const clang.FunctionType,
fn getReturnType(self: @This()) clang.QualType {
switch (@as(@TagType(@This()), self)) {
switch (@as(std.meta.Tag(@This()), self)) {
.Proto => return self.Proto.getReturnType(),
.NoProto => return self.NoProto.getReturnType(),
}

View File

@ -110,7 +110,7 @@ pub const Type = extern union {
pub fn tag(self: Type) Tag {
if (self.tag_if_small_enough < Tag.no_payload_count) {
return @intToEnum(Tag, @intCast(@TagType(Tag), self.tag_if_small_enough));
return @intToEnum(Tag, @intCast(std.meta.Tag(Tag), self.tag_if_small_enough));
} else {
return self.ptr_otherwise.tag;
}

View File

@ -223,7 +223,7 @@ pub const Value = extern union {
pub fn tag(self: Value) Tag {
if (self.tag_if_small_enough < Tag.no_payload_count) {
return @intToEnum(Tag, @intCast(@TagType(Tag), self.tag_if_small_enough));
return @intToEnum(Tag, @intCast(std.meta.Tag(Tag), self.tag_if_small_enough));
} else {
return self.ptr_otherwise.tag;
}

View File

@ -323,7 +323,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ e: E,
\\};
\\export fn entry() void {
\\ if (@TagType(E) != u8) @compileError("did not infer u8 tag type");
\\ if (@typeInfo(E).Enum.tag_type != u8) @compileError("did not infer u8 tag type");
\\ const s: S = undefined;
\\}
, &[_][]const u8{
@ -2728,7 +2728,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\const InvalidToken = struct {};
\\const ExpectedVarDeclOrFn = struct {};
, &[_][]const u8{
"tmp.zig:4:9: error: expected type '@TagType(Error)', found 'type'",
"tmp.zig:4:9: error: expected type '@typeInfo(Error).Union.tag_type.?', found 'type'",
});
cases.addTest("binary OR operator on error sets",
@ -7462,24 +7462,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
"tmp.zig:4:5: note: declared here",
});
cases.add("@TagType when union has no attached enum",
\\const Foo = union {
\\ A: i32,
\\};
\\export fn entry() void {
\\ const x = @TagType(Foo);
\\}
, &[_][]const u8{
"tmp.zig:5:24: error: union 'Foo' has no tag",
"tmp.zig:1:13: note: consider 'union(enum)' here",
});
cases.add("non-integer tag type to automatic union enum",
\\const Foo = union(enum(f32)) {
\\ A: i32,
\\};
\\export fn entry() void {
\\ const x = @TagType(Foo);
\\ const x = @typeInfo(Foo).Union.tag_type.?;
\\}
, &[_][]const u8{
"tmp.zig:1:24: error: expected integer tag type, found 'f32'",
@ -7490,7 +7478,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
\\ A: i32,
\\};
\\export fn entry() void {
\\ const x = @TagType(Foo);
\\ const x = @typeInfo(Foo).Union.tag_type.?;
\\}
, &[_][]const u8{
"tmp.zig:1:19: error: expected enum tag type, found 'u32'",

View File

@ -74,7 +74,7 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\pub fn main() void {
\\ var u: U = undefined;
\\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
\\ var t: @TagType(U) = u;
\\ var t: @typeInfo(U).Union.tag_type.? = u;
\\ var n = @tagName(t);
\\}
);

View File

@ -13,7 +13,7 @@ const C = struct {};
test "tagged union with all void fields but a meaningful tag" {
var a: A = A{ .b = B{ .c = C{} } };
std.testing.expect(@as(@TagType(B), a.b) == @TagType(B).c);
std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).c);
a = A{ .b = B.None };
std.testing.expect(@as(@TagType(B), a.b) == @TagType(B).None);
std.testing.expect(@as(std.meta.Tag(B), a.b) == std.meta.Tag(B).None);
}

View File

@ -1,5 +1,6 @@
const expect = @import("std").testing.expect;
const mem = @import("std").mem;
const Tag = @import("std").meta.Tag;
test "extern enum" {
const S = struct {
@ -827,12 +828,12 @@ test "set enum tag type" {
{
var x = Small.One;
x = Small.Two;
comptime expect(@TagType(Small) == u2);
comptime expect(Tag(Small) == u2);
}
{
var x = Small2.One;
x = Small2.Two;
comptime expect(@TagType(Small2) == u2);
comptime expect(Tag(Small2) == u2);
}
}
@ -905,11 +906,11 @@ fn getC(data: *const BitFieldOfEnums) C {
}
test "casting enum to its tag type" {
testCastEnumToTagType(Small2.Two);
comptime testCastEnumToTagType(Small2.Two);
testCastEnumTag(Small2.Two);
comptime testCastEnumTag(Small2.Two);
}
fn testCastEnumToTagType(value: Small2) void {
fn testCastEnumTag(value: Small2) void {
expect(@enumToInt(value) == 1);
}
@ -1163,14 +1164,14 @@ test "enum with comptime_int tag type" {
Two = 2,
Three = 1,
};
comptime expect(@TagType(Enum) == comptime_int);
comptime expect(Tag(Enum) == comptime_int);
}
test "enum with one member default to u0 tag type" {
const E0 = enum {
X,
};
comptime expect(@TagType(E0) == u0);
comptime expect(Tag(E0) == u0);
}
test "tagName on enum literals" {

View File

@ -14,7 +14,7 @@ test "type info: tag type, void info" {
}
fn testBasic() void {
expect(@TagType(TypeInfo) == TypeId);
expect(@typeInfo(TypeInfo).Union.tag_type == TypeId);
const void_info = @typeInfo(void);
expect(void_info == TypeId.Void);
expect(void_info.Void == {});

View File

@ -1,6 +1,7 @@
const std = @import("std");
const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const Tag = std.meta.Tag;
const Value = union(enum) {
Int: u64,
@ -128,7 +129,7 @@ const MultipleChoice = union(enum(u32)) {
test "simple union(enum(u32))" {
var x = MultipleChoice.C;
expect(x == MultipleChoice.C);
expect(@enumToInt(@as(@TagType(MultipleChoice), x)) == 60);
expect(@enumToInt(@as(Tag(MultipleChoice), x)) == 60);
}
const MultipleChoice2 = union(enum(u32)) {
@ -144,13 +145,13 @@ const MultipleChoice2 = union(enum(u32)) {
};
test "union(enum(u32)) with specified and unspecified tag values" {
comptime expect(@TagType(@TagType(MultipleChoice2)) == u32);
comptime expect(Tag(Tag(MultipleChoice2)) == u32);
testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 });
}
fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void {
expect(@enumToInt(@as(@TagType(MultipleChoice2), x)) == 60);
expect(@enumToInt(@as(Tag(MultipleChoice2), x)) == 60);
expect(1123 == switch (x) {
MultipleChoice2.A => 1,
MultipleChoice2.B => 2,
@ -204,11 +205,11 @@ test "union field access gives the enum values" {
}
test "cast union to tag type of union" {
testCastUnionToTagType(TheUnion{ .B = 1234 });
comptime testCastUnionToTagType(TheUnion{ .B = 1234 });
testCastUnionToTag(TheUnion{ .B = 1234 });
comptime testCastUnionToTag(TheUnion{ .B = 1234 });
}
fn testCastUnionToTagType(x: TheUnion) void {
fn testCastUnionToTag(x: TheUnion) void {
expect(@as(TheTag, x) == TheTag.B);
}
@ -298,7 +299,7 @@ const TaggedUnionWithAVoid = union(enum) {
fn testTaggedUnionInit(x: anytype) bool {
const y = TaggedUnionWithAVoid{ .A = x };
return @as(@TagType(TaggedUnionWithAVoid), y) == TaggedUnionWithAVoid.A;
return @as(Tag(TaggedUnionWithAVoid), y) == TaggedUnionWithAVoid.A;
}
pub const UnionEnumNoPayloads = union(enum) {
@ -309,8 +310,8 @@ pub const UnionEnumNoPayloads = union(enum) {
test "tagged union with no payloads" {
const a = UnionEnumNoPayloads{ .B = {} };
switch (a) {
@TagType(UnionEnumNoPayloads).A => @panic("wrong"),
@TagType(UnionEnumNoPayloads).B => {},
Tag(UnionEnumNoPayloads).A => @panic("wrong"),
Tag(UnionEnumNoPayloads).B => {},
}
}
@ -325,9 +326,9 @@ test "union with only 1 field casted to its enum type" {
};
var e = Expr{ .Literal = Literal{ .Bool = true } };
const Tag = @TagType(Expr);
comptime expect(@TagType(Tag) == u0);
var t = @as(Tag, e);
const ExprTag = Tag(Expr);
comptime expect(Tag(ExprTag) == u0);
var t = @as(ExprTag, e);
expect(t == Expr.Literal);
}
@ -337,17 +338,17 @@ test "union with only 1 field casted to its enum type which has enum value speci
Bool: bool,
};
const Tag = enum(comptime_int) {
const ExprTag = enum(comptime_int) {
Literal = 33,
};
const Expr = union(Tag) {
const Expr = union(ExprTag) {
Literal: Literal,
};
var e = Expr{ .Literal = Literal{ .Bool = true } };
comptime expect(@TagType(Tag) == comptime_int);
var t = @as(Tag, e);
comptime expect(Tag(ExprTag) == comptime_int);
var t = @as(ExprTag, e);
expect(t == Expr.Literal);
expect(@enumToInt(t) == 33);
comptime expect(@enumToInt(t) == 33);
@ -501,7 +502,7 @@ test "union with one member defaults to u0 tag type" {
const U0 = union(enum) {
X: u32,
};
comptime expect(@TagType(@TagType(U0)) == u0);
comptime expect(Tag(Tag(U0)) == u0);
}
test "union with comptime_int tag" {
@ -510,7 +511,7 @@ test "union with comptime_int tag" {
Y: u16,
Z: u8,
};
comptime expect(@TagType(@TagType(Union)) == comptime_int);
comptime expect(Tag(Tag(Union)) == comptime_int);
}
test "extern union doesn't trigger field check at comptime" {
@ -591,7 +592,7 @@ test "function call result coerces from tagged union to the tag" {
Two: usize,
};
const ArchTag = @TagType(Arch);
const ArchTag = Tag(Arch);
fn doTheTest() void {
var x: ArchTag = getArch1();
@ -696,8 +697,8 @@ test "cast from pointer to anonymous struct to pointer to union" {
test "method call on an empty union" {
const S = struct {
const MyUnion = union(Tag) {
pub const Tag = enum { X1, X2 };
const MyUnion = union(MyUnionTag) {
pub const MyUnionTag = enum { X1, X2 };
X1: [0]u8,
X2: [0]u8,
@ -797,7 +798,7 @@ test "union enum type gets a separate scope" {
};
fn doTheTest() void {
expect(!@hasDecl(@TagType(U), "foo"));
expect(!@hasDecl(Tag(U), "foo"));
}
};

View File

@ -499,7 +499,7 @@ pub fn addPkgTests(
if (skip_single_threaded and test_target.single_threaded)
continue;
const ArchTag = @TagType(builtin.Arch);
const ArchTag = std.meta.Tag(builtin.Arch);
if (test_target.disable_native and
test_target.target.getOsTag() == std.Target.current.os.tag and
test_target.target.getCpuArch() == std.Target.current.cpu.arch)

View File

@ -47,7 +47,7 @@ const MultiAbi = union(enum) {
fn eql(a: MultiAbi, b: MultiAbi) bool {
if (@enumToInt(a) != @enumToInt(b))
return false;
if (@TagType(MultiAbi)(a) != .specific)
if (std.meta.Tag(MultiAbi)(a) != .specific)
return true;
return a.specific == b.specific;
}