authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-16 16:17:56+03:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-08-17 14:09:42-07:00
log59b6483d63ca68f63c0b3758e80da918629ada2c
treecde36ed0a1e6bf58fe3562eb7bbd38636bb17559
parent07f64a2e13ab80acffba7f7bdd5d7c58df7893c0

add test


2 files changed, 32 insertions(+), 9 deletions(-)

src/Sema.zig+8-9
...@@ -11202,18 +11202,17 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins...@@ -11202,18 +11202,17 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins
11202 const maybe_lhs_val = try sema.resolveMaybeUndefValIntable(block, lhs_src, casted_lhs);11202 const maybe_lhs_val = try sema.resolveMaybeUndefValIntable(block, lhs_src, casted_lhs);
11203 const maybe_rhs_val = try sema.resolveMaybeUndefValIntable(block, rhs_src, casted_rhs);11203 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) or11205 if ((lhs_ty.zigTypeTag() == .ComptimeFloat and rhs_ty.zigTypeTag() == .ComptimeInt) or
11206 (lhs_ty.tag() == .comptime_int and rhs_ty.tag() == .comptime_float))11206 (lhs_ty.zigTypeTag() == .ComptimeInt and rhs_ty.zigTypeTag() == .ComptimeFloat))
11207 {11207 {
11208 // If it makes a difference whether we coerce to ints or floats before doing the division, error.11208 // If it makes a difference whether we coerce to ints or floats before doing the division, error.
11209 // If lhs % rhs is 0, it doesn't matter.11209 // If lhs % rhs is 0, it doesn't matter.
11210 var lhs_val = maybe_lhs_val orelse unreachable;11210 const lhs_val = maybe_lhs_val orelse unreachable;
11211 var rhs_val = maybe_rhs_val orelse unreachable;11211 const rhs_val = maybe_rhs_val orelse unreachable;
11212 var rem = lhs_val.floatRem(rhs_val, resolved_type, sema.arena, target) catch unreachable;11212 const rem = lhs_val.floatRem(rhs_val, resolved_type, sema.arena, target) catch unreachable;
11213 var float_rem = rem.toFloat(f32);11213 if (rem.compareWithZero(.neq)) {
11214 if (float_rem != 0.0) {11214 return sema.fail(block, src, "ambiguous coercion of division operands '{s}' and '{s}'; division has non-zero reminder '{}'", .{
11215 return sema.fail(block, src, "ambiguous coercion of division operands: '{s}' and '{s}': division has non-zero reminder: {d}", .{11215 @tagName(lhs_ty.tag()), @tagName(rhs_ty.tag()), rem.fmtValue(resolved_type, sema.mod),
11216 @tagName(lhs_ty.tag()), @tagName(rhs_ty.tag()), float_rem,
11217 });11216 });
11218 }11217 }
11219 }11218 }
test/cases/compile_errors/ambiguous_coercion_of_division_operands.zig created+24
...@@ -0,0 +1,24 @@
1export fn entry1() void {
2 var f: f32 = 54.0 / 5;
3 _ = f;
4}
5export fn entry2() void {
6 var f: f32 = 54 / 5.0;
7 _ = f;
8}
9export fn entry3() void {
10 var f: f32 = 55.0 / 5;
11 _ = f;
12}
13export 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'