authorgravatar for 12179851+leesongun@users.noreply.github.comleesongun <12179851+leesongun@users.noreply.github.com> 2021-07-06 18:42:18+09:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-06 21:30:58-07:00
logceebcfa317874ea6cf0cab55cfc12ec60078273d
treef3d85b05474a426a41ff3bd385d299102843d359
parentb26ab39836ea07cdc92af430f12f0d4732433b53

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)...@@ -88,8 +88,11 @@ static void to_twos_complement(BigInt *dest, const BigInt *op, size_t bit_count)
88 size_t digits_to_copy = bit_count / 64;88 size_t digits_to_copy = bit_count / 64;
89 size_t leftover_bits = bit_count % 64;89 size_t leftover_bits = bit_count % 64;
90 dest->digit_count = digits_to_copy + ((leftover_bits == 0) ? 0 : 1);90 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) {
92 dest->data.digit = op_digits[0];92 dest->data.digit = op_digits[0];
93 if (leftover_bits != 0) {
94 dest->data.digit &= (1ULL << leftover_bits) - 1;
95 }
93 if (dest->data.digit == 0) dest->digit_count = 0;96 if (dest->data.digit == 0) dest->digit_count = 0;
94 return;97 return;
95 }98 }
test/behavior/truncate.zig+2
...@@ -54,4 +54,6 @@ test "truncate on comptime integer" {...@@ -54,4 +54,6 @@ test "truncate on comptime integer" {
54 try expect(y == 0xabcd);54 try expect(y == 0xabcd);
55 var z = @truncate(i16, -65537);55 var z = @truncate(i16, -65537);
56 try expect(z == -1);56 try expect(z == -1);
57 var w = @truncate(u1, 1 << 100);
58 try expect(w == 0);
57}59}