zig

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

commit cff7ecee070df59683dbbe3ede04b494d4619f7f (tree)
parent 6c11e9bb0798f47e8945182e668854e8b225a788
Author: leesongun <12179851+leesongun@users.noreply.github.com>
Date:   Tue,  6 Jul 2021 18:42:18 +0900

Fix unexpected truncation behavior with comptime_int larger than u64 range (#9303)

Closes #9299
Diffstat:
Msrc/stage1/bigint.cpp | 5++++-
Mtest/behavior/truncate.zig | 2++
2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/src/stage1/bigint.cpp b/src/stage1/bigint.cpp @@ -88,8 +88,11 @@ static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count) size_t digits_to_copy = bit_count / 64; size_t leftover_bits = bit_count % 64; dest->digit_count = digits_to_copy + ((leftover_bits == 0) ? 0 : 1); - if (dest->digit_count == 1 && leftover_bits == 0) { + if (dest->digit_count == 1) { dest->data.digit = op_digits[0]; + if (leftover_bits != 0) { + dest->data.digit &= (1ULL << leftover_bits) - 1; + } if (dest->data.digit == 0) dest->digit_count = 0; return; } diff --git a/test/behavior/truncate.zig b/test/behavior/truncate.zig @@ -54,4 +54,6 @@ test "truncate on comptime integer" { try expect(y == 0xabcd); var z = @truncate(i16, -65537); try expect(z == -1); + var w = @truncate(u1, 1 << 100); + try expect(w == 0); }