mirror of
https://github.com/ziglang/zig.git
synced 2025-12-07 14:53:08 +00:00
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:
parent
fa50e179f7
commit
92568a0097
@ -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);
|
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,
|
.Optimized => Air.Inst.Tag.div_float_optimized,
|
||||||
.Strict => Air.Inst.Tag.div_float,
|
.Strict => Air.Inst.Tag.div_float,
|
||||||
};
|
};
|
||||||
|
|||||||
9
test/cases/compile_errors/signed_integer_division.zig
Normal file
9
test/cases/compile_errors/signed_integer_division.zig
Normal 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
|
||||||
@ -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
|
|
||||||
Loading…
x
Reference in New Issue
Block a user