authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-18 11:04:18-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-18 11:04:18-04:00
logd49d6f0cde782f4ec3c1d623d58644c3e51a6ce9
treec433020e6b41a8d465ed3708e5aadcef5e745a20
parent7151d72532ebc11fe72fda91d156082bd8c1761b

fix compiler crash when using @intToFloat with float literal

closes #1132

3 files changed, 20 insertions(+), 0 deletions(-)

src/ir.cpp+6
......@@ -17602,6 +17602,12 @@ static TypeTableEntry *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrIns
1760217602 if (type_is_invalid(target->value.type))
1760317603 return ira->codegen->builtin_types.entry_invalid;
1760417604
17605 if (target->value.type->id != TypeTableEntryIdInt && target->value.type->id != TypeTableEntryIdComptimeInt) {
17606 ir_add_error(ira, instruction->target, buf_sprintf("expected int type, found '%s'",
17607 buf_ptr(&target->value.type->name)));
17608 return ira->codegen->builtin_types.entry_invalid;
17609 }
17610
1760517611 IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false);
1760617612 ir_link_new_instruction(result, &instruction->base);
1760717613 return dest_type;
test/cases/cast.zig+6
......@@ -414,3 +414,9 @@ test "@floatCast comptime_int and comptime_float" {
414414 assert(@typeOf(result) == f32);
415415 assert(result == 1234.0);
416416}
417
418test "comptime_int @intToFloat" {
419 const result = @intToFloat(f32, 1234);
420 assert(@typeOf(result) == f32);
421 assert(result == 1234.0);
422}
test/compile_errors.zig+8
......@@ -1,6 +1,14 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompileErrorContext) void {
4 cases.add(
5 "non int passed to @intToFloat",
6 \\export fn entry() void {
7 \\ const x = @intToFloat(f32, 1.1);
8 \\}
9 ,
10 ".tmp_source.zig:2:32: error: expected int type, found 'comptime_float'",
11 );
412 cases.add(
513 "use implicit casts to assign null to non-nullable pointer",
614 \\export fn entry() void {