Skip to content

Commit 59b6483

Browse files
Vexuandrewrk
authored andcommitted
add test
1 parent 07f64a2 commit 59b6483

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/Sema.zig

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11202,18 +11202,17 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
1120211202
const maybe_lhs_val = try sema.resolveMaybeUndefValIntable(block, lhs_src, casted_lhs);
1120311203
const maybe_rhs_val = try sema.resolveMaybeUndefValIntable(block, rhs_src, casted_rhs);
1120411204

11205-
if ((lhs_ty.tag() == .comptime_float and rhs_ty.tag() == .comptime_int) or
11206-
(lhs_ty.tag() == .comptime_int and rhs_ty.tag() == .comptime_float))
11205+
if ((lhs_ty.zigTypeTag() == .ComptimeFloat and rhs_ty.zigTypeTag() == .ComptimeInt) or
11206+
(lhs_ty.zigTypeTag() == .ComptimeInt and rhs_ty.zigTypeTag() == .ComptimeFloat))
1120711207
{
1120811208
// If it makes a difference whether we coerce to ints or floats before doing the division, error.
1120911209
// If lhs % rhs is 0, it doesn't matter.
11210-
var lhs_val = maybe_lhs_val orelse unreachable;
11211-
var rhs_val = maybe_rhs_val orelse unreachable;
11212-
var rem = lhs_val.floatRem(rhs_val, resolved_type, sema.arena, target) catch unreachable;
11213-
var float_rem = rem.toFloat(f32);
11214-
if (float_rem != 0.0) {
11215-
return sema.fail(block, src, "ambiguous coercion of division operands: '{s}' and '{s}': division has non-zero reminder: {d}", .{
11216-
@tagName(lhs_ty.tag()), @tagName(rhs_ty.tag()), float_rem,
11210+
const lhs_val = maybe_lhs_val orelse unreachable;
11211+
const rhs_val = maybe_rhs_val orelse unreachable;
11212+
const rem = lhs_val.floatRem(rhs_val, resolved_type, sema.arena, target) catch unreachable;
11213+
if (rem.compareWithZero(.neq)) {
11214+
return sema.fail(block, src, "ambiguous coercion of division operands '{s}' and '{s}'; division has non-zero reminder '{}'", .{
11215+
@tagName(lhs_ty.tag()), @tagName(rhs_ty.tag()), rem.fmtValue(resolved_type, sema.mod),
1121711216
});
1121811217
}
1121911218
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
export fn entry1() void {
2+
var f: f32 = 54.0 / 5;
3+
_ = f;
4+
}
5+
export fn entry2() void {
6+
var f: f32 = 54 / 5.0;
7+
_ = f;
8+
}
9+
export fn entry3() void {
10+
var f: f32 = 55.0 / 5;
11+
_ = f;
12+
}
13+
export fn entry4() void {
14+
var f: f32 = 55 / 5.0;
15+
_ = f;
16+
}
17+
18+
19+
// error
20+
// backend=stage2
21+
// target=native
22+
//
23+
// :2:23: error: ambiguous coercion of division operands 'comptime_float' and 'comptime_int'; division has non-zero reminder '4'
24+
// :6:21: error: ambiguous coercion of division operands 'comptime_int' and 'comptime_float'; division has non-zero reminder '4'

0 commit comments

Comments
 (0)