zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit 29b3be4f2f30f756ef0317f79c349f5565b04d19 (tree)
parent 7456389ef3c29e37d5d9bc7f8365db3217b3f60e
Author: Vexu <git@vexu.eu>
Date:   Tue, 12 May 2020 15:20:03 +0300

Merge pull request #5319 from Vexu/float-fix

Fix intToFloat on comptime_floats
Diffstat:
Msrc/ir.cpp | 12+++++++-----
Mtest/stage1/behavior/cast.zig | 9+++++++++
2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/ir.cpp b/src/ir.cpp @@ -12760,9 +12760,7 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr, const_val->type = new_type; break; case CastOpIntToFloat: - { - assert(new_type->id == ZigTypeIdFloat); - + if (new_type->id == ZigTypeIdFloat) { BigFloat bigfloat; bigfloat_init_bigint(&bigfloat, &other_val->data.x_bigint); switch (new_type->data.floating.bit_count) { @@ -12783,9 +12781,13 @@ static bool eval_const_expr_implicit_cast(IrAnalyze *ira, IrInst *source_instr, default: zig_unreachable(); } - const_val->special = ConstValSpecialStatic; - break; + } else if (new_type->id == ZigTypeIdComptimeFloat) { + bigfloat_init_bigint(&const_val->data.x_bigfloat, &other_val->data.x_bigint); + } else { + zig_unreachable(); } + const_val->special = ConstValSpecialStatic; + break; case CastOpFloatToInt: float_init_bigint(&const_val->data.x_bigint, other_val); if (new_type->id == ZigTypeIdInt) { diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig @@ -827,3 +827,12 @@ test "peer type resolve array pointer and unknown pointer" { comptime expect(@TypeOf(&const_array, const_ptr) == [*]const u8); comptime expect(@TypeOf(const_ptr, &const_array) == [*]const u8); } + +test "comptime float casts" { + const a = @intToFloat(comptime_float, 1); + expect(a == 1); + expect(@TypeOf(a) == comptime_float); + const b = @floatToInt(comptime_int, 2); + expect(b == 2); + expect(@TypeOf(b) == comptime_int); +}