Commit 0847b47bf8

Vexu <git@vexu.eu>
2020-05-11 23:24:09
fix `@intToFloat` on comptime_floats
1 parent 3e3c651
Changed files (2)
src
test
stage1
behavior
src/ir.cpp
@@ -12760,9 +12760,7 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr,
             const_val->type = new_type;
             break;
         case CastOpIntToFloat:
-            {
-                assert(new_type->id == ZigTypeIdFloat);
-
+            if (new_type->id == ZigTypeIdFloat) {
                 BigFloat bigfloat;
                 bigfloat_init_bigint(&bigfloat, &other_val->data.x_bigint);
                 switch (new_type->data.floating.bit_count) {
@@ -12783,9 +12781,13 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr,
                     default:
                         zig_unreachable();
                 }
-                const_val->special = ConstValSpecialStatic;
-                break;
+            } else if (new_type->id == ZigTypeIdComptimeFloat) {
+                bigfloat_init_bigint(&const_val->data.x_bigfloat, &other_val->data.x_bigint);
+            } else {
+                zig_unreachable();
             }
+            const_val->special = ConstValSpecialStatic;
+            break;
         case CastOpFloatToInt:
             float_init_bigint(&const_val->data.x_bigint, other_val);
             if (new_type->id == ZigTypeIdInt) {
test/stage1/behavior/cast.zig
@@ -827,3 +827,12 @@ test "peer type resolve array pointer and unknown pointer" {
     comptime expect(@TypeOf(&const_array, const_ptr) == [*]const u8);
     comptime expect(@TypeOf(const_ptr, &const_array) == [*]const u8);
 }
+
+test "comptime float casts" {
+    const a = @intToFloat(comptime_float, 1);
+    expect(a == 1);
+    expect(@TypeOf(a) == comptime_float);
+    const b = @floatToInt(comptime_int, 2);
+    expect(b == 2);
+    expect(@TypeOf(b) == comptime_int);
+}