[PATCH] D91736: [flang] Improve error message on bad LOGICAL compare operations

Pete Steinfeld via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 18 12:44:02 PST 2020


PeteSteinfeld created this revision.
PeteSteinfeld added reviewers: klausler, tskeith.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
PeteSteinfeld requested review of this revision.

When comparing LOGICAL operands using ".eq." or ".ne." we were not
guiding users to the ".eqv." and ".neqv." operations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D91736

Files:
  flang/lib/Semantics/expression.cpp
  flang/test/Semantics/resolve98.f90


Index: flang/test/Semantics/resolve98.f90
===================================================================
--- /dev/null
+++ 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
Index: flang/lib/Semantics/expression.cpp
===================================================================
--- flang/lib/Semantics/expression.cpp
+++ 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<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);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D91736.306190.patch
Type: text/x-patch
Size: 1807 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20201118/3cabe408/attachment.bin>


More information about the llvm-commits mailing list