Skip to content

Commit

Permalink
allow value == value used for NAN checks
Browse files Browse the repository at this point in the history
  • Loading branch information
orpuente-MS committed Jan 15, 2025
1 parent 7b0f6c1 commit db277c4
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
13 changes: 13 additions & 0 deletions compiler/qsc_linter/src/lints/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,19 @@ struct DoubleEquality {

impl HirLintPass for DoubleEquality {
fn check_expr(&mut self, expr: &Expr, buffer: &mut Vec<Lint>, _compilation: Compilation) {
// Ignore the case where the identifier is being compared for equlity with itself,
// Since this is used to check if the double is `NaN`. This will be compiled to
// fcmp oeq value value
// in LLVM because we only implemented the ord side of fcmp.
// See https://llvm.org/docs/LangRef.html#id313 for more details.
if let ExprKind::BinOp(BinOp::Eq, ref lhs, ref rhs) = expr.kind {
if let (ExprKind::Var(lhs_id, _), ExprKind::Var(rhs_id, _)) = (&lhs.kind, &rhs.kind) {
if lhs_id == rhs_id {
return;
}
}
}

if let ExprKind::BinOp(BinOp::Eq | BinOp::Neq, ref lhs, ref rhs) = expr.kind {
if matches!(lhs.ty, Ty::Prim(Prim::Double)) && matches!(rhs.ty, Ty::Prim(Prim::Double))
{
Expand Down
16 changes: 16 additions & 0 deletions compiler/qsc_linter/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ fn double_equality() {
);
}

#[test]
fn check_double_equality_with_itself_is_allowed_for_nan_check() {
check(
&wrap_in_callable(
r#"
let a = 1.0;
let is_nan = not (a == a);
"#,
CallableKind::Function,
),
&expect![[r#"
[]
"#]],
);
}

#[test]
fn double_inequality() {
check(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,5 @@ internal function AllEigenVectorsAsBitString(eigenVectors : Double[][], precisio
}

internal function IsNaN(value : Double) : Bool {
value != value
not (value == value)
}

0 comments on commit db277c4

Please sign in to comment.