authorgravatar for mitchell.hashimoto@gmail.comMitchell Hashimoto <mitchell.hashimoto@gmail.com> 2022-03-25 09:53:07-07:00
committergravatar for mitchell.hashimoto@gmail.comMitchell Hashimoto <mitchell.hashimoto@gmail.com> 2022-03-27 09:20:37-07:00
log4ad98d07141cfba9ec9eb94c5daa13cc70c9d9cd
treec114871429dee37afe75a5427a6c1aa86517b592
parent8fbac2e86d35bf363b67aba0f1915b7c9d32dcd0
signature Commit is signed but in an unrecognized format.

stage2: runtime safety check intCast to u0 must fit


1 files changed, 15 insertions(+), 1 deletions(-)

src/Sema.zig+15-1
...@@ -6782,6 +6782,20 @@ fn intCast(...@@ -6782,6 +6782,20 @@ fn intCast(
6782 }6782 }
67836783
6784 if ((try sema.typeHasOnePossibleValue(block, dest_ty_src, dest_ty))) |opv| {6784 if ((try sema.typeHasOnePossibleValue(block, dest_ty_src, dest_ty))) |opv| {
6785 // requirement: intCast(u0, input) iff input == 0
6786 if (runtime_safety and block.wantSafety()) {
6787 try sema.requireRuntimeBlock(block, operand_src);
6788 const target = sema.mod.getTarget();
6789 const wanted_info = dest_ty.intInfo(target);
6790 const wanted_bits = wanted_info.bits;
6791
6792 if (wanted_bits == 0) {
6793 const zero_inst = try sema.addConstant(sema.typeOf(operand), Value.zero);
6794 const is_in_range = try block.addBinOp(.cmp_eq, operand, zero_inst);
6795 try sema.addSafetyCheck(block, is_in_range, .cast_truncated_data);
6796 }
6797 }
6798
6785 return sema.addConstant(dest_ty, opv);6799 return sema.addConstant(dest_ty, opv);
6786 }6800 }
67876801
...@@ -6794,7 +6808,7 @@ fn intCast(...@@ -6794,7 +6808,7 @@ fn intCast(
6794 const actual_bits = actual_info.bits;6808 const actual_bits = actual_info.bits;
6795 const wanted_bits = wanted_info.bits;6809 const wanted_bits = wanted_info.bits;
67966810
6797 // requirement: operand can fit into bit size of destination type6811 // requirement: bitSizeOf(operand) <= bitSizeOf(destination type)
6798 if (actual_bits > wanted_bits) {6812 if (actual_bits > wanted_bits) {
6799 const max_int = try dest_ty.maxInt(sema.arena, target);6813 const max_int = try dest_ty.maxInt(sema.arena, target);
6800 const max_int_inst = try sema.addConstant(operand_ty, max_int);6814 const max_int_inst = try sema.addConstant(operand_ty, max_int);