Skip to content

Commit 573bd5d

Browse files
committed
✨ Use f64 for float comparison check
1 parent 1b6d00b commit 573bd5d

17 files changed

+292
-4
lines changed

compiler-core/src/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,11 +1702,11 @@ fn pattern_and_expression_are_the_same(pattern: &TypedPattern, expression: &Type
17021702
// `"wibble" -> "wibble"`
17031703
(
17041704
TypedPattern::Float {
1705-
value: pattern_value,
1705+
float_value: pattern_value,
17061706
..
17071707
},
1708-
TypedExpr::Float { value, .. },
1709-
) => pattern_value == value,
1708+
TypedExpr::Float { float_value, .. },
1709+
) => pattern_value == float_value,
17101710
(TypedPattern::Float { .. }, _) => false,
17111711

17121712
(

compiler-core/src/parse.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4730,3 +4730,15 @@ impl NotNan {
47304730
}
47314731

47324732
impl Eq for NotNan {}
4733+
impl Ord for NotNan {
4734+
fn cmp(&self, other: &Self) -> Ordering {
4735+
self.0
4736+
.partial_cmp(&other.0)
4737+
.expect("Only NaN comparisons should fail")
4738+
}
4739+
}
4740+
impl PartialOrd for NotNan {
4741+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
4742+
Some(self.cmp(other))
4743+
}
4744+
}

compiler-core/src/type_/expression.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1945,6 +1945,22 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
19451945
}
19461946
}
19471947

1948+
(
1949+
TypedExpr::Float { float_value: n, .. },
1950+
op,
1951+
TypedExpr::Float { float_value: m, .. },
1952+
) => match op {
1953+
BinOp::LtFloat if n < m => ComparisonOutcome::AlwaysSucceeds,
1954+
BinOp::LtFloat => ComparisonOutcome::AlwaysFails,
1955+
BinOp::LtEqFloat if n <= m => ComparisonOutcome::AlwaysSucceeds,
1956+
BinOp::LtEqFloat => ComparisonOutcome::AlwaysFails,
1957+
BinOp::GtFloat if n > m => ComparisonOutcome::AlwaysSucceeds,
1958+
BinOp::GtFloat => ComparisonOutcome::AlwaysFails,
1959+
BinOp::GtEqFloat if n >= m => ComparisonOutcome::AlwaysSucceeds,
1960+
BinOp::GtEqFloat => ComparisonOutcome::AlwaysFails,
1961+
_ => return,
1962+
},
1963+
19481964
_ => return,
19491965
};
19501966

@@ -5198,7 +5214,7 @@ fn static_compare(one: &TypedExpr, other: &TypedExpr) -> StaticComparison {
51985214
}
51995215
}
52005216

5201-
(TypedExpr::Float { value: n, .. }, TypedExpr::Float { value: m, .. }) => {
5217+
(TypedExpr::Float { float_value: n, .. }, TypedExpr::Float { float_value: m, .. }) => {
52025218
if n == m {
52035219
StaticComparison::CertainlyEqual
52045220
} else {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "pub fn main() { 1.0 == 1.0 }"
4+
---
5+
----- SOURCE CODE
6+
pub fn main() { 1.0 == 1.0 }
7+
8+
----- WARNING
9+
warning: Redundant comparison
10+
┌─ /src/warning/wrn.gleam:1:17
11+
12+
1pub fn main() { 1.0 == 1.0 }
13+
^^^^^^^^^^ This is always `True`
14+
15+
This comparison is redundant since it always succeeds.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "pub fn main() { 1.0 == 2.0 }"
4+
---
5+
----- SOURCE CODE
6+
pub fn main() { 1.0 == 2.0 }
7+
8+
----- WARNING
9+
warning: Redundant comparison
10+
┌─ /src/warning/wrn.gleam:1:17
11+
12+
1pub fn main() { 1.0 == 2.0 }
13+
^^^^^^^^^^ This is always `False`
14+
15+
This comparison is redundant since it always fails.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "pub fn main() { 1.0 != 1.0 }"
4+
---
5+
----- SOURCE CODE
6+
pub fn main() { 1.0 != 1.0 }
7+
8+
----- WARNING
9+
warning: Redundant comparison
10+
┌─ /src/warning/wrn.gleam:1:17
11+
12+
1pub fn main() { 1.0 != 1.0 }
13+
^^^^^^^^^^ This is always `False`
14+
15+
This comparison is redundant since it always fails.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "pub fn main() { 1.0 != 2.0 }"
4+
---
5+
----- SOURCE CODE
6+
pub fn main() { 1.0 != 2.0 }
7+
8+
----- WARNING
9+
warning: Redundant comparison
10+
┌─ /src/warning/wrn.gleam:1:17
11+
12+
1pub fn main() { 1.0 != 2.0 }
13+
^^^^^^^^^^ This is always `True`
14+
15+
This comparison is redundant since it always succeeds.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "pub fn main() { 1.0 >. 2.0 }"
4+
---
5+
----- SOURCE CODE
6+
pub fn main() { 1.0 >. 2.0 }
7+
8+
----- WARNING
9+
warning: Redundant comparison
10+
┌─ /src/warning/wrn.gleam:1:17
11+
12+
1pub fn main() { 1.0 >. 2.0 }
13+
^^^^^^^^^^ This is always `False`
14+
15+
This comparison is redundant since it always fails.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "pub fn main() { 1.0 <=. 2.0 }"
4+
---
5+
----- SOURCE CODE
6+
pub fn main() { 1.0 <=. 2.0 }
7+
8+
----- WARNING
9+
warning: Redundant comparison
10+
┌─ /src/warning/wrn.gleam:1:17
11+
12+
1pub fn main() { 1.0 <=. 2.0 }
13+
^^^^^^^^^^^ This is always `True`
14+
15+
This comparison is redundant since it always succeeds.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
source: compiler-core/src/type_/tests/warnings.rs
3+
expression: "pub fn main() { 1.0 <. 2.0 }"
4+
---
5+
----- SOURCE CODE
6+
pub fn main() { 1.0 <. 2.0 }
7+
8+
----- WARNING
9+
warning: Redundant comparison
10+
┌─ /src/warning/wrn.gleam:1:17
11+
12+
1pub fn main() { 1.0 <. 2.0 }
13+
^^^^^^^^^^ This is always `True`
14+
15+
This comparison is redundant since it always succeeds.

0 commit comments

Comments
 (0)