Skip to content

Commit

Permalink
Fix unexpected truncation behavior with comptime_int larger than u64 …
Browse files Browse the repository at this point in the history
…range (#9303)

Closes #9299
  • Loading branch information
leesongun committed Jul 6, 2021
1 parent 6c11e9b commit cff7ece
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/stage1/bigint.cpp
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions test/behavior/truncate.zig
Expand Up @@ -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);
}

0 comments on commit cff7ece

Please sign in to comment.