Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,6 @@
- Fixed a bug where the formatter would remove the `@deprecated` attribute from
constants.
([Surya Rose](https://github.com/GearsDatapacks))

- Fix invalid JavaScript codegen in cases where underscores follow a decimal.
([Patrick Dewey](https://github.com/ptdewey))
4 changes: 2 additions & 2 deletions compiler-core/src/javascript/decision.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
INDENT, bit_array_segment_int_value_to_bytes,
expression::{self, Generator, Ordering, float},
expression::{self, Generator, Ordering, float, float_from_value},
};
use crate::{
ast::{AssignmentKind, Endianness, SrcSpan, TypedClause, TypedExpr, TypedPattern},
Expand Down Expand Up @@ -1024,7 +1024,7 @@ impl<'generator, 'module, 'a> Variables<'generator, 'module, 'a> {
RuntimeCheck::String { value: expected } => docvec![value, equality, string(expected)],
RuntimeCheck::Float {
float_value: expected,
} => docvec![value, equality, expected.value()],
} => docvec![value, equality, float_from_value(expected.value())],
RuntimeCheck::Int {
int_value: expected,
} => docvec![value, equality, expected.clone()],
Expand Down
18 changes: 17 additions & 1 deletion compiler-core/src/javascript/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl<'module, 'a> Generator<'module, 'a> {
TypedExpr::String { value, .. } => string(value),

TypedExpr::Int { value, .. } => int(value),
TypedExpr::Float { value, .. } => float(value),
TypedExpr::Float { float_value, .. } => float_from_value(float_value.value()),

TypedExpr::List { elements, tail, .. } => {
self.not_in_tail_position(Some(Ordering::Strict), |this| match tail {
Expand Down Expand Up @@ -2353,6 +2353,22 @@ pub fn float(value: &str) -> Document<'_> {
out.to_doc()
}

pub fn float_from_value(value: f64) -> Document<'static> {
if value.is_infinite() {
if value.is_sign_positive() {
"Infinity".to_doc()
} else {
"-Infinity".to_doc()
}
} else if value.is_nan() {
// NOTE: this case is probably unnecessary, as this function is only
// invoked with `LiteralFloatValue` values, which cannot be nan.
"NaN".to_doc()
} else {
value.to_doc()
}
}
Comment on lines +2356 to +2370
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fix for the JavaScript infinity special case


/// The context where the constant expression is used, it might be inside a
/// function call, or in the definition of another constant.
///
Expand Down
50 changes: 50 additions & 0 deletions compiler-core/src/javascript/tests/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,56 @@ pub fn main() {
);
}

#[test]
fn underscore_after_decimal_point() {
assert_js!(
"
pub fn main() {
0._1
}
"
);
}

#[test]
fn underscore_after_decimal_point_case_statement() {
assert_js!(
"
pub fn main(x) {
case x {
0._1 -> \"bar\"
_ -> \"foo\"
}
}
"
);
}

#[test]
fn inf_float_case_statement() {
assert_js!(
"
pub fn main(x) {
case x {
100.001e123_456_789 -> \"bar\"
_ -> \"foo\"
}
}
"
);
}

#[test]
fn division_inf_by_inf_float() {
assert_js!(
"
pub fn main(x) {
-100.001e123_456_789 /. 100.001e123_456_789
}
"
);
}

#[test]
fn division_by_zero_float() {
assert_js!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub fn main(x) {\n -100.001e123_456_789 /. 100.001e123_456_789\n}\n"
---
----- SOURCE CODE

pub fn main(x) {
-100.001e123_456_789 /. 100.001e123_456_789
}


----- COMPILED JAVASCRIPT
export function main(x) {
return -Infinity / Infinity;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
assertion_line: 23
expression: "\npub fn go() {\n 1.5\n 2.0\n -0.1\n 1.\n}\n"
snapshot_kind: text
---
----- SOURCE CODE

Expand All @@ -19,5 +17,5 @@ export function go() {
1.5;
2.0;
-0.1;
return 1.;
return 1.0;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
assertion_line: 37
expression: "\npub fn go() {\n 0.01e-1\n 0.01e-0\n -10.01e-1\n -10.01e-0\n 100.001e523\n -100.001e-523\n 100.001e123_456_789\n -100.001e-123_456_789\n}\n"
snapshot_kind: text
---
----- SOURCE CODE

Expand All @@ -20,12 +18,12 @@ pub fn go() {

----- COMPILED JAVASCRIPT
export function go() {
0.01e-1;
0.01e-0;
-10.01e-1;
-10.01e-0;
100.001e523;
-100.001e-523;
100.001e123_456_789;
return -100.001e-123_456_789;
0.001;
0.01;
-1.001;
-10.01;
Infinity;
-0.0;
Infinity;
return -0.0;
}
Copy link
Contributor Author

@ptdewey ptdewey Nov 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the infinity issue also occurs in the case statements after #5118.

Ah! What code results in that?

A very large float, as seen in this test:

case x {
  100.001e123_456_789 -> "bar"
    _ -> "foo"
  }

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you share a full reproduction please? I couldn't reproduce it with that snippet.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generates invalid code for me with gleam build -tjs

pub fn main(n: Float) {
  case n {
    100.001e123_456_789 -> "wibble"
    _ -> "wobble"
  }
}

The code indeed has that inf:

export function main(n) {
  if (n === inf) {
    return "wibble";
  } else {
    return "wobble";
  }
}

This happens on main

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, the issue is reproducible on main, but I fixed it in f3e449e in this PR (see below).

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub fn main(x) {\n case x {\n 100.001e123_456_789 -> \"bar\"\n _ -> \"foo\"\n }\n}\n"
---
----- SOURCE CODE

pub fn main(x) {
case x {
100.001e123_456_789 -> "bar"
_ -> "foo"
}
}


----- COMPILED JAVASCRIPT
export function main(x) {
if (x === Infinity) {
return "bar";
} else {
return "foo";
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
assertion_line: 264
expression: "\npub fn main() {\n 09_179.1\n}\n"
snapshot_kind: text
---
----- SOURCE CODE

Expand All @@ -13,5 +11,5 @@ pub fn main() {

----- COMPILED JAVASCRIPT
export function main() {
return 9_179.1;
return 9179.1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub fn main() {\n 0._1\n}\n"
---
----- SOURCE CODE

pub fn main() {
0._1
}


----- COMPILED JAVASCRIPT
export function main() {
return 0.1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
source: compiler-core/src/javascript/tests/numbers.rs
expression: "\npub fn main(x) {\n case x {\n 0._1 -> \"bar\"\n _ -> \"foo\"\n }\n}\n"
---
----- SOURCE CODE

pub fn main(x) {
case x {
0._1 -> "bar"
_ -> "foo"
}
}


----- COMPILED JAVASCRIPT
export function main(x) {
if (x === 0.1) {
return "bar";
} else {
return "foo";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub fn go() {

----- COMPILED JAVASCRIPT
export function go() {
return 111111111111111111111111111111. / 22222222222222222222222222222222222.;
return 1.111111111111111e29 / 2.222222222222222e34;
}
Loading