Skip to content

[flang] Allow nested scopes for implied DO loops with DATA statements #129410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 5, 2025

Conversation

eugeneepshteyn
Copy link
Contributor

Previously, nested scopes for implied DO loops with DATA statements were disallowed, which meant that the following code couldn't compile due to re-use of j loop variable name:

DATA (a(i),(b(i,j),j=1,3),(c(i,j),j=1,3),i=0,4)/

This change allows nested scopes implied DO loops, which allows the code above to compile.

Tests modified to in accordance with this change: Semantics/resolve40.f90, Semantics/symbol09.f90

Previously, netsted implied DO loops with DATA statements were disallowed,
which meant that the following code couldn't compile:

DATA (a(i),(b(i,j),j=1,3),(c(i,j),j=1,3),i=0,4)/

This change allowes nested implied DO loops.
	modified:   lib/Semantics/resolve-names.cpp

Adjusted the test to the new behavior of implied-DO loops in DATA statements:

	modified:   test/Semantics/resolve40.f90
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Mar 2, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 2, 2025

@llvm/pr-subscribers-flang-semantics

Author: Eugene Epshteyn (eugeneepshteyn)

Changes

Previously, nested scopes for implied DO loops with DATA statements were disallowed, which meant that the following code couldn't compile due to re-use of j loop variable name:

DATA (a(i),(b(i,j),j=1,3),(c(i,j),j=1,3),i=0,4)/

This change allows nested scopes implied DO loops, which allows the code above to compile.

Tests modified to in accordance with this change: Semantics/resolve40.f90, Semantics/symbol09.f90


Full diff: https://github.com/llvm/llvm-project/pull/129410.diff

3 Files Affected:

  • (modified) flang/lib/Semantics/resolve-names.cpp (+4-9)
  • (modified) flang/test/Semantics/resolve40.f90 (+2-2)
  • (modified) flang/test/Semantics/symbol09.f90 (+1-1)
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 1514c01a49528..d29e04869e615 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -7477,15 +7477,10 @@ bool ConstructVisitor::Pre(const parser::DataImpliedDo &x) {
   Walk(bounds.upper);
   Walk(bounds.step);
   EndCheckOnIndexUseInOwnBounds(restore);
-  bool pushScope{currScope().kind() != Scope::Kind::ImpliedDos};
-  if (pushScope) {
-    PushScope(Scope::Kind::ImpliedDos, nullptr);
-  }
+  PushScope(Scope::Kind::ImpliedDos, nullptr);
   DeclareStatementEntity(bounds.name, type);
   Walk(objects);
-  if (pushScope) {
-    PopScope();
-  }
+  PopScope();
   return false;
 }
 
@@ -7526,9 +7521,9 @@ bool ConstructVisitor::Pre(const parser::DataStmtObject &x) {
             }
           },
           [&](const parser::DataImpliedDo &y) {
-            PushScope(Scope::Kind::ImpliedDos, nullptr);
+            // Don't push scope here, since it's done when visiting
+            // DataImpliedDo.
             Walk(y);
-            PopScope();
           },
       },
       x.u);
diff --git a/flang/test/Semantics/resolve40.f90 b/flang/test/Semantics/resolve40.f90
index b3384a91097d7..a91507aa62282 100644
--- a/flang/test/Semantics/resolve40.f90
+++ b/flang/test/Semantics/resolve40.f90
@@ -69,8 +69,8 @@ subroutine s8
 
 subroutine s9
   real :: x(2,2)
-  !ERROR: 'i' is already declared in this scoping unit
-  data ((x(i,i),i=1,2),i=1,2)/4*0.0/
+  ! Nested implied DO loops have their own scope
+  data ((x(i,j),j=1,2),(x(i,j),j=1,2),i=1,2)/8*0.0/
 end
 
 module m10
diff --git a/flang/test/Semantics/symbol09.f90 b/flang/test/Semantics/symbol09.f90
index 98cd1d954c3e7..0ed80b55b34d1 100644
--- a/flang/test/Semantics/symbol09.f90
+++ b/flang/test/Semantics/symbol09.f90
@@ -51,7 +51,7 @@ subroutine s3
  real, dimension(n,n) :: x
  !REF: /s3/x
  !DEF: /s3/ImpliedDos1/k (Implicit) ObjectEntity INTEGER(4)
- !DEF: /s3/ImpliedDos1/j ObjectEntity INTEGER(8)
+ !DEF: /s3/ImpliedDos1/ImpliedDos1/j ObjectEntity INTEGER(8)
  !REF: /s3/n
  !REF: /s3/n2
  data ((x(k,j),integer(kind=8)::j=1,n),k=1,n)/n2*3.0/

@eugeneepshteyn eugeneepshteyn merged commit ab6cc6b into llvm:main Mar 5, 2025
14 checks passed
@eugeneepshteyn eugeneepshteyn deleted the nested-impl-do-data branch March 5, 2025 01:42
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
…llvm#129410)

Previously, nested scopes for implied DO loops with DATA statements were
disallowed, which meant that the following code couldn't compile due to
re-use of `j` loop variable name:
    
    DATA (a(i),(b(i,j),j=1,3),(c(i,j),j=1,3),i=0,4)/
    
This change allows nested scopes implied DO loops, which allows the code
above to compile.

Tests modified to in accordance with this change:
Semantics/resolve40.f90, Semantics/symbol09.f90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants