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
This commit is contained in:
martinhath 2022-08-12 10:45:11 +02:00 committed by GitHub
parent fa50e179f7
commit 92568a0097
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 10 deletions

View File

@ -11261,7 +11261,12 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
try sema.addDivByZeroSafety(block, resolved_type, maybe_rhs_val, casted_rhs, is_int);
}
const air_tag = if (is_int) Air.Inst.Tag.div_trunc else switch (block.float_mode) {
const air_tag = if (is_int) blk: {
if (lhs_ty.isSignedInt() or rhs_ty.isSignedInt()) {
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()) });
}
break :blk Air.Inst.Tag.div_trunc;
} else switch (block.float_mode) {
.Optimized => Air.Inst.Tag.div_float_optimized,
.Strict => Air.Inst.Tag.div_float,
};

View File

@ -0,0 +1,9 @@
export fn foo(a: i32, b: i32) i32 {
return a / b;
}
// error
// backend=stage2
// target=native
//
// :2:14: error: division with 'i32' and 'i32': signed integers must use @divTrunc, @divFloor, or @divExact

View File

@ -1,9 +0,0 @@
export fn foo(a: i32, b: i32) i32 {
return a / b;
}
// error
// backend=stage1
// target=native
//
// tmp.zig:2:14: error: division with 'i32' and 'i32': signed integers must use @divTrunc, @divFloor, or @divExact