Fork of daniellemaywood.uk/gleam — Wasm codegen work
2

Configure Feed

Select the types of activity you want to include in your feed.

report invalid floating point values on both targets

author
Giacomo Cavalieri
committer
Louis Pilfold
date (Nov 24, 2025, 12:14 PM UTC) commit aafa282d parent a7953f2b change-id kvlxkkun
+81 -80
+5
CHANGELOG.md
··· 214 214 rename all its occurrences. 215 215 ([Giacomo Cavalieri](https://github.com/giacomocavalieri)) 216 216 217 + - The compiler now reports an error for literal floats that are outside the 218 + floating point representable range on both targets. Previously it would only 219 + do that when compiling on the Erlang target. 220 + ([Giacomo Cavalieri](https://github.com/giacomocavalieri)) 221 + 217 222 - Fixed a typo in the error message emitted when trying to run a module that 218 223 does not have a main function. 219 224 ([Louis Pilfold](https://github.com/lpil))
+4 -4
compiler-core/src/error.rs
··· 1558 1558 } => error 1559 1559 .iter() 1560 1560 .map(|error| match error { 1561 - TypeError::ErlangFloatUnsafe { location, .. } => Diagnostic { 1562 - title: "Float is outside Erlang's floating point range".into(), 1561 + TypeError::LiteralFloatOutOfRange { location, .. } => Diagnostic { 1562 + title: "Float outside of valid range".into(), 1563 1563 text: wrap( 1564 1564 "This float value is too large to be represented by \ 1565 - Erlang's floating point type. To avoid this error float values must be in the range \ 1566 - -1.7976931348623157e308 - 1.7976931348623157e308.", 1565 + a floating point type: float values must be in the range -1.7976931348623157e308 \ 1566 + - 1.7976931348623157e308.", 1567 1567 ), 1568 1568 hint: None, 1569 1569 level: Level::Error,
+3 -5
compiler-core/src/javascript/tests/numbers.rs
··· 1 - use crate::assert_js; 1 + use crate::{assert_js, assert_js_module_error}; 2 2 3 3 #[test] 4 4 fn int_literals() { ··· 41 41 0.01e-0 42 42 -10.01e-1 43 43 -10.01e-0 44 - 100.001e523 45 44 -100.001e-523 46 - 100.001e123_456_789 47 45 -100.001e-123_456_789 48 46 } 49 47 "#, ··· 458 456 459 457 #[test] 460 458 fn inf_float_case_statement() { 461 - assert_js!( 459 + assert_js_module_error!( 462 460 " 463 461 pub fn main(x) { 464 462 case x { ··· 472 470 473 471 #[test] 474 472 fn division_inf_by_inf_float() { 475 - assert_js!( 473 + assert_js_module_error!( 476 474 " 477 475 pub fn main(x) { 478 476 -100.001e123_456_789 /. 100.001e123_456_789
+20 -4
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__division_inf_by_inf_float.snap
··· 9 9 } 10 10 11 11 12 - ----- COMPILED JAVASCRIPT 13 - export function main(x) { 14 - return -Infinity / Infinity; 15 - } 12 + ----- ERROR 13 + error: Float outside of valid range 14 + ┌─ /src/one/two.gleam:3:3 15 + 16 + 3 │ -100.001e123_456_789 /. 100.001e123_456_789 17 + │ ^^^^^^^^^^^^^^^^^^^^ 18 + 19 + This float value is too large to be represented by a floating point type: 20 + float values must be in the range -1.7976931348623157e308 - 21 + 1.7976931348623157e308. 22 + 23 + error: Float outside of valid range 24 + ┌─ /src/one/two.gleam:3:27 25 + 26 + 3 │ -100.001e123_456_789 /. 100.001e123_456_789 27 + │ ^^^^^^^^^^^^^^^^^^^ 28 + 29 + This float value is too large to be represented by a floating point type: 30 + float values must be in the range -1.7976931348623157e308 - 31 + 1.7976931348623157e308.
+1 -5
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__float_scientific_literals.snap
··· 1 1 --- 2 2 source: compiler-core/src/javascript/tests/numbers.rs 3 - 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" 3 + expression: "\npub fn go() {\n 0.01e-1\n 0.01e-0\n -10.01e-1\n -10.01e-0\n -100.001e-523\n -100.001e-123_456_789\n}\n" 4 4 --- 5 5 ----- SOURCE CODE 6 6 ··· 9 9 0.01e-0 10 10 -10.01e-1 11 11 -10.01e-0 12 - 100.001e523 13 12 -100.001e-523 14 - 100.001e123_456_789 15 13 -100.001e-123_456_789 16 14 } 17 15 ··· 22 20 0.01; 23 21 -1.001; 24 22 -10.01; 25 - Infinity; 26 23 -0.0; 27 - Infinity; 28 24 return -0.0; 29 25 }
+10 -8
compiler-core/src/javascript/tests/snapshots/gleam_core__javascript__tests__numbers__inf_float_case_statement.snap
··· 12 12 } 13 13 14 14 15 - ----- COMPILED JAVASCRIPT 16 - export function main(x) { 17 - if (x === Infinity) { 18 - return "bar"; 19 - } else { 20 - return "foo"; 21 - } 22 - } 15 + ----- ERROR 16 + error: Float outside of valid range 17 + ┌─ /src/one/two.gleam:4:3 18 + 19 + 4 │ 100.001e123_456_789 -> "bar" 20 + │ ^^^^^^^^^^^^^^^^^^^ 21 + 22 + This float value is too large to be represented by a floating point type: 23 + float values must be in the range -1.7976931348623157e308 - 24 + 1.7976931348623157e308.
+11 -13
compiler-core/src/type_/error.rs
··· 577 577 location: SrcSpan, 578 578 }, 579 579 580 - /// Occers when any varient of a custom type is deprecated while 580 + /// Occurs when any varient of a custom type is deprecated while 581 581 /// the custom type itself is deprecated 582 582 DeprecatedVariantOnDeprecatedType { 583 583 location: SrcSpan, 584 584 }, 585 585 586 - ErlangFloatUnsafe { 586 + /// Occurs when a literal floating point has a value that is outside of the 587 + /// range representable by floats: -1.7976931348623157e308 to 588 + /// 1.7976931348623157e308. 589 + LiteralFloatOutOfRange { 587 590 location: SrcSpan, 588 591 }, 589 592 ··· 1306 1309 | Error::AllVariantsDeprecated { location } 1307 1310 | Error::EchoWithNoFollowingExpression { location } 1308 1311 | Error::DeprecatedVariantOnDeprecatedType { location } 1309 - | Error::ErlangFloatUnsafe { location } 1312 + | Error::LiteralFloatOutOfRange { location } 1310 1313 | Error::FloatOperatorOnInts { location, .. } 1311 1314 | Error::IntOperatorOnFloats { location, .. } 1312 1315 | Error::StringConcatenationWithAddInt { location } ··· 1983 1986 /// -1.7976931348623157e308 to 1.7976931348623157e308 which is the allowed range for 1984 1987 /// Erlang's floating point numbers 1985 1988 /// 1986 - pub fn check_erlang_float_safety( 1987 - value: LiteralFloatValue, 1988 - location: SrcSpan, 1989 - problems: &mut Problems, 1990 - ) { 1991 - let erl_min_float = -1.7976931348623157e308f64; 1992 - let erl_max_float = 1.7976931348623157e308f64; 1989 + pub fn check_float_safety(value: LiteralFloatValue, location: SrcSpan, problems: &mut Problems) { 1990 + let min_float = -1.7976931348623157e308f64; 1991 + let max_float = 1.7976931348623157e308f64; 1993 1992 1994 1993 let float_value = value.value(); 1995 - 1996 - if float_value < erl_min_float || float_value > erl_max_float { 1997 - problems.error(Error::ErlangFloatUnsafe { location }); 1994 + if float_value < min_float || float_value > max_float { 1995 + problems.error(Error::LiteralFloatOutOfRange { location }); 1998 1996 } 1999 1997 }
+2 -10
compiler-core/src/type_/expression.rs
··· 458 458 value, 459 459 float_value, 460 460 } => { 461 - if self.environment.target == Target::Erlang 462 - && !self.current_function_definition.has_erlang_external 463 - { 464 - check_erlang_float_safety(float_value, location, self.problems) 465 - } 466 - 461 + check_float_safety(float_value, location, self.problems); 467 462 Ok(self.infer_float(value, float_value, location)) 468 463 } 469 464 ··· 3801 3796 value, 3802 3797 float_value, 3803 3798 } => { 3804 - if self.environment.target == Target::Erlang { 3805 - check_erlang_float_safety(float_value, location, self.problems) 3806 - } 3807 - 3799 + check_float_safety(float_value, location, self.problems); 3808 3800 Ok(Constant::Float { 3809 3801 location, 3810 3802 value,
+1 -7
compiler-core/src/type_/pattern.rs
··· 753 753 float_value, 754 754 } => { 755 755 self.unify_types(type_, float(), location); 756 - 757 - if self.environment.target == Target::Erlang 758 - && !self.implementations.uses_erlang_externals 759 - { 760 - check_erlang_float_safety(float_value, location, self.problems) 761 - } 762 - 756 + check_float_safety(float_value, location, self.problems); 763 757 Pattern::Float { 764 758 location, 765 759 value,
+4 -4
compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__negative_out_of_range_erlang_float.snap
··· 6 6 -1.8e308 7 7 8 8 ----- ERROR 9 - error: Float is outside Erlang's floating point range 9 + error: Float outside of valid range 10 10 ┌─ /src/one/two.gleam:1:1 11 11 12 12 1 │ -1.8e308 13 13 │ ^^^^^^^^ 14 14 15 - This float value is too large to be represented by Erlang's floating point 16 - type. To avoid this error float values must be in the range 17 - -1.7976931348623157e308 - 1.7976931348623157e308. 15 + This float value is too large to be represented by a floating point type: 16 + float values must be in the range -1.7976931348623157e308 - 17 + 1.7976931348623157e308.
+4 -4
compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__negative_out_of_range_erlang_float_in_const.snap
··· 6 6 const x = -1.8e308 7 7 8 8 ----- ERROR 9 - error: Float is outside Erlang's floating point range 9 + error: Float outside of valid range 10 10 ┌─ /src/one/two.gleam:1:11 11 11 12 12 1 │ const x = -1.8e308 13 13 │ ^^^^^^^^ 14 14 15 - This float value is too large to be represented by Erlang's floating point 16 - type. To avoid this error float values must be in the range 17 - -1.7976931348623157e308 - 1.7976931348623157e308. 15 + This float value is too large to be represented by a floating point type: 16 + float values must be in the range -1.7976931348623157e308 - 17 + 1.7976931348623157e308.
+4 -4
compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__negative_out_of_range_erlang_float_in_pattern.snap
··· 22 22 23 23 The name `y` is not in scope here. 24 24 25 - error: Float is outside Erlang's floating point range 25 + error: Float outside of valid range 26 26 ┌─ /src/one/two.gleam:1:13 27 27 28 28 1 │ let assert [-1.8e308, b] = [x, y] 29 29 │ ^^^^^^^^ 30 30 31 - This float value is too large to be represented by Erlang's floating point 32 - type. To avoid this error float values must be in the range 33 - -1.7976931348623157e308 - 1.7976931348623157e308. 31 + This float value is too large to be represented by a floating point type: 32 + float values must be in the range -1.7976931348623157e308 - 33 + 1.7976931348623157e308.
+4 -4
compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__out_of_range_erlang_float.snap
··· 6 6 1.8e308 7 7 8 8 ----- ERROR 9 - error: Float is outside Erlang's floating point range 9 + error: Float outside of valid range 10 10 ┌─ /src/one/two.gleam:1:1 11 11 12 12 1 │ 1.8e308 13 13 │ ^^^^^^^ 14 14 15 - This float value is too large to be represented by Erlang's floating point 16 - type. To avoid this error float values must be in the range 17 - -1.7976931348623157e308 - 1.7976931348623157e308. 15 + This float value is too large to be represented by a floating point type: 16 + float values must be in the range -1.7976931348623157e308 - 17 + 1.7976931348623157e308.
+4 -4
compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__out_of_range_erlang_float_in_const.snap
··· 6 6 const x = 1.8e308 7 7 8 8 ----- ERROR 9 - error: Float is outside Erlang's floating point range 9 + error: Float outside of valid range 10 10 ┌─ /src/one/two.gleam:1:11 11 11 12 12 1 │ const x = 1.8e308 13 13 │ ^^^^^^^ 14 14 15 - This float value is too large to be represented by Erlang's floating point 16 - type. To avoid this error float values must be in the range 17 - -1.7976931348623157e308 - 1.7976931348623157e308. 15 + This float value is too large to be represented by a floating point type: 16 + float values must be in the range -1.7976931348623157e308 - 17 + 1.7976931348623157e308.
+4 -4
compiler-core/src/type_/tests/snapshots/gleam_core__type___tests__errors__out_of_range_erlang_float_in_pattern.snap
··· 22 22 23 23 The name `y` is not in scope here. 24 24 25 - error: Float is outside Erlang's floating point range 25 + error: Float outside of valid range 26 26 ┌─ /src/one/two.gleam:1:13 27 27 28 28 1 │ let assert [1.8e308, b] = [x, y] 29 29 │ ^^^^^^^ 30 30 31 - This float value is too large to be represented by Erlang's floating point 32 - type. To avoid this error float values must be in the range 33 - -1.7976931348623157e308 - 1.7976931348623157e308. 31 + This float value is too large to be represented by a floating point type: 32 + float values must be in the range -1.7976931348623157e308 - 33 + 1.7976931348623157e308.