| author | |
| committer | |
| log | d49d6f0cde782f4ec3c1d623d58644c3e51a6ce9 |
| tree | c433020e6b41a8d465ed3708e5aadcef5e745a20 |
| parent | 7151d72532ebc11fe72fda91d156082bd8c1761b |
closes #11323 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 | ... | @@ -17602,6 +17602,12 @@ static TypeTableEntry *ir_analyze_instruction_int_to_float(IrAnalyze *ira, IrIns |
| 17602 | if (type_is_invalid(target->value.type)) | 17602 | if (type_is_invalid(target->value.type)) |
| 17603 | return ira->codegen->builtin_types.entry_invalid; | 17603 | return ira->codegen->builtin_types.entry_invalid; |
| 17604 | 17604 | ||
| 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 | |||
| 17605 | IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false); | 17611 | IrInstruction *result = ir_resolve_cast(ira, &instruction->base, target, dest_type, CastOpIntToFloat, false); |
| 17606 | ir_link_new_instruction(result, &instruction->base); | 17612 | ir_link_new_instruction(result, &instruction->base); |
| 17607 | return dest_type; | 17613 | return dest_type; |
test/cases/cast.zig+6| ... | @@ -414,3 +414,9 @@ test "@floatCast comptime_int and comptime_float" { | ... | @@ -414,3 +414,9 @@ test "@floatCast comptime_int and comptime_float" { |
| 414 | assert(@typeOf(result) == f32); | 414 | assert(@typeOf(result) == f32); |
| 415 | assert(result == 1234.0); | 415 | assert(result == 1234.0); |
| 416 | } | 416 | } |
| 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+8| ... | @@ -1,6 +1,14 @@ | ... | @@ -1,6 +1,14 @@ |
| 1 | const tests = @import("tests.zig"); | 1 | const tests = @import("tests.zig"); |
| 2 | 2 | ||
| 3 | pub fn addCases(cases: *tests.CompileErrorContext) void { | 3 | 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 | ); | ||
| 4 | cases.add( | 12 | cases.add( |
| 5 | "use implicit casts to assign null to non-nullable pointer", | 13 | "use implicit casts to assign null to non-nullable pointer", |
| 6 | \\export fn entry() void { | 14 | \\export fn entry() void { |