Skip to content

Commit 67028c9

Browse files
fruno-bulaxlpil
authored andcommitted
🐛 Use f64 for float comparison check
1 parent e1b6e6f commit 67028c9

18 files changed

+314
-4
lines changed

compiler-core/src/ast.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,11 +1739,11 @@ fn pattern_and_expression_are_the_same(pattern: &TypedPattern, expression: &Type
17391739
// `"wibble" -> "wibble"`
17401740
(
17411741
TypedPattern::Float {
1742-
value: pattern_value,
1742+
float_value: pattern_value,
17431743
..
17441744
},
1745-
TypedExpr::Float { value, .. },
1746-
) => pattern_value == value,
1745+
TypedExpr::Float { float_value, .. },
1746+
) => pattern_value == float_value,
17471747
(TypedPattern::Float { .. }, _) => false,
17481748

17491749
(

compiler-core/src/parse.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4751,3 +4751,17 @@ impl LiteralFloatValue {
47514751
}
47524752

47534753
impl Eq for LiteralFloatValue {}
4754+
4755+
impl Ord for LiteralFloatValue {
4756+
fn cmp(&self, other: &Self) -> Ordering {
4757+
self.0
4758+
.partial_cmp(&other.0)
4759+
.expect("Only NaN comparisons should fail")
4760+
}
4761+
}
4762+
4763+
impl PartialOrd for LiteralFloatValue {
4764+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
4765+
Some(self.cmp(other))
4766+
}
4767+
}

compiler-core/src/type_/expression.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1964,6 +1964,22 @@ impl<'a, 'b> ExprTyper<'a, 'b> {
19641964
}
19651965
}
19661966

1967+
(
1968+
TypedExpr::Float { float_value: n, .. },
1969+
op,
1970+
TypedExpr::Float { float_value: m, .. },
1971+
) => match op {
1972+
BinOp::LtFloat if n < m => ComparisonOutcome::AlwaysSucceeds,
1973+
BinOp::LtFloat => ComparisonOutcome::AlwaysFails,
1974+
BinOp::LtEqFloat if n <= m => ComparisonOutcome::AlwaysSucceeds,
1975+
BinOp::LtEqFloat => ComparisonOutcome::AlwaysFails,
1976+
BinOp::GtFloat if n > m => ComparisonOutcome::AlwaysSucceeds,
1977+
BinOp::GtFloat => ComparisonOutcome::AlwaysFails,
1978+
BinOp::GtEqFloat if n >= m => ComparisonOutcome::AlwaysSucceeds,
1979+
BinOp::GtEqFloat => ComparisonOutcome::AlwaysFails,
1980+
_ => return,
1981+
},
1982+
19671983
_ => return,
19681984
};
19691985

@@ -5222,7 +5238,7 @@ fn static_compare(one: &TypedExpr, other: &TypedExpr) -> StaticComparison {
52225238
}
52235239
}
52245240

5225-
(TypedExpr::Float { value: n, .. }, TypedExpr::Float { value: m, .. }) => {
5241+
(TypedExpr::Float { float_value: n, .. }, TypedExpr::Float { float_value: m, .. }) => {
52265242
if n == m {
52275243
StaticComparison::CertainlyEqual
52285244
} 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)