diff --git a/src/ir.cpp b/src/ir.cpp index e6339a72f6..a312b501ab 100644 --- 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 index 7035740c54..f1e49c6d1f 100644 --- 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 index b51a6e9761..23337ca479 100644 --- a/test/compile_errors.zig +++ b/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 {