[flang-commits] [flang] [Flang][Semantics] Treat function result variables as local in pure definability checks. (PR #192892)

via flang-commits flang-commits at lists.llvm.org
Sun Apr 19 21:25:17 PDT 2026


https://github.com/ShashwathiNavada created https://github.com/llvm/llvm-project/pull/192892

This patch fixes a false semantic error in Flang where function result variables were incorrectly treated as externally visible in pure-definability checks. 

As a result, valid code assigning a pointer component of a function result (as in flang/test/Semantics/pure-function-result-pointer.f90) was rejected with “not definable in a pure subprogram.”

The fix updates _FindExternallyVisibleObject_ to treat function result symbols as local, which matches Fortran semantics for function result variables.

>From 9453e59c29fc11a706d2c37d07a399ae7dcee5b5 Mon Sep 17 00:00:00 2001
From: ShashwathiNavada <shashwathinavada at gmail.com>
Date: Sun, 19 Apr 2026 23:20:53 -0500
Subject: [PATCH] Treat function result variables as local in pure definability
 checks

---
 flang/lib/Semantics/tools.cpp                 |  4 ++
 .../pure-function-result-pointer.f90          | 53 +++++++++++++++++++
 2 files changed, 57 insertions(+)
 create mode 100644 flang/test/Semantics/pure-function-result-pointer.f90

diff --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp
index 68307f7504e28..d2a10930ae9dc 100644
--- a/flang/lib/Semantics/tools.cpp
+++ b/flang/lib/Semantics/tools.cpp
@@ -338,6 +338,10 @@ const Symbol *FindExternallyVisibleObject(
         IsPureProcedure(ultimate.owner()) && IsFunction(ultimate.owner())) {
       return &ultimate;
     }
+  } else if (IsFunctionResult(ultimate)) {
+    // A function result variable is local to its function and not an
+    // externally visible object for C1594 checks.
+    return nullptr;
   } else if (ultimate.owner().IsDerivedType()) {
     return nullptr;
   } else if (&GetProgramUnitContaining(ultimate) !=
diff --git a/flang/test/Semantics/pure-function-result-pointer.f90 b/flang/test/Semantics/pure-function-result-pointer.f90
new file mode 100644
index 0000000000000..fc98f50b6d8dc
--- /dev/null
+++ b/flang/test/Semantics/pure-function-result-pointer.f90
@@ -0,0 +1,53 @@
+! RUN: %flang_fc1 -fsyntax-only %s
+
+module associated_func_call
+  implicit none
+
+  private
+  public :: type_t
+  public :: test_function_i
+
+  abstract interface
+    function test_function_i() result(passes)
+      implicit none
+      logical passes
+    end function
+  end interface
+
+  type type_t
+    private
+    procedure(test_function_i), pointer, nopass :: test_function_ => null()
+  contains
+    generic :: operator(==) => equals
+    procedure, private :: equals
+  end type
+
+  interface type_t
+
+    module function construct(test_function) result(test_description)
+      implicit none
+      procedure(test_function_i), intent(in), pointer :: test_function
+      type(type_t) test_description
+    end function
+
+  end interface
+
+  interface
+
+    elemental module function equals(lhs, rhs) result(lhs_eq_rhs)
+      implicit none
+      class(type_t), intent(in) :: lhs, rhs
+      logical lhs_eq_rhs
+    end function
+
+  end interface
+
+contains
+    module procedure construct
+      test_description%test_function_ => test_function
+    end procedure
+
+    module procedure equals
+      lhs_eq_rhs = associated(lhs%test_function_, rhs%test_function_)
+    end procedure
+end module associated_func_call



More information about the flang-commits mailing list