authorgravatar for martinhath@users.noreply.github.commartinhath <martinhath@users.noreply.github.com> 2022-08-12 10:45:11+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-08-12 11:45:11+03:00
log92568a0097a6aeb0cd59f1f3dae43d0ecd7a18d0
tree7416433a436f7f0f92192377d182b7aea255d949
parentfa50e179f7f8d523ff00be4cac90bf7659394140
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Sema: add error for signed integer division

stage1 error reads error: division with 'i32' and 'comptime_int': signed integers must use @divTrunc, @divFloor, or @divExact Fixes: #12339

3 files changed, 15 insertions(+), 10 deletions(-)

src/Sema.zig+6-1
......@@ -11261,7 +11261,12 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
1126111261 try sema.addDivByZeroSafety(block, resolved_type, maybe_rhs_val, casted_rhs, is_int);
1126211262 }
1126311263
11264 const air_tag = if (is_int) Air.Inst.Tag.div_trunc else switch (block.float_mode) {
11264 const air_tag = if (is_int) blk: {
11265 if (lhs_ty.isSignedInt() or rhs_ty.isSignedInt()) {
11266 return sema.fail(block, src, "division with '{s}' and '{s}': signed integers must use @divTrunc, @divFloor, or @divExact", .{ @tagName(lhs_ty.tag()), @tagName(rhs_ty.tag()) });
11267 }
11268 break :blk Air.Inst.Tag.div_trunc;
11269 } else switch (block.float_mode) {
1126511270 .Optimized => Air.Inst.Tag.div_float_optimized,
1126611271 .Strict => Air.Inst.Tag.div_float,
1126711272 };
test/cases/compile_errors/signed_integer_division.zig created+9
......@@ -0,0 +1,9 @@
1export fn foo(a: i32, b: i32) i32 {
2 return a / b;
3}
4
5// error
6// backend=stage2
7// target=native
8//
9// :2:14: error: division with 'i32' and 'i32': signed integers must use @divTrunc, @divFloor, or @divExact
test/cases/compile_errors/stage1/obj/signed_integer_division.zig deleted-9
......@@ -1,9 +0,0 @@
1export fn foo(a: i32, b: i32) i32 {
2 return a / b;
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:14: error: division with 'i32' and 'i32': signed integers must use @divTrunc, @divFloor, or @divExact