commit d49d6f0cde782f4ec3c1d623d58644c3e51a6ce9 (tree)
parent 7151d72532ebc11fe72fda91d156082bd8c1761b
Author: Andrew Kelley <superjoe30@gmail.com>
Date: Mon, 18 Jun 2018 11:04:18 -0400
fix compiler crash when using @intToFloat with float literal
closes #1132
Diffstat:
3 files changed, 20 insertions(+), 0 deletions(-)
diff --git a/src/ir.cpp b/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;
diff --git a/test/cases/cast.zig b/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);
+}
diff --git a/test/compile_errors.zig b/test/compile_errors.zig
@@ -2,6 +2,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 {
\\ var x: i32 = 1234;