| ... | @@ -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); |
| 14471 | | 14471 | |
| 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 | } |
| 14474 | | 14510 | |
| | 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 | } |
| 14486 | | 14529 | |
| ... | @@ -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 | } |
| 14489 | | 14532 | |
| 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 | // TODO | 14535 | 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 | } |
| 14497 | | 14548 | |