Skip to content

Commit

Permalink
Fix trailing :bytes pattern on JS giving incorrect result
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-viney authored and lpil committed Dec 30, 2024
1 parent c08eff1 commit 88fa47f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

### Compiler

- Fixed a bug on JavaScript where a trailing `:bytes` segment would give the
wrong pattern match result for a sliced bit array.

## 1.7.0-rc1 - 2024-12-29

### Compiler
Expand Down
3 changes: 2 additions & 1 deletion compiler-core/templates/prelude.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ export class BitArray {
sliceAfter(index) {
const buffer = new Uint8Array(
this.buffer.buffer,
this.buffer.byteOffset + index
this.buffer.byteOffset + index,
this.buffer.byteLength - index
);
return new BitArray(buffer);
}
Expand Down
6 changes: 6 additions & 0 deletions test/javascript_prelude/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,12 @@ assertEqual(
new BitArray(new Uint8Array([1, 2, 3])).sliceAfter(1),
new BitArray(new Uint8Array([2, 3])),
);
assertEqual(
new BitArray(new Uint8Array([1, 2, 3, 4, 5]))
.binaryFromSlice(1, 4)
.sliceAfter(1),
new BitArray(new Uint8Array([3, 4]))
);

// sizedInt()

Expand Down
8 changes: 8 additions & 0 deletions test/language/test/language_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,14 @@ fn bit_array_tests() -> List(Test) {
_ -> False
})
}),
"pattern match using `:bytes` on a sliced bit array"
|> example(fn() {
assert_equal(<<3, 4>>, {
let assert <<_, b:bytes-3, _>> = <<1, 2, 3, 4, 5>>
let assert <<_, rest:bytes>> = b
rest
})
}),
]
}

Expand Down

0 comments on commit 88fa47f

Please sign in to comment.