Skip to content

Commit aef7e66

Browse files
giacomocavalierilpil
authored andcommitted
fix erlang code generation with 0 literal denominator
1 parent 3735e85 commit aef7e66

6 files changed

Lines changed: 68 additions & 13 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020

2121
([Adi Salimgereyev](https://github.com/abs0luty))
2222

23+
- Improved the code generated on the Erlang target when dividing a `Float`
24+
number by the literal number `0.0`.
25+
([Giacomo Cavalieri](https://github.com/giacomocavalieri))
26+
2327
### Build tool
2428

2529
- When adding a package that does not exist on Hex, the message is a bit

compiler-core/src/ast/typed.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::sync::OnceLock;
2-
31
use type_::{FieldMap, TypedCallArg};
42

53
use super::*;
@@ -607,7 +605,7 @@ impl TypedExpr {
607605
pub fn is_non_zero_compile_time_number(&self) -> bool {
608606
match self {
609607
Self::Int { int_value, .. } => int_value != &BigInt::ZERO,
610-
Self::Float { value, .. } => is_non_zero_number(value),
608+
Self::Float { float_value, .. } => !float_value.value().is_zero(),
611609
Self::String { .. }
612610
| Self::Block { .. }
613611
| Self::Pipeline { .. }
@@ -636,7 +634,7 @@ impl TypedExpr {
636634
pub fn is_zero_compile_time_number(&self) -> bool {
637635
match self {
638636
Self::Int { int_value, .. } => int_value == &BigInt::ZERO,
639-
Self::Float { value, .. } => !is_non_zero_number(value),
637+
Self::Float { float_value, .. } => float_value.value().is_zero(),
640638
Self::String { .. }
641639
| Self::Block { .. }
642640
| Self::Pipeline { .. }
@@ -1655,15 +1653,6 @@ pub(crate) fn pairwise_all<A>(one: &[A], other: &[A], function: impl Fn((&A, &A)
16551653
one.len() == other.len() && one.iter().zip(other).all(function)
16561654
}
16571655

1658-
fn is_non_zero_number(value: &EcoString) -> bool {
1659-
use regex::Regex;
1660-
static NON_ZERO: OnceLock<Regex> = OnceLock::new();
1661-
1662-
NON_ZERO
1663-
.get_or_init(|| Regex::new(r"[1-9]").expect("NON_ZERO regex"))
1664-
.is_match(value)
1665-
}
1666-
16671656
impl<'a> From<&'a TypedExpr> for Located<'a> {
16681657
fn from(expression: &'a TypedExpr) -> Self {
16691658
Located::Expression {

compiler-core/src/erlang.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,8 @@ fn statement_sequence<'a>(statements: &'a [TypedStatement], env: &mut Env<'a>) -
11151115
fn float_div<'a>(left: &'a TypedExpr, right: &'a TypedExpr, env: &mut Env<'a>) -> Document<'a> {
11161116
if right.is_non_zero_compile_time_number() {
11171117
return binop_exprs(left, "/", right, env);
1118+
} else if right.is_zero_compile_time_number() {
1119+
return "+0.0".to_doc();
11181120
}
11191121

11201122
let left = expr(left, env);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: compiler-core/src/erlang/tests.rs
3+
expression: "\npub fn main() {\n 1.0 /. 2.0\n} "
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
1.0 /. 2.0
9+
}
10+
11+
----- COMPILED ERLANG
12+
-module(my@mod).
13+
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
14+
-define(FILEPATH, "project/test/my/mod.gleam").
15+
-export([main/0]).
16+
17+
-file("project/test/my/mod.gleam", 2).
18+
-spec main() -> float().
19+
main() ->
20+
1.0 / 2.0.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
source: compiler-core/src/erlang/tests.rs
3+
expression: "\npub fn main() {\n 1.0 /. 0.0\n} "
4+
---
5+
----- SOURCE CODE
6+
7+
pub fn main() {
8+
1.0 /. 0.0
9+
}
10+
11+
----- COMPILED ERLANG
12+
-module(my@mod).
13+
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
14+
-define(FILEPATH, "project/test/my/mod.gleam").
15+
-export([main/0]).
16+
17+
-file("project/test/my/mod.gleam", 2).
18+
-spec main() -> float().
19+
main() ->
20+
+0.0.

compiler-core/src/erlang/tests.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,3 +1013,23 @@ pub fn main() {
10131013
} "
10141014
);
10151015
}
1016+
1017+
#[test]
1018+
fn float_division_by_literal_zero() {
1019+
assert_erl!(
1020+
"
1021+
pub fn main() {
1022+
1.0 /. 0.0
1023+
} "
1024+
);
1025+
}
1026+
1027+
#[test]
1028+
fn float_division_by_literal_non_zero() {
1029+
assert_erl!(
1030+
"
1031+
pub fn main() {
1032+
1.0 /. 2.0
1033+
} "
1034+
);
1035+
}

0 commit comments

Comments
 (0)