Skip to content

Commit 92568a0

Browse files
authored
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
1 parent fa50e17 commit 92568a0

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/Sema.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export 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

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)