From aa0bd20c466216ba3d74d7c307274e6421f805fb Mon Sep 17 00:00:00 2001 From: g-w1 Date: Wed, 23 Dec 2020 15:47:41 -0500 Subject: [PATCH] few small things before moving error set widening from Module.coerce -> Module.resolvePeerTypes * make Type.eql for errors not reach unreachable * use global_error_set instead of getErrorValue * use value in analyzeInstCmp instead of type and use the u16 error value field instead of the name so that we don't need mem.eql --- src/Module.zig | 5 +++++ src/type.zig | 2 ++ src/zir_sema.zig | 19 ++++++++----------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Module.zig b/src/Module.zig index 87cf84d4428d..805d20f6c416 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -2731,6 +2731,11 @@ pub fn resolvePeerTypes(self: *Module, scope: *Scope, instructions: []*Inst) !Ty chosen = candidate; continue; } + + // error set widening + // TODO remove: dest is chosen, cur is canditade + if (chosen.ty.zigTypeTag() == .ErrorSet and candidate.ty.zigTypeTag() == .ErrorSet) {} + if (chosen.ty.isInt() and candidate.ty.isInt() and chosen.ty.isSignedInt() == candidate.ty.isSignedInt()) diff --git a/src/type.zig b/src/type.zig index 3bb0a2c64d3b..333b38a0cf7b 100644 --- a/src/type.zig +++ b/src/type.zig @@ -232,6 +232,8 @@ pub const Type = extern union { return true else return false; + } else if (b.tag() == .anyerror) { + return false; } if (a.tag() == .error_set_single and b.tag() == .error_set_single) { diff --git a/src/zir_sema.zig b/src/zir_sema.zig index e765e7bd6c53..6203b96215fe 100644 --- a/src/zir_sema.zig +++ b/src/zir_sema.zig @@ -873,8 +873,8 @@ fn analyzeInstMergeErrorSets(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) switch (lhs_fields) { .err_single => |name| { - const entry = mod.global_error_set.get(name).?; - payload.fields.putAssumeCapacity(name, entry); + const num = mod.global_error_set.get(name).?; + payload.fields.putAssumeCapacity(name, num); }, .multiple => |multiple| { var it = multiple.iterator(); @@ -887,8 +887,8 @@ fn analyzeInstMergeErrorSets(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) switch (rhs_fields) { .err_single => |name| { - const entry = try mod.getErrorValue(name); - payload.fields.putAssumeCapacity(entry.key, entry.value); + const num = mod.global_error_set.get(name).?; + payload.fields.putAssumeCapacity(name, num); }, .multiple => |multiple| { var it = multiple.iterator(); @@ -1751,13 +1751,10 @@ fn analyzeInstCmp( if (!is_equality_cmp) { return mod.fail(scope, inst.base.src, "{} operator not allowed for errors", .{@tagName(op)}); } - const lhs_comptime = lhs.ty.cast(Type.Payload.ErrorSetSingle); - const rhs_comptime = rhs.ty.cast(Type.Payload.ErrorSetSingle); - if (lhs_comptime != null and rhs_comptime != null) { - if (op == .eq) - return mod.constBool(scope, inst.base.src, !std.mem.eql(u8, lhs_comptime.?.name, rhs_comptime.?.name)) - else - return mod.constBool(scope, inst.base.src, std.mem.eql(u8, lhs_comptime.?.name, rhs_comptime.?.name)); + const lhs_val = lhs.value(); + const rhs_val = rhs.value(); + if (lhs_val != null and rhs_val != null) { + return mod.constBool(scope, inst.base.src, !(lhs_val.?.cast(Value.Payload.Error).?.value == rhs_val.?.cast(Value.Payload.Error).?.value) == (op == .eq)); } return mod.fail(scope, inst.base.src, "TODO runtime error comparison", .{}); } else if (lhs.ty.isNumeric() and rhs.ty.isNumeric()) {