From bd32a0f3db2d03749e7aef22cbc2aa6f85b689d1 Mon Sep 17 00:00:00 2001 From: William Sengir Date: Wed, 11 May 2022 03:34:44 -0700 Subject: [PATCH 1/4] Sema: add "declared here" note to `zirErrSetCast` --- src/Sema.zig | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 2cc9b82410..d12b85e097 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -14215,12 +14215,18 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat if (!dest_ty.isAnyError()) { const error_name = val.castTag(.@"error").?.data.name; if (!dest_ty.errorSetHasField(error_name)) { - return sema.fail( - block, - src, - "error.{s} not a member of error set '{}'", - .{ error_name, dest_ty.fmt(sema.mod) }, - ); + const msg = msg: { + const msg = try sema.errMsg( + block, + src, + "error.{s} not a member of error set '{}'", + .{ error_name, dest_ty.fmt(sema.mod) }, + ); + errdefer msg.destroy(sema.gpa); + try sema.addDeclaredHereNote(msg, dest_ty); + break :msg msg; + }; + return sema.failWithOwnedErrorMsg(block, msg); } } From 363cdb7d5fc376dfb6fc0f67d983922fc4208aa1 Mon Sep 17 00:00:00 2001 From: William Sengir Date: Wed, 11 May 2022 03:36:58 -0700 Subject: [PATCH 2/4] Sema: add error for disjoint error sets in `zirErrSetCast` --- src/Sema.zig | 41 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index d12b85e097..5f584fe40c 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -14209,9 +14209,46 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat try sema.checkErrorSetType(block, dest_ty_src, dest_ty); try sema.checkErrorSetType(block, operand_src, operand_ty); - if (try sema.resolveDefinedValue(block, operand_src, operand)) |val| { - try sema.resolveInferredErrorSetTy(block, src, dest_ty); + // operand must be defined since it can be an invalid error value + const maybe_operand_val = try sema.resolveDefinedValue(block, operand_src, operand); + if (disjoint: { + // Try avoiding resolving inferred error sets if we can + if (!dest_ty.isAnyError() and dest_ty.errorSetNames().len == 0) break :disjoint true; + if (!operand_ty.isAnyError() and operand_ty.errorSetNames().len == 0) break :disjoint true; + if (dest_ty.isAnyError()) break :disjoint false; + if (operand_ty.isAnyError()) break :disjoint false; + for (dest_ty.errorSetNames()) |dest_err_name| + if (operand_ty.errorSetHasField(dest_err_name)) + break :disjoint false; + + if (dest_ty.tag() != .error_set_inferred and operand_ty.tag() != .error_set_inferred) + break :disjoint true; + + try sema.resolveInferredErrorSetTy(block, dest_ty_src, dest_ty); + try sema.resolveInferredErrorSetTy(block, operand_src, operand_ty); + for (dest_ty.errorSetNames()) |dest_err_name| + if (operand_ty.errorSetHasField(dest_err_name)) + break :disjoint false; + + break :disjoint true; + }) { + const msg = msg: { + const msg = try sema.errMsg( + block, + src, + "error sets '{}' and '{}' have no common errors", + .{ operand_ty.fmt(sema.mod), dest_ty.fmt(sema.mod) }, + ); + errdefer msg.destroy(sema.gpa); + try sema.addDeclaredHereNote(msg, operand_ty); + try sema.addDeclaredHereNote(msg, dest_ty); + break :msg msg; + }; + return sema.failWithOwnedErrorMsg(block, msg); + } + + if (maybe_operand_val) |val| { if (!dest_ty.isAnyError()) { const error_name = val.castTag(.@"error").?.data.name; if (!dest_ty.errorSetHasField(error_name)) { From 4a6aee9dbe41f640b5244536f4bad9a1ab04eb60 Mon Sep 17 00:00:00 2001 From: William Sengir Date: Wed, 11 May 2022 03:42:32 -0700 Subject: [PATCH 3/4] Sema: implement runtime safety checks for `zirErrSetCast` --- src/Sema.zig | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 5f584fe40c..157fc1ea0e 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -14271,10 +14271,18 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat } try sema.requireRuntimeBlock(block, src); - if (block.wantSafety()) { - // TODO + if (block.wantSafety() and !dest_ty.isAnyError()) { + const err_int_inst = try block.addBitCast(Type.u16, operand); + // TODO: Output a switch instead of chained OR's. + var found_match: Air.Inst.Ref = undefined; + for (dest_ty.errorSetNames()) |dest_err_name, i| { + const dest_err_int = (try sema.mod.getErrorValue(dest_err_name)).value; + const dest_err_int_inst = try sema.addIntUnsigned(Type.u16, dest_err_int); + const next_match = try block.addBinOp(.cmp_eq, dest_err_int_inst, err_int_inst); + found_match = if (i == 0) next_match else try block.addBinOp(.bool_or, found_match, next_match); + } + try sema.addSafetyCheck(block, found_match, .invalid_error_code); } - return block.addBitCast(dest_ty, operand); } From 77d732a85f863d5bdafb060151429b18459a2021 Mon Sep 17 00:00:00 2001 From: William Sengir Date: Wed, 11 May 2022 03:44:58 -0700 Subject: [PATCH 4/4] aarch64,arm: disable broken `@errSetCast` test --- test/behavior/error.zig | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/behavior/error.zig b/test/behavior/error.zig index 3c19471705..dc86967175 100644 --- a/test/behavior/error.zig +++ b/test/behavior/error.zig @@ -202,6 +202,8 @@ fn testErrorSetType() !void { test "explicit error set cast" { if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO + if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO try testExplicitErrorSetCast(Set1.A); comptime try testExplicitErrorSetCast(Set1.A);