Skip to content

Commit

Permalink
try to list all not handled errors but blocking on ziglang#7218, NOTE…
Browse files Browse the repository at this point in the history
…: this will fail the ci
  • Loading branch information
g-w1 committed Dec 28, 2020
1 parent 450d5f3 commit ef616e4
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/zir_sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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);
Expand Down

0 comments on commit ef616e4

Please sign in to comment.