authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-14 11:55:29-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-03-14 11:57:56-04:00
log080dd27157e79adc8e5d5b7c1dcf94919624abab
tree108c39a7f852c0b606bf2e8b2f6a6fecb7a8c2ee
parente861da03f9b9d1261cf32872ea942ee7a63812d3
signaturelock-open Commit is signed but in an unrecognized format.

breaking: fix @typeInfo handling of global error set type

`builtin.TypeInfo.ErrorSet` is now `?[]Error` instead of `struct{errors:[]Error}`. closes #1936

7 files changed, 17 insertions(+), 49 deletions(-)

doc/docgen.zig+1-1
...@@ -707,7 +707,7 @@ const builtin_types = [][]const u8{...@@ -707,7 +707,7 @@ const builtin_types = [][]const u8{
707 "f16", "f32", "f64", "f128", "c_longdouble", "c_short",707 "f16", "f32", "f64", "f128", "c_longdouble", "c_short",
708 "c_ushort", "c_int", "c_uint", "c_long", "c_ulong", "c_longlong",708 "c_ushort", "c_int", "c_uint", "c_long", "c_ulong", "c_longlong",
709 "c_ulonglong", "c_char", "c_void", "void", "bool", "isize",709 "c_ulonglong", "c_char", "c_void", "void", "bool", "isize",
710 "usize", "noreturn", "type", "error", "comptime_int", "comptime_float",710 "usize", "noreturn", "type", "anyerror", "comptime_int", "comptime_float",
711};711};
712712
713fn isType(name: []const u8) bool {713fn isType(name: []const u8) bool {
src/codegen.cpp+1-3
...@@ -7581,9 +7581,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {...@@ -7581,9 +7581,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
7581 " value: comptime_int,\n"7581 " value: comptime_int,\n"
7582 " };\n"7582 " };\n"
7583 "\n"7583 "\n"
7584 " pub const ErrorSet = struct {\n"7584 " pub const ErrorSet = ?[]Error;\n"
7585 " errors: []Error,\n"
7586 " };\n"
7587 "\n"7585 "\n"
7588 " pub const EnumField = struct {\n"7586 " pub const EnumField = struct {\n"
7589 " name: []const u8,\n"7587 " name: []const u8,\n"
src/ir.cpp+5-10
...@@ -18265,21 +18265,16 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18265,21 +18265,16 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18265 result->special = ConstValSpecialStatic;18265 result->special = ConstValSpecialStatic;
18266 result->type = ir_type_info_get_type(ira, "ErrorSet", nullptr);18266 result->type = ir_type_info_get_type(ira, "ErrorSet", nullptr);
1826718267
18268 ConstExprValue *fields = create_const_vals(1);
18269 result->data.x_struct.fields = fields;
18270
18271 // errors: []TypeInfo.Error
18272 ensure_field_index(result->type, "errors", 0);
18273
18274 ZigType *type_info_error_type = ir_type_info_get_type(ira, "Error", nullptr);18268 ZigType *type_info_error_type = ir_type_info_get_type(ira, "Error", nullptr);
18275 if (!resolve_inferred_error_set(ira->codegen, type_entry, source_instr->source_node)) {18269 if (!resolve_inferred_error_set(ira->codegen, type_entry, source_instr->source_node)) {
18276 return ErrorSemanticAnalyzeFail;18270 return ErrorSemanticAnalyzeFail;
18277 }18271 }
18278 if (type_is_global_error_set(type_entry)) {18272 if (type_is_global_error_set(type_entry)) {
18279 ir_add_error(ira, source_instr,18273 result->data.x_optional = nullptr;
18280 buf_sprintf("TODO: compiler bug: implement @typeInfo support for anyerror. https://github.com/ziglang/zig/issues/1936"));18274 break;
18281 return ErrorSemanticAnalyzeFail;
18282 }18275 }
18276 ConstExprValue *slice_val = create_const_vals(1);
18277 result->data.x_optional = slice_val;
1828318278
18284 uint32_t error_count = type_entry->data.error_set.err_count;18279 uint32_t error_count = type_entry->data.error_set.err_count;
18285 ConstExprValue *error_array = create_const_vals(1);18280 ConstExprValue *error_array = create_const_vals(1);
...@@ -18288,7 +18283,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr...@@ -18288,7 +18283,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, IrInstruction *source_instr
18288 error_array->data.x_array.special = ConstArraySpecialNone;18283 error_array->data.x_array.special = ConstArraySpecialNone;
18289 error_array->data.x_array.data.s_none.elements = create_const_vals(error_count);18284 error_array->data.x_array.data.s_none.elements = create_const_vals(error_count);
1829018285
18291 init_const_slice(ira->codegen, &fields[0], error_array, 0, error_count, false);18286 init_const_slice(ira->codegen, slice_val, error_array, 0, error_count, false);
18292 for (uint32_t error_index = 0; error_index < error_count; error_index++) {18287 for (uint32_t error_index = 0; error_index < error_count; error_index++) {
18293 ErrorTableEntry *error = type_entry->data.error_set.errors[error_index];18288 ErrorTableEntry *error = type_entry->data.error_set.errors[error_index];
18294 ConstExprValue *error_val = &error_array->data.x_array.data.s_none.elements[error_index];18289 ConstExprValue *error_val = &error_array->data.x_array.data.s_none.elements[error_index];
std/fmt.zig-4
...@@ -112,10 +112,6 @@ pub fn formatType(...@@ -112,10 +112,6 @@ pub fn formatType(
112 output: fn (@typeOf(context), []const u8) Errors!void,112 output: fn (@typeOf(context), []const u8) Errors!void,
113) Errors!void {113) Errors!void {
114 const T = @typeOf(value);114 const T = @typeOf(value);
115 if (T == anyerror) {
116 try output(context, "error.");
117 return output(context, @errorName(value));
118 }
119 switch (@typeInfo(T)) {115 switch (@typeInfo(T)) {
120 builtin.TypeId.ComptimeInt, builtin.TypeId.Int, builtin.TypeId.Float => {116 builtin.TypeId.ComptimeInt, builtin.TypeId.Int, builtin.TypeId.Float => {
121 return formatValue(value, fmt, context, Errors, output);117 return formatValue(value, fmt, context, Errors, output);
std/meta.zig+3-27
...@@ -13,32 +13,8 @@ const TypeInfo = builtin.TypeInfo;...@@ -13,32 +13,8 @@ const TypeInfo = builtin.TypeInfo;
13pub fn tagName(v: var) []const u8 {13pub fn tagName(v: var) []const u8 {
14 const T = @typeOf(v);14 const T = @typeOf(v);
15 switch (@typeInfo(T)) {15 switch (@typeInfo(T)) {
16 TypeId.Enum => |info| {16 TypeId.ErrorSet => return @errorName(v),
17 const Tag = info.tag_type;17 else => return @tagName(v),
18 inline for (info.fields) |field| {
19 if (field.value == @enumToInt(v)) return field.name;
20 }
21
22 unreachable;
23 },
24 TypeId.Union => |info| {
25 const UnionTag = if (info.tag_type) |UT| UT else @compileError("union is untagged");
26 const Tag = @typeInfo(UnionTag).Enum.tag_type;
27 inline for (info.fields) |field| {
28 if (field.enum_field.?.value == @enumToInt(UnionTag(v)))
29 return field.name;
30 }
31
32 unreachable;
33 },
34 TypeId.ErrorSet => |info| {
35 inline for (info.errors) |err| {
36 if (err.value == @errorToInt(v)) return err.name;
37 }
38
39 unreachable;
40 },
41 else => @compileError("expected enum, error set or union type, found '" ++ @typeName(T) ++ "'"),
42 }18 }
43}19}
4420
...@@ -267,7 +243,7 @@ pub fn fields(comptime T: type) switch (@typeInfo(T)) {...@@ -267,7 +243,7 @@ pub fn fields(comptime T: type) switch (@typeInfo(T)) {
267 TypeId.Struct => |info| info.fields,243 TypeId.Struct => |info| info.fields,
268 TypeId.Union => |info| info.fields,244 TypeId.Union => |info| info.fields,
269 TypeId.Enum => |info| info.fields,245 TypeId.Enum => |info| info.fields,
270 TypeId.ErrorSet => |info| info.errors,246 TypeId.ErrorSet => |errors| errors.?, // must be non global error set
271 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),247 else => @compileError("Expected struct, union, error set or enum type, found '" ++ @typeName(T) ++ "'"),
272 };248 };
273}249}
std/testing.zig-1
...@@ -5,7 +5,6 @@ const std = @import("std.zig");...@@ -5,7 +5,6 @@ const std = @import("std.zig");
5/// This function is intended to be used only in tests. It prints diagnostics to stderr5/// This function is intended to be used only in tests. It prints diagnostics to stderr
6/// and then aborts when actual_error_union is not expected_error.6/// and then aborts when actual_error_union is not expected_error.
7pub fn expectError(expected_error: anyerror, actual_error_union: var) void {7pub fn expectError(expected_error: anyerror, actual_error_union: var) void {
8 // TODO remove the workaround here for https://github.com/ziglang/zig/issues/1936
9 if (actual_error_union) |actual_payload| {8 if (actual_error_union) |actual_payload| {
10 // TODO remove workaround here for https://github.com/ziglang/zig/issues/5579 // TODO remove workaround here for https://github.com/ziglang/zig/issues/557
11 if (@sizeOf(@typeOf(actual_payload)) == 0) {10 if (@sizeOf(@typeOf(actual_payload)) == 0) {
test/stage1/behavior/type_info.zig+7-3
...@@ -143,14 +143,18 @@ fn testErrorSet() void {...@@ -143,14 +143,18 @@ fn testErrorSet() void {
143143
144 const error_set_info = @typeInfo(TestErrorSet);144 const error_set_info = @typeInfo(TestErrorSet);
145 expect(TypeId(error_set_info) == TypeId.ErrorSet);145 expect(TypeId(error_set_info) == TypeId.ErrorSet);
146 expect(error_set_info.ErrorSet.errors.len == 3);146 expect(error_set_info.ErrorSet.?.len == 3);
147 expect(mem.eql(u8, error_set_info.ErrorSet.errors[0].name, "First"));147 expect(mem.eql(u8, error_set_info.ErrorSet.?[0].name, "First"));
148 expect(error_set_info.ErrorSet.errors[2].value == @errorToInt(TestErrorSet.Third));148 expect(error_set_info.ErrorSet.?[2].value == @errorToInt(TestErrorSet.Third));
149149
150 const error_union_info = @typeInfo(TestErrorSet!usize);150 const error_union_info = @typeInfo(TestErrorSet!usize);
151 expect(TypeId(error_union_info) == TypeId.ErrorUnion);151 expect(TypeId(error_union_info) == TypeId.ErrorUnion);
152 expect(error_union_info.ErrorUnion.error_set == TestErrorSet);152 expect(error_union_info.ErrorUnion.error_set == TestErrorSet);
153 expect(error_union_info.ErrorUnion.payload == usize);153 expect(error_union_info.ErrorUnion.payload == usize);
154
155 const global_info = @typeInfo(anyerror);
156 expect(TypeId(global_info) == TypeId.ErrorSet);
157 expect(global_info.ErrorSet == null);
154}158}
155159
156test "type info: enum info" {160test "type info: enum info" {