[flang-commits] [flang] 3382edf - [flang] Allow implicit declaration of DATA objects in inner procedures

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Mon May 9 17:49:54 PDT 2022


Author: Peter Klausler
Date: 2022-05-09T17:49:47-07:00
New Revision: 3382edf9b96caae4f1e2fde1b6611be95191de14

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

LOG: [flang] Allow implicit declaration of DATA objects in inner procedures

DATA statements in inner procedures were not treating undeclared objects
as implicitly declared variables if the DATA statement appeared in a
specification part; they were treated as host-associated symbols instead.
This was incorrect.  Fix DATA statement name resolution to always treat
DATA as if it had appeared in the executable part.

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

Added: 
    flang/test/Semantics/data16.f90

Modified: 
    flang/lib/Semantics/resolve-names.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index ed77d57b4f8c..f13c291a8088 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -648,7 +648,6 @@ class ScopeHandler : public ImplicitRulesVisitor {
   std::optional<SourceName> HadForwardRef(const Symbol &) const;
   bool CheckPossibleBadForwardRef(const Symbol &);
 
-  bool inExecutionPart_{false};
   bool inSpecificationPart_{false};
   bool inEquivalenceStmt_{false};
 
@@ -3389,7 +3388,7 @@ void SubprogramVisitor::PostEntryStmt(const parser::EntryStmt &stmt) {
                 context().SetError(*resultSymbol);
               }},
           resultSymbol->details());
-    } else if (inExecutionPart_) {
+    } else if (!inSpecificationPart_) {
       ObjectEntityDetails entity;
       entity.set_funcResult(true);
       resultSymbol = &MakeSymbol(effectiveResultName, std::move(entity));
@@ -3422,7 +3421,7 @@ void SubprogramVisitor::PostEntryStmt(const parser::EntryStmt &stmt) {
             dummy->details());
       } else {
         dummy = &MakeSymbol(*dummyName, EntityDetails{true});
-        if (inExecutionPart_) {
+        if (!inSpecificationPart_) {
           ApplyImplicitRules(*dummy);
         }
       }
@@ -5859,6 +5858,12 @@ bool ConstructVisitor::Pre(const parser::DataIDoObject &x) {
 }
 
 bool ConstructVisitor::Pre(const parser::DataStmtObject &x) {
+  // Subtle: DATA statements may appear in both the specification and
+  // execution parts, but should be treated as if in the execution part
+  // for purposes of implicit variable declaration vs. host association.
+  // When a name first appears as an object in a DATA statement, it should
+  // be implicitly declared locally as if it had been assigned.
+  auto flagRestorer{common::ScopedSet(inSpecificationPart_, false)};
   common::visit(common::visitors{
                     [&](const Indirection<parser::Variable> &y) {
                       Walk(y.value());
@@ -6415,7 +6420,7 @@ const parser::Name *DeclarationVisitor::ResolveName(const parser::Name &name) {
 // be wrong we report an error later in CheckDeclarations().
 bool DeclarationVisitor::CheckForHostAssociatedImplicit(
     const parser::Name &name) {
-  if (inExecutionPart_) {
+  if (!inSpecificationPart_) {
     return false;
   }
   if (name.symbol) {
@@ -7228,9 +7233,7 @@ bool ResolveNamesVisitor::Pre(const parser::ProgramUnit &x) {
   SetScope(topScope_);
   ResolveSpecificationParts(root);
   FinishSpecificationParts(root);
-  inExecutionPart_ = true;
   ResolveExecutionParts(root);
-  inExecutionPart_ = false;
   ResolveAccParts(context(), x);
   ResolveOmpParts(context(), x);
   return false;

diff  --git a/flang/test/Semantics/data16.f90 b/flang/test/Semantics/data16.f90
new file mode 100644
index 000000000000..7f8075c2ec66
--- /dev/null
+++ b/flang/test/Semantics/data16.f90
@@ -0,0 +1,15 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! Ensure that implicit declarations work in DATA statements
+! appearing in specification parts of inner procedures; they
+! should not elicit diagnostics about initialization of host
+! associated objects.
+program main
+ contains
+  subroutine subr
+    data foo/6.66/ ! implicit declaration of "foo": ok
+    !ERROR: Implicitly typed local entity 'n' not allowed in specification expression
+    real a(n)
+    !ERROR: Host-associated object 'n' must not be initialized in a DATA statement
+    data n/123/
+  end subroutine
+end program


        


More information about the flang-commits mailing list