Commit 1b83ee78a4

Andrew Kelley <andrew@ziglang.org>
2019-08-11 18:00:32
allow comptime_int to implicit cast to comptime_float
1 parent 4bd4c5e
Changed files (4)
src
std
test
src/ir.cpp
@@ -9713,6 +9713,9 @@ static bool ir_num_lit_fits_in_other_type(IrAnalyze *ira, IrInstruction *instruc
     bool const_val_is_float = (const_val->type->id == ZigTypeIdFloat || const_val->type->id == ZigTypeIdComptimeFloat);
     assert(const_val_is_int || const_val_is_float);
 
+    if (const_val_is_int && other_type->id == ZigTypeIdComptimeFloat) {
+        return true;
+    }
     if (other_type->id == ZigTypeIdFloat) {
         if (const_val->type->id == ZigTypeIdComptimeInt || const_val->type->id == ZigTypeIdComptimeFloat) {
             return true;
std/math.zig
@@ -305,6 +305,13 @@ test "math.min" {
         testing.expect(@typeOf(result) == i16);
         testing.expect(result == -200);
     }
+    {
+        const a = 10.34;
+        var b: f32 = 999.12;
+        var result = min(a, b);
+        testing.expect(@typeOf(result) == f32);
+        testing.expect(result == 10.34);
+    }
 }
 
 pub fn max(x: var, y: var) @typeOf(x + y) {
test/stage1/behavior/cast.zig
@@ -508,7 +508,7 @@ test "peer type resolution: unreachable, null, slice" {
 }
 
 test "peer type resolution: unreachable, error set, unreachable" {
-    const Error = error {
+    const Error = error{
         FileDescriptorAlreadyPresentInSet,
         OperationCausesCircularLoop,
         FileDescriptorNotRegistered,
@@ -529,3 +529,8 @@ test "peer type resolution: unreachable, error set, unreachable" {
     };
     expect(transformed_err == error.SystemResources);
 }
+
+test "implicit cast comptime_int to comptime_float" {
+    comptime expect(comptime_float(10) == f32(10));
+    expect(2 == 2.0);
+}
test/compile_errors.zig
@@ -3225,14 +3225,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
         "tmp.zig:3:17: note: value 8 cannot fit into type u3",
     );
 
-    cases.add(
-        "incompatible number literals",
-        \\const x = 2 == 2.0;
-        \\export fn entry() usize { return @sizeOf(@typeOf(x)); }
-    ,
-        "tmp.zig:1:11: error: integer value 2 cannot be implicitly casted to type 'comptime_float'",
-    );
-
     cases.add(
         "missing function call param",
         \\const Foo = struct {