Index: flang/lib/Semantics/resolve-names.cpp =================================================================== --- flang/lib/Semantics/resolve-names.cpp +++ flang/lib/Semantics/resolve-names.cpp @@ -644,7 +644,6 @@ std::optional HadForwardRef(const Symbol &) const; bool CheckPossibleBadForwardRef(const Symbol &); - bool inExecutionPart_{false}; bool inSpecificationPart_{false}; bool inEquivalenceStmt_{false}; @@ -3380,7 +3379,7 @@ context().SetError(*resultSymbol); }}, resultSymbol->details()); - } else if (inExecutionPart_) { + } else if (!inSpecificationPart_) { ObjectEntityDetails entity; entity.set_funcResult(true); resultSymbol = &MakeSymbol(effectiveResultName, std::move(entity)); @@ -3413,7 +3412,7 @@ dummy->details()); } else { dummy = &MakeSymbol(*dummyName, EntityDetails{true}); - if (inExecutionPart_) { + if (!inSpecificationPart_) { ApplyImplicitRules(*dummy); } } @@ -5850,6 +5849,12 @@ } 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 &y) { Walk(y.value()); @@ -6406,7 +6411,7 @@ // 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) { @@ -7219,9 +7224,7 @@ SetScope(topScope_); ResolveSpecificationParts(root); FinishSpecificationParts(root); - inExecutionPart_ = true; ResolveExecutionParts(root); - inExecutionPart_ = false; ResolveAccParts(context(), x); ResolveOmpParts(context(), x); return false; Index: flang/test/Semantics/data16.f90 =================================================================== --- /dev/null +++ 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