diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp --- a/flang/lib/Semantics/expression.cpp +++ b/flang/lib/Semantics/expression.cpp @@ -2450,11 +2450,18 @@ "operator"_err_en_US); return std::nullopt; } - analyzer.ConvertBOZ(0, analyzer.GetType(1)); - analyzer.ConvertBOZ(1, analyzer.GetType(0)); + std::optional leftType{analyzer.GetType(0)}; + std::optional rightType{analyzer.GetType(1)}; + analyzer.ConvertBOZ(0, rightType); + analyzer.ConvertBOZ(1, leftType); if (analyzer.IsIntrinsicRelational(opr)) { return AsMaybeExpr(Relate(context.GetContextualMessages(), opr, analyzer.MoveExpr(0), analyzer.MoveExpr(1))); + } else if (leftType && leftType->category() == TypeCategory::Logical && + rightType && rightType->category() == TypeCategory::Logical) { + context.Say("LOGICAL operands must be compared using .EQV. or " + ".NEQV."_err_en_US); + return std::nullopt; } else { return analyzer.TryDefinedOp(opr, "Operands of %s must have comparable types; have %s and %s"_err_en_US); diff --git a/flang/test/Semantics/resolve98.f90 b/flang/test/Semantics/resolve98.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Semantics/resolve98.f90 @@ -0,0 +1,13 @@ +! RUN: %S/test_errors.sh %s %t %f18 + +! Errors when comparing LOGICAL operands + +program testCompare + logical flag1, flag2 + if (flag1 .eqv. .false.) continue + if (flag1 .neqv. flag2) continue + !ERROR: LOGICAL operands must be compared using .EQV. or .NEQV. + if (flag1 .eq. .false.) continue + !ERROR: LOGICAL operands must be compared using .EQV. or .NEQV. + if (flag1 .ne. flag2) continue +end program testCompare