Skip to content

Commit d49d6f0

Browse files
committedJun 18, 2018
fix compiler crash when using @inttofloat with float literal
closes #1132
·
0.15.20.3.0
1 parent 7151d72 commit d49d6f0

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed
 

‎src/ir.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,3 +414,9 @@ test "@floatCast comptime_int and comptime_float" {
414414
assert(@typeOf(result) == f32);
415415
assert(result == 1234.0);
416416
}
417+
418+
test "comptime_int @intToFloat" {
419+
const result = @intToFloat(f32, 1234);
420+
assert(@typeOf(result) == f32);
421+
assert(result == 1234.0);
422+
}

‎test/compile_errors.zig‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
const tests = @import("tests.zig");
22

33
pub 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 {

0 commit comments

Comments
 (0)
Please sign in to comment.