Skip to content

Commit

Permalink
fix compiler crash when using @inttofloat with float literal
Browse files Browse the repository at this point in the history
closes #1132
  • Loading branch information
andrewrk committed Jun 18, 2018
1 parent 7151d72 commit d49d6f0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/ir.cpp
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions test/cases/cast.zig
Expand Up @@ -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);
}
8 changes: 8 additions & 0 deletions 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 {
Expand Down

0 comments on commit d49d6f0

Please sign in to comment.