[flang-commits] [PATCH] D142767: [flang] Check that DO index variables are definable

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Fri Jan 27 09:52:13 PST 2023


klausler created this revision.
klausler added a reviewer: vdonaldson.
klausler added a project: Flang.
Herald added subscribers: sunshaoce, jdoerfert, arphaman.
Herald added a reviewer: sscalpone.
Herald added a project: All.
klausler requested review of this revision.

We're letting immutable objects appear as DO index variables;
catch and diagnose this error.


https://reviews.llvm.org/D142767

Files:
  flang/lib/Semantics/check-do-forall.cpp
  flang/test/Semantics/definable03.f90


Index: flang/test/Semantics/definable03.f90
===================================================================
--- /dev/null
+++ flang/test/Semantics/definable03.f90
@@ -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
Index: flang/lib/Semantics/check-do-forall.cpp
===================================================================
--- flang/lib/Semantics/check-do-forall.cpp
+++ flang/lib/Semantics/check-do-forall.cpp
@@ -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 @@
       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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D142767.492825.patch
Type: text/x-patch
Size: 1501 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20230127/1fff4f9c/attachment-0001.bin>


More information about the flang-commits mailing list