From ea4973f2068abfbfd637ed446c18fe2191301cad Mon Sep 17 00:00:00 2001 From: Peter Steinfeld Date: Wed, 18 Nov 2020 12:38:29 -0800 Subject: [PATCH] [flang] Improve error message on bad LOGICAL compare operations When comparing LOGICAL operands using ".eq." or ".ne." we were not guiding users to the ".eqv." and ".neqv." operations. Differential Revision: https://reviews.llvm.org/D91736 --- flang/lib/Semantics/expression.cpp | 11 +++++++++-- flang/test/Semantics/resolve98.f90 | 13 +++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 flang/test/Semantics/resolve98.f90 diff --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp index 98f0c36..364847c 100644 --- a/flang/lib/Semantics/expression.cpp +++ b/flang/lib/Semantics/expression.cpp @@ -2450,11 +2450,18 @@ MaybeExpr RelationHelper(ExpressionAnalyzer &context, RelationalOperator opr, "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 index 0000000..dede042 --- /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 -- 2.7.4