-
-
Notifications
You must be signed in to change notification settings - Fork 657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Try to compile time error when trying to make non byte aligned array on js #3111
base: main
Are you sure you want to change the base?
Conversation
69c73e8
to
3775b34
Compare
We already de facto do not support this as seen in these lines of the Javascript template: gleam/compiler-core/templates/prelude.mjs Lines 164 to 167 in da0d82c
So this change will just make that more explicit in certain circumstances |
Also, where should I add tests for these? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!!
I would put the tests in https://github.com/gleam-lang/gleam/blob/main/compiler-core/src/javascript/tests/bit_arrays.rs
return toBitArray([sizedInt(256, 4)]); | ||
} | ||
|
||
import { toBitArray } from "../gleam.mjs"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happened to these two tests?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This snapshot correspond to this test:
gleam/compiler-core/src/javascript/tests/bit_arrays.rs
Lines 70 to 78 in f863f0b
fn explicit_sized() { | |
assert_js!( | |
r#" | |
fn go() { | |
<<256:size(4)>> | |
} | |
"#, | |
); | |
} |
Since the size of this array is being set to 4 it would not be byte aligned and it would cause a runtime error. So instead this code marks that as unsupported and so removes the function it is contained in.
I guess I can rewrite these tests to have a size which is a multiple of 8 instead and also add in some new tests which use not byte aligned bit arrays.
…on js Try to detect when someone tries to make a non byte aligned array on Javascript. This compile time error still fails to trigger if the size value is a variable though. So we still need the runtime error in the Javascript template too. Fixes gleam-lang#1591
It seems our current behaviour is to assume a negative size really means a size of 0 so might as well document this with some tests
The tests have been added. I also added extra tests which demonstrate how negatively sized bit arrays are currently handled in both Erlang and Javascript. Which is that for both targets we just assume the developer meant a bit array of size 0. |
Try to detect when someone tries to make a non byte aligned array on Javascript. This compile time error still fails to trigger if the size value is a variable though. So we still need the runtime error in the Javascript template too.
Fixes #1591