authorgravatar for 12179851+leesongun@users.noreply.github.comleesongun <12179851+leesongun@users.noreply.github.com> 2021-07-06 18:42:18+09:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-07-06 12:42:18+03:00
logcff7ecee070df59683dbbe3ede04b494d4619f7f
treeebf345b6e108996decb7ae817687249c485fa525
parent6c11e9bb0798f47e8945182e668854e8b225a788
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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

Closes #9299

2 files changed, 6 insertions(+), 1 deletions(-)

src/stage1/bigint.cpp+4-1
......@@ -88,8 +88,11 @@ static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count)
8888 size_t digits_to_copy = bit_count / 64;
8989 size_t leftover_bits = bit_count % 64;
9090 dest->digit_count = digits_to_copy + ((leftover_bits == 0) ? 0 : 1);
91 if (dest->digit_count == 1 && leftover_bits == 0) {
91 if (dest->digit_count == 1) {
9292 dest->data.digit = op_digits[0];
93 if (leftover_bits != 0) {
94 dest->data.digit &= (1ULL << leftover_bits) - 1;
95 }
9396 if (dest->data.digit == 0) dest->digit_count = 0;
9497 return;
9598 }
test/behavior/truncate.zig+2
......@@ -54,4 +54,6 @@ test "truncate on comptime integer" {
5454 try expect(y == 0xabcd);
5555 var z = @truncate(i16, -65537);
5656 try expect(z == -1);
57 var w = @truncate(u1, 1 << 100);
58 try expect(w == 0);
5759}