Commit 895aea0b63

Andrew Kelley <andrew@ziglang.org>
2021-10-22 08:01:05
Sema: fix type checking of `@intToFloat` operands
There was a typo and they were backwards.
1 parent 0a6851c
Changed files (3)
src/Sema.zig
@@ -9561,8 +9561,8 @@ fn zirIntToFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!
     const operand = sema.resolveInst(extra.rhs);
     const operand_ty = sema.typeOf(operand);
 
-    _ = try sema.checkIntType(block, ty_src, dest_ty);
-    try sema.checkFloatType(block, operand_src, operand_ty);
+    try sema.checkFloatType(block, ty_src, dest_ty);
+    _ = try sema.checkIntType(block, operand_src, operand_ty);
 
     if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| {
         const target = sema.mod.getTarget();
test/behavior/cast.zig
@@ -77,3 +77,32 @@ test "pointer reinterpret const float to int" {
     else
         try expect(int_val == 0x3fe33333);
 }
+
+test "comptime_int @intToFloat" {
+    {
+        const result = @intToFloat(f16, 1234);
+        try expect(@TypeOf(result) == f16);
+        try expect(result == 1234.0);
+    }
+    {
+        const result = @intToFloat(f32, 1234);
+        try expect(@TypeOf(result) == f32);
+        try expect(result == 1234.0);
+    }
+    {
+        const result = @intToFloat(f64, 1234);
+        try expect(@TypeOf(result) == f64);
+        try expect(result == 1234.0);
+    }
+    {
+        const result = @intToFloat(f128, 1234);
+        try expect(@TypeOf(result) == f128);
+        try expect(result == 1234.0);
+    }
+    // big comptime_int (> 64 bits) to f128 conversion
+    {
+        const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
+        try expect(@TypeOf(result) == f128);
+        try expect(result == 0x1_0000_0000_0000_0000.0);
+    }
+}
test/behavior/cast_stage1.zig
@@ -356,35 +356,6 @@ test "vector casts" {
     comptime try S.doTheTestFloat();
 }
 
-test "comptime_int @intToFloat" {
-    {
-        const result = @intToFloat(f16, 1234);
-        try expect(@TypeOf(result) == f16);
-        try expect(result == 1234.0);
-    }
-    {
-        const result = @intToFloat(f32, 1234);
-        try expect(@TypeOf(result) == f32);
-        try expect(result == 1234.0);
-    }
-    {
-        const result = @intToFloat(f64, 1234);
-        try expect(@TypeOf(result) == f64);
-        try expect(result == 1234.0);
-    }
-    {
-        const result = @intToFloat(f128, 1234);
-        try expect(@TypeOf(result) == f128);
-        try expect(result == 1234.0);
-    }
-    // big comptime_int (> 64 bits) to f128 conversion
-    {
-        const result = @intToFloat(f128, 0x1_0000_0000_0000_0000);
-        try expect(@TypeOf(result) == f128);
-        try expect(result == 0x1_0000_0000_0000_0000.0);
-    }
-}
-
 test "@floatCast cast down" {
     {
         var double: f64 = 0.001534;