fix compiler crash when using @intToFloat with float literal

closes #1132
This commit is contained in:
Andrew Kelley 2018-06-18 11:04:18 -04:00
parent 7151d72532
commit d49d6f0cde
3 changed files with 20 additions and 0 deletions

View File

@ -17602,6 +17602,12 @@ static TypeTableEntry *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrIns
if (type_is_invalid(target->value.type)) if (type_is_invalid(target->value.type))
return ira->codegen->builtin_types.entry_invalid; 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); IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false);
ir_link_new_instruction(result, &instruction->base); ir_link_new_instruction(result, &instruction->base);
return dest_type; return dest_type;

View File

@ -414,3 +414,9 @@ test "@floatCast comptime_int and comptime_float" {
assert(@typeOf(result) == f32); assert(@typeOf(result) == f32);
assert(result == 1234.0); assert(result == 1234.0);
} }
test "comptime_int @intToFloat" {
const result = @intToFloat(f32, 1234);
assert(@typeOf(result) == f32);
assert(result == 1234.0);
}

View File

@ -1,6 +1,14 @@
const tests = @import("tests.zig"); const tests = @import("tests.zig");
pub fn addCases(cases: *tests.CompileErrorContext) void { 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( cases.add(
"use implicit casts to assign null to non-nullable pointer", "use implicit casts to assign null to non-nullable pointer",
\\export fn entry() void { \\export fn entry() void {