authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-05-18 16:35:37-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-05-18 16:35:37-04:00
log50a5ddecc54b88cf295fb97b47fecd2a29a1bf8e
tree8d0e18dbcae16beb12d0362318168f9e02a3239a
parent8660661af4626c4bd589e6c6331a6ac786143f12
parent77d732a85f863d5bdafb060151429b18459a2021
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11635 from wsengir/stage2-errsetcast-safety

stage2: `@errSetCast` safety

2 files changed, 64 insertions(+), 11 deletions(-)

src/Sema.zig+62-11
...@@ -14469,18 +14469,61 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -14469,18 +14469,61 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
14469 try sema.checkErrorSetType(block, dest_ty_src, dest_ty);14469 try sema.checkErrorSetType(block, dest_ty_src, dest_ty);
14470 try sema.checkErrorSetType(block, operand_src, operand_ty);14470 try sema.checkErrorSetType(block, operand_src, operand_ty);
1447114471
14472 if (try sema.resolveDefinedValue(block, operand_src, operand)) |val| {14472 // operand must be defined since it can be an invalid error value
14473 try sema.resolveInferredErrorSetTy(block, src, dest_ty);14473 const maybe_operand_val = try sema.resolveDefinedValue(block, operand_src, operand);
14474
14475 if (disjoint: {
14476 // Try avoiding resolving inferred error sets if we can
14477 if (!dest_ty.isAnyError() and dest_ty.errorSetNames().len == 0) break :disjoint true;
14478 if (!operand_ty.isAnyError() and operand_ty.errorSetNames().len == 0) break :disjoint true;
14479 if (dest_ty.isAnyError()) break :disjoint false;
14480 if (operand_ty.isAnyError()) break :disjoint false;
14481 for (dest_ty.errorSetNames()) |dest_err_name|
14482 if (operand_ty.errorSetHasField(dest_err_name))
14483 break :disjoint false;
14484
14485 if (dest_ty.tag() != .error_set_inferred and operand_ty.tag() != .error_set_inferred)
14486 break :disjoint true;
14487
14488 try sema.resolveInferredErrorSetTy(block, dest_ty_src, dest_ty);
14489 try sema.resolveInferredErrorSetTy(block, operand_src, operand_ty);
14490 for (dest_ty.errorSetNames()) |dest_err_name|
14491 if (operand_ty.errorSetHasField(dest_err_name))
14492 break :disjoint false;
14493
14494 break :disjoint true;
14495 }) {
14496 const msg = msg: {
14497 const msg = try sema.errMsg(
14498 block,
14499 src,
14500 "error sets '{}' and '{}' have no common errors",
14501 .{ operand_ty.fmt(sema.mod), dest_ty.fmt(sema.mod) },
14502 );
14503 errdefer msg.destroy(sema.gpa);
14504 try sema.addDeclaredHereNote(msg, operand_ty);
14505 try sema.addDeclaredHereNote(msg, dest_ty);
14506 break :msg msg;
14507 };
14508 return sema.failWithOwnedErrorMsg(block, msg);
14509 }
1447414510
14511 if (maybe_operand_val) |val| {
14475 if (!dest_ty.isAnyError()) {14512 if (!dest_ty.isAnyError()) {
14476 const error_name = val.castTag(.@"error").?.data.name;14513 const error_name = val.castTag(.@"error").?.data.name;
14477 if (!dest_ty.errorSetHasField(error_name)) {14514 if (!dest_ty.errorSetHasField(error_name)) {
14478 return sema.fail(14515 const msg = msg: {
14479 block,14516 const msg = try sema.errMsg(
14480 src,14517 block,
14481 "error.{s} not a member of error set '{}'",14518 src,
14482 .{ error_name, dest_ty.fmt(sema.mod) },14519 "error.{s} not a member of error set '{}'",
14483 );14520 .{ error_name, dest_ty.fmt(sema.mod) },
14521 );
14522 errdefer msg.destroy(sema.gpa);
14523 try sema.addDeclaredHereNote(msg, dest_ty);
14524 break :msg msg;
14525 };
14526 return sema.failWithOwnedErrorMsg(block, msg);
14484 }14527 }
14485 }14528 }
1448614529
...@@ -14488,10 +14531,18 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat...@@ -14488,10 +14531,18 @@ fn zirErrSetCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstDat
14488 }14531 }
1448914532
14490 try sema.requireRuntimeBlock(block, src);14533 try sema.requireRuntimeBlock(block, src);
14491 if (block.wantSafety()) {14534 if (block.wantSafety() and !dest_ty.isAnyError()) {
14492 // TODO14535 const err_int_inst = try block.addBitCast(Type.u16, operand);
14536 // TODO: Output a switch instead of chained OR's.
14537 var found_match: Air.Inst.Ref = undefined;
14538 for (dest_ty.errorSetNames()) |dest_err_name, i| {
14539 const dest_err_int = (try sema.mod.getErrorValue(dest_err_name)).value;
14540 const dest_err_int_inst = try sema.addIntUnsigned(Type.u16, dest_err_int);
14541 const next_match = try block.addBinOp(.cmp_eq, dest_err_int_inst, err_int_inst);
14542 found_match = if (i == 0) next_match else try block.addBinOp(.bool_or, found_match, next_match);
14543 }
14544 try sema.addSafetyCheck(block, found_match, .invalid_error_code);
14493 }14545 }
14494
14495 return block.addBitCast(dest_ty, operand);14546 return block.addBitCast(dest_ty, operand);
14496}14547}
1449714548
test/behavior/error.zig+2
...@@ -202,6 +202,8 @@ fn testErrorSetType() !void {...@@ -202,6 +202,8 @@ fn testErrorSetType() !void {
202202
203test "explicit error set cast" {203test "explicit error set cast" {
204 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO204 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
205 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
206 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
205207
206 try testExplicitErrorSetCast(Set1.A);208 try testExplicitErrorSetCast(Set1.A);
207 comptime try testExplicitErrorSetCast(Set1.A);209 comptime try testExplicitErrorSetCast(Set1.A);