Skip to content

Conversation

@klausler
Copy link
Contributor

A PURE subprogram can't have a local variable with the SAVE attribute. An ASSOCIATE or SELECT TYPE construct entity whose selector is a variable will return true from IsSave(); exclude them from the local variable check.

Fixes #131356.

A PURE subprogram can't have a local variable with the SAVE
attribute.  An ASSOCIATE or SELECT TYPE construct entity whose
selector is a variable will return true from IsSave(); exclude
them from the local variable check.

Fixes llvm#131356.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Mar 14, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 14, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

A PURE subprogram can't have a local variable with the SAVE attribute. An ASSOCIATE or SELECT TYPE construct entity whose selector is a variable will return true from IsSave(); exclude them from the local variable check.

Fixes #131356.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/check-declarations.cpp (+4-1)
  • (modified) flang/test/Semantics/call10.f90 (+4)
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index e5a01657e4a15..0a0601cb04c85 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -363,7 +363,10 @@ void CheckHelper::Check(const Symbol &symbol) {
       // are not pertinent to the characteristics of the procedure.
       // Restrictions on entities in pure procedure interfaces don't need
       // enforcement.
-    } else if (!FindCommonBlockContaining(symbol) && IsSaved(symbol)) {
+    } else if (symbol.has<AssocEntityDetails>() ||
+        FindCommonBlockContaining(symbol)) {
+      // can look like they have SAVE but are fine in PURE
+    } else if (IsSaved(symbol)) {
       if (IsInitialized(symbol)) {
         messages_.Say(
             "A pure subprogram may not initialize a variable"_err_en_US);
diff --git a/flang/test/Semantics/call10.f90 b/flang/test/Semantics/call10.f90
index 81c28082a843f..47976ca9630cd 100644
--- a/flang/test/Semantics/call10.f90
+++ b/flang/test/Semantics/call10.f90
@@ -36,6 +36,8 @@ pure subroutine s05a
     end subroutine
   end interface
 
+  real :: moduleVar = 1.
+
  contains
 
   subroutine impure(x)
@@ -117,6 +119,8 @@ pure subroutine s05 ! C1589
     !ERROR: A pure subprogram may not initialize a variable
       real :: v6 = 0.
     end block
+    associate (x => moduleVar) ! ok
+    end associate
   end subroutine
   pure subroutine s06 ! C1589
     !ERROR: A pure subprogram may not have a variable with the VOLATILE attribute

@klausler klausler merged commit b99dab2 into llvm:main Mar 19, 2025
14 checks passed
@klausler klausler deleted the bug131356 branch March 19, 2025 19:01
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request May 9, 2025
A PURE subprogram can't have a local variable with the SAVE attribute.
An ASSOCIATE or SELECT TYPE construct entity whose selector is a
variable will return true from IsSave(); exclude them from the local
variable check.

Fixes llvm#131356.

(cherry picked from commit b99dab2)
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.

[flang] Spurious error with ASSOCIATE in a PURE procedure

3 participants