From ca4ee9ae73559062da24937ef4a7376d9d27bbeb Mon Sep 17 00:00:00 2001 From: amp-59 <114923809+amp-59@users.noreply.github.com> Date: Wed, 3 Jan 2024 07:34:24 +0000 Subject: [PATCH] Sema: Added logic to avoid unchecked operations calling `preparePanicId` (#18416) Fixes #18415 --- src/Sema.zig | 5 ++++- .../no_compile_panic_for_unchecked_arith.zig | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 test/cases/no_compile_panic_for_unchecked_arith.zig diff --git a/src/Sema.zig b/src/Sema.zig index 0c464a0bc1..c0b6f3678a 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -16019,9 +16019,12 @@ fn analyzeArithmetic( }; try sema.requireRuntimeBlock(block, src, runtime_src); + if (block.wantSafety() and want_safety and scalar_tag == .Int) { if (mod.backendSupportsFeature(.safety_checked_instructions)) { - _ = try sema.preparePanicId(block, .integer_overflow); + if (air_tag != air_tag_safe) { + _ = try sema.preparePanicId(block, .integer_overflow); + } return block.addBinOp(air_tag_safe, casted_lhs, casted_rhs); } else { const maybe_op_ov: ?Air.Inst.Tag = switch (air_tag) { diff --git a/test/cases/no_compile_panic_for_unchecked_arith.zig b/test/cases/no_compile_panic_for_unchecked_arith.zig new file mode 100644 index 0000000000..a9aa8272b1 --- /dev/null +++ b/test/cases/no_compile_panic_for_unchecked_arith.zig @@ -0,0 +1,16 @@ +pub const panic = @compileError(""); + +export fn entry() usize { + var x: usize = 0; + x +%= 1; + x -%= 1; + x *%= 2; + x +|= 1; + x -|= 1; + x *|= 2; + return x; +} + +// compile +// output_mode=Obj +// backend=stage2,llvm