| author | |
| committer | |
| log | 080dd27157e79adc8e5d5b7c1dcf94919624abab |
| tree | 108c39a7f852c0b606bf2e8b2f6a6fecb7a8c2ee |
| parent | e861da03f9b9d1261cf32872ea942ee7a63812d3 |
| signature | Commit is signed but in an unrecognized format. |
`builtin.TypeInfo.ErrorSet` is now `?[]Error`
instead of `struct{errors:[]Error}`.
closes #19367 files changed, 17 insertions(+), 49 deletions(-)
doc/docgen.zig+1-1| ... | ... | @@ -707,7 +707,7 @@ const builtin_types = [][]const u8{ |
| 707 | 707 | "f16", "f32", "f64", "f128", "c_longdouble", "c_short", |
| 708 | 708 | "c_ushort", "c_int", "c_uint", "c_long", "c_ulong", "c_longlong", |
| 709 | 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 | }; |
| 712 | 712 | |
| 713 | 713 | fn isType(name: []const u8) bool { |
src/codegen.cpp+1-3| ... | ... | @@ -7581,9 +7581,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) { |
| 7581 | 7581 | " value: comptime_int,\n" |
| 7582 | 7582 | " };\n" |
| 7583 | 7583 | "\n" |
| 7584 | " pub const ErrorSet = struct {\n" | |
| 7585 | " errors: []Error,\n" | |
| 7586 | " };\n" | |
| 7584 | " pub const ErrorSet = ?[]Error;\n" | |
| 7587 | 7585 | "\n" |
| 7588 | 7586 | " pub const EnumField = struct {\n" |
| 7589 | 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 | 18265 | result->special = ConstValSpecialStatic; |
| 18266 | 18266 | result->type = ir_type_info_get_type(ira, "ErrorSet", nullptr); |
| 18267 | 18267 | |
| 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 | 18268 | ZigType *type_info_error_type = ir_type_info_get_type(ira, "Error", nullptr); |
| 18275 | 18269 | if (!resolve_inferred_error_set(ira->codegen, type_entry, source_instr->source_node)) { |
| 18276 | 18270 | return ErrorSemanticAnalyzeFail; |
| 18277 | 18271 | } |
| 18278 | 18272 | if (type_is_global_error_set(type_entry)) { |
| 18279 | ir_add_error(ira, source_instr, | |
| 18280 | buf_sprintf("TODO: compiler bug: implement @typeInfo support for anyerror. https://github.com/ziglang/zig/issues/1936")); | |
| 18281 | return ErrorSemanticAnalyzeFail; | |
| 18273 | result->data.x_optional = nullptr; | |
| 18274 | break; | |
| 18282 | 18275 | } |
| 18276 | ConstExprValue *slice_val = create_const_vals(1); | |
| 18277 | result->data.x_optional = slice_val; | |
| 18283 | 18278 | |
| 18284 | 18279 | uint32_t error_count = type_entry->data.error_set.err_count; |
| 18285 | 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 | 18283 | error_array->data.x_array.special = ConstArraySpecialNone; |
| 18289 | 18284 | error_array->data.x_array.data.s_none.elements = create_const_vals(error_count); |
| 18290 | 18285 | |
| 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 | 18287 | for (uint32_t error_index = 0; error_index < error_count; error_index++) { |
| 18293 | 18288 | ErrorTableEntry *error = type_entry->data.error_set.errors[error_index]; |
| 18294 | 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 | 112 | output: fn (@typeOf(context), []const u8) Errors!void, |
| 113 | 113 | ) Errors!void { |
| 114 | 114 | const T = @typeOf(value); |
| 115 | if (T == anyerror) { | |
| 116 | try output(context, "error."); | |
| 117 | return output(context, @errorName(value)); | |
| 118 | } | |
| 119 | 115 | switch (@typeInfo(T)) { |
| 120 | 116 | builtin.TypeId.ComptimeInt, builtin.TypeId.Int, builtin.TypeId.Float => { |
| 121 | 117 | return formatValue(value, fmt, context, Errors, output); |
std/meta.zig+3-27| ... | ... | @@ -13,32 +13,8 @@ const TypeInfo = builtin.TypeInfo; |
| 13 | 13 | pub fn tagName(v: var) []const u8 { |
| 14 | 14 | const T = @typeOf(v); |
| 15 | 15 | switch (@typeInfo(T)) { |
| 16 | TypeId.Enum => |info| { | |
| 17 | const Tag = info.tag_type; | |
| 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) ++ "'"), | |
| 16 | TypeId.ErrorSet => return @errorName(v), | |
| 17 | else => return @tagName(v), | |
| 42 | 18 | } |
| 43 | 19 | } |
| 44 | 20 | |
| ... | ... | @@ -267,7 +243,7 @@ pub fn fields(comptime T: type) switch (@typeInfo(T)) { |
| 267 | 243 | TypeId.Struct => |info| info.fields, |
| 268 | 244 | TypeId.Union => |info| info.fields, |
| 269 | 245 | TypeId.Enum => |info| info.fields, |
| 270 | TypeId.ErrorSet => |info| info.errors, | |
| 246 | TypeId.ErrorSet => |errors| errors.?, // must be non global error set | |
| 271 | 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 | 5 | /// This function is intended to be used only in tests. It prints diagnostics to stderr |
| 6 | 6 | /// and then aborts when actual_error_union is not expected_error. |
| 7 | 7 | pub 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 | 8 | if (actual_error_union) |actual_payload| { |
| 10 | 9 | // TODO remove workaround here for https://github.com/ziglang/zig/issues/557 |
| 11 | 10 | if (@sizeOf(@typeOf(actual_payload)) == 0) { |
test/stage1/behavior/type_info.zig+7-3| ... | ... | @@ -143,14 +143,18 @@ fn testErrorSet() void { |
| 143 | 143 | |
| 144 | 144 | const error_set_info = @typeInfo(TestErrorSet); |
| 145 | 145 | expect(TypeId(error_set_info) == TypeId.ErrorSet); |
| 146 | expect(error_set_info.ErrorSet.errors.len == 3); | |
| 147 | expect(mem.eql(u8, error_set_info.ErrorSet.errors[0].name, "First")); | |
| 148 | expect(error_set_info.ErrorSet.errors[2].value == @errorToInt(TestErrorSet.Third)); | |
| 146 | expect(error_set_info.ErrorSet.?.len == 3); | |
| 147 | expect(mem.eql(u8, error_set_info.ErrorSet.?[0].name, "First")); | |
| 148 | expect(error_set_info.ErrorSet.?[2].value == @errorToInt(TestErrorSet.Third)); | |
| 149 | 149 | |
| 150 | 150 | const error_union_info = @typeInfo(TestErrorSet!usize); |
| 151 | 151 | expect(TypeId(error_union_info) == TypeId.ErrorUnion); |
| 152 | 152 | expect(error_union_info.ErrorUnion.error_set == TestErrorSet); |
| 153 | 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 | } |
| 155 | 159 | |
| 156 | 160 | test "type info: enum info" { |