[flang-commits] [flang] ea4973f - [flang] Improve error message on bad LOGICAL compare operations

Peter Steinfeld via flang-commits flang-commits at lists.llvm.org
Wed Nov 18 13:53:46 PST 2020


Author: Peter Steinfeld
Date: 2020-11-18T13:53:29-08:00
New Revision: ea4973f2068abfbfd637ed446c18fe2191301cad

URL: https://github.com/llvm/llvm-project/commit/ea4973f2068abfbfd637ed446c18fe2191301cad
DIFF: https://github.com/llvm/llvm-project/commit/ea4973f2068abfbfd637ed446c18fe2191301cad.diff

LOG: [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

Added: 
    flang/test/Semantics/resolve98.f90

Modified: 
    flang/lib/Semantics/expression.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/expression.cpp b/flang/lib/Semantics/expression.cpp
index 98f0c3653e3b..364847ca56d9 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<DynamicType> leftType{analyzer.GetType(0)};
+    std::optional<DynamicType> 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 000000000000..dede0420757d
--- /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


        


More information about the flang-commits mailing list