From 07f64a2e13ab80acffba7f7bdd5d7c58df7893c0 Mon Sep 17 00:00:00 2001 From: Martin Hafskjold Thoresen Date: Wed, 10 Aug 2022 23:32:02 +0200 Subject: Sema: error on ambiguous coercion of comptime float and ints The following, from the documentation as of the time of writing, illustrates the problem: ```zig // Compile time coercion of float to int test "implicit cast to comptime_int" { var f: f32 = 54.0 / 5; _ = f; } ``` It is not clear how to unify the types of 54.0 and 5 to perform the division. We can either - cast 54.0 to comptime_int resulting in @as(comptime_int, 10), which is casted to @as(f32, 10), or - cast 5 to comptime_float resulting in @as(comptime_float, 10.8), which is casted to @as(f32, 10.8) Since the two resulting values are different, a compiler error is appropriate. If we know that casting to either type will result in the same value we don't need to error. For instance, 10.0 / 2 is okay, as is 10 / 2.0. Fixes: #12364 --- src/Sema.zig | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'src') diff --git a/src/Sema.zig b/src/Sema.zig index f1d140520c..e5700a6fe4 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -11202,6 +11202,22 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins const maybe_lhs_val = try sema.resolveMaybeUndefValIntable(block, lhs_src, casted_lhs); const maybe_rhs_val = try sema.resolveMaybeUndefValIntable(block, rhs_src, casted_rhs); + if ((lhs_ty.tag() == .comptime_float and rhs_ty.tag() == .comptime_int) or + (lhs_ty.tag() == .comptime_int and rhs_ty.tag() == .comptime_float)) + { + // If it makes a difference whether we coerce to ints or floats before doing the division, error. + // If lhs % rhs is 0, it doesn't matter. + var lhs_val = maybe_lhs_val orelse unreachable; + var rhs_val = maybe_rhs_val orelse unreachable; + var rem = lhs_val.floatRem(rhs_val, resolved_type, sema.arena, target) catch unreachable; + var float_rem = rem.toFloat(f32); + if (float_rem != 0.0) { + return sema.fail(block, src, "ambiguous coercion of division operands: '{s}' and '{s}': division has non-zero reminder: {d}", .{ + @tagName(lhs_ty.tag()), @tagName(rhs_ty.tag()), float_rem, + }); + } + } + // TODO: emit compile error when .div is used on integers and there would be an // ambiguous result between div_floor and div_trunc. -- cgit v1.2.3