[flang] Check that DO index variables are definable
authorPeter Klausler <pklausler@nvidia.com>
Tue, 10 Jan 2023 23:53:12 +0000 (15:53 -0800)
committerPeter Klausler <pklausler@nvidia.com>
Sun, 29 Jan 2023 22:27:26 +0000 (14:27 -0800)
We're letting immutable objects appear as DO index variables;
catch and diagnose this error.

Differential Revision: https://reviews.llvm.org/D142767

flang/lib/Semantics/check-do-forall.cpp
flang/test/Semantics/definable03.f90 [new file with mode: 0644]

index 3af15a6..65b2986 100644 (file)
@@ -7,6 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "check-do-forall.h"
+#include "definable.h"
 #include "flang/Common/template.h"
 #include "flang/Evaluate/call.h"
 #include "flang/Evaluate/expression.h"
@@ -486,6 +487,14 @@ private:
       if (!IsVariableName(*symbol)) {
         context_.Say(
             sourceLocation, "DO control must be an INTEGER variable"_err_en_US);
+      } else if (auto why{WhyNotDefinable(sourceLocation,
+                     context_.FindScope(sourceLocation), DefinabilityFlags{},
+                     *symbol)}) {
+        context_
+            .Say(sourceLocation,
+                "'%s' may not be used as a DO variable"_err_en_US,
+                symbol->name())
+            .Attach(std::move(*why));
       } else {
         const DeclTypeSpec *symType{symbol->GetType()};
         if (!symType) {
diff --git a/flang/test/Semantics/definable03.f90 b/flang/test/Semantics/definable03.f90
new file mode 100644 (file)
index 0000000..736a22f
--- /dev/null
@@ -0,0 +1,8 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+subroutine sub(j)
+  integer, intent(in) :: j
+  !ERROR: 'j' may not be used as a DO variable
+  !BECAUSE: 'j' is an INTENT(IN) dummy argument
+  do j = 1, 10
+  end do
+end