Skip to content

Commit

Permalink
few small things before moving error set widening from Module.coerce …
Browse files Browse the repository at this point in the history
…-> 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
  • Loading branch information
g-w1 committed Dec 28, 2020
1 parent d6be906 commit 450d5f3
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/Module.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2743,6 +2743,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())
Expand Down
2 changes: 2 additions & 0 deletions src/type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 8 additions & 11 deletions src/zir_sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -897,8 +897,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();
Expand All @@ -911,8 +911,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();
Expand Down Expand Up @@ -1834,13 +1834,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()) {
Expand Down

0 comments on commit 450d5f3

Please sign in to comment.