Commit d49d6f0cde

Andrew Kelley <superjoe30@gmail.com>
2018-06-18 17:04:18
fix compiler crash when using @intToFloat with float literal
closes #1132
1 parent 7151d72
Changed files (3)
src/ir.cpp
@@ -17602,6 +17602,12 @@ static TypeTableEntry *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrIns
     if (type_is_invalid(target->value.type))
         return ira->codegen->builtin_types.entry_invalid;
 
+    if (target->value.type->id != TypeTableEntryIdInt && target->value.type->id != TypeTableEntryIdComptimeInt) {
+        ir_add_error(ira, instruction->target, buf_sprintf("expected int type, found '%s'",
+                    buf_ptr(&target->value.type->name)));
+        return ira->codegen->builtin_types.entry_invalid;
+    }
+
     IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false);
     ir_link_new_instruction(result, &instruction->base);
     return dest_type;
test/cases/cast.zig
@@ -414,3 +414,9 @@ test "@floatCast comptime_int and comptime_float" {
     assert(@typeOf(result) == f32);
     assert(result == 1234.0);
 }
+
+test "comptime_int @intToFloat" {
+    const result = @intToFloat(f32, 1234);
+    assert(@typeOf(result) == f32);
+    assert(result == 1234.0);
+}
test/compile_errors.zig
@@ -1,6 +1,14 @@
 const tests = @import("tests.zig");
 
 pub fn addCases(cases: *tests.CompileErrorContext) void {
+    cases.add(
+        "non int passed to @intToFloat",
+        \\export fn entry() void {
+        \\    const x = @intToFloat(f32, 1.1);
+        \\}
+    ,
+        ".tmp_source.zig:2:32: error: expected int type, found 'comptime_float'",
+    );
     cases.add(
         "use implicit casts to assign null to non-nullable pointer",
         \\export fn entry() void {