authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-05 18:01:48-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-09-05 18:01:48-04:00
logb517bea734c0141fcda6b267a8f0de34cddc65f8
tree64c287ecaa26f47b05f06fee0b234c9c18ab0eb9
parent768d1fc539e425280acba5b30256e3b1267ddfbb
signaturelock-open Commit is signed but in an unrecognized format.

allow comptime_int to @floatToInt


3 files changed, 36 insertions(+), 18 deletions(-)

src/ir.cpp+8
......@@ -18497,6 +18497,14 @@ static TypeTableEntry *ir_analyze_instruction_float_to_int(IrAnalyze *ira, IrIns
1849718497 if (type_is_invalid(target->value.type))
1849818498 return ira->codegen->builtin_types.entry_invalid;
1849918499
18500 if (target->value.type->id == TypeTableEntryIdComptimeInt) {
18501 IrInstruction *casted_value = ir_implicit_cast(ira, target, dest_type);
18502 if (type_is_invalid(casted_value->value.type))
18503 return ira->codegen->builtin_types.entry_invalid;
18504 ir_link_new_instruction(casted_value, &instruction->base);
18505 return casted_value->value.type;
18506 }
18507
1850018508 if (target->value.type->id != TypeTableEntryIdFloat && target->value.type->id != TypeTableEntryIdComptimeFloat) {
1850118509 ir_add_error(ira, instruction->target, buf_sprintf("expected float type, found '%s'",
1850218510 buf_ptr(&target->value.type->name)));
test/cases/cast.zig+1
......@@ -356,6 +356,7 @@ fn testFloatToInts() void {
356356 expectFloatToInt(f32, 255.1, u8, 255);
357357 expectFloatToInt(f32, 127.2, i8, 127);
358358 expectFloatToInt(f32, -128.2, i8, -128);
359 expectFloatToInt(comptime_int, 1234, i16, 1234);
359360}
360361
361362fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) void {
test/compile_errors.zig+27-18
......@@ -1,6 +1,33 @@
11const tests = @import("tests.zig");
22
33pub 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 );
12
13 cases.add(
14 "non float passed to @floatToInt",
15 \\export fn entry() void {
16 \\ const x = @floatToInt(i32, i32(54));
17 \\}
18 ,
19 ".tmp_source.zig:2:35: error: expected float type, found 'i32'",
20 );
21
22 cases.add(
23 "out of range comptime_int passed to @floatToInt",
24 \\export fn entry() void {
25 \\ const x = @floatToInt(i8, 200);
26 \\}
27 ,
28 ".tmp_source.zig:2:31: error: integer value 200 cannot be implicitly casted to type 'i8'",
29 );
30
431 cases.add(
532 "load too many bytes from comptime reinterpreted pointer",
633 \\export fn entry() void {
......@@ -486,24 +513,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
486513 ".tmp_source.zig:2:20: error: return type cannot be opaque",
487514 );
488515
489 cases.add(
490 "non int passed to @intToFloat",
491 \\export fn entry() void {
492 \\ const x = @intToFloat(f32, 1.1);
493 \\}
494 ,
495 ".tmp_source.zig:2:32: error: expected int type, found 'comptime_float'",
496 );
497
498 cases.add(
499 "non float passed to @floatToInt",
500 \\export fn entry() void {
501 \\ const x = @floatToInt(i32, 54);
502 \\}
503 ,
504 ".tmp_source.zig:2:32: error: expected float type, found 'comptime_int'",
505 );
506
507516 cases.add(
508517 "use implicit casts to assign null to non-nullable pointer",
509518 \\export fn entry() void {