From ef616e43a4dccc88d02e793db3c86b1a10b3e029 Mon Sep 17 00:00:00 2001 From: g-w1 Date: Wed, 23 Dec 2020 22:24:24 -0500 Subject: [PATCH] try to list all not handled errors but blocking on #7218, NOTE: this will fail the ci --- src/zir_sema.zig | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/zir_sema.zig b/src/zir_sema.zig index c4f3e891a369..a44ea5e93e3a 100644 --- a/src/zir_sema.zig +++ b/src/zir_sema.zig @@ -1412,9 +1412,8 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw const gotten_err_set = target.ty.getErrs(); const is_anyerror = gotten_err_set == .anyerror; var is_single = gotten_err_set == .err_single; - if (is_anyerror and !(inst.kw_args.special_prong == .@"else")) { + if (is_anyerror and !(inst.kw_args.special_prong == .@"else")) return mod.fail(scope, inst.base.src, "else prong required when switching on type 'anyerror'", .{}); - } var seen_values = std.HashMap(Value, usize, Value.hash, Value.eql, std.hash_map.DefaultMaxLoadPercentage).init(mod.gpa); defer seen_values.deinit(); for (inst.positionals.items) |item| { @@ -1437,8 +1436,22 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw // } // } } - if (!is_single and !is_anyerror and gotten_err_set.multiple.size > inst.positionals.items.len and !(inst.kw_args.special_prong == .@"else")) - return mod.fail(scope, inst.base.src, "switch must handle all possibilities", .{}); + if (!is_single and !is_anyerror and gotten_err_set.multiple.size > inst.positionals.items.len and !(inst.kw_args.special_prong == .@"else")) { + var not_handled = false; + var it = gotten_err_set.multiple.iterator(); + while (it.next()) |entry| { + var err: Value.Payload.Error = .{ .name = entry.key, .value = entry.value }; + const e_val = Value.initPayload(&err.base); + if (seen_values.get(e_val) == null) { + switch (mod.fail(scope, inst.base.src, "error.{} not handled in switch", .{entry.key})) { + error.AnalysisFail => {}, + else => |e| return e, + } + not_handled = true; + } + } + if (not_handled) return error.AnalysisFail; + } }, .Int, .ComptimeInt => { var range_set = @import("RangeSet.zig").init(mod.gpa);