Skip to content

[flang] Enforce C15104(5) for coindexed values #130203

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 1 commit into from
Mar 10, 2025
Merged

Conversation

klausler
Copy link
Contributor

@klausler klausler commented Mar 6, 2025

A object's value can't be copied from another image by means of an intrinsic assignment statement if it has a derived type that contains a pointer subobject ultimate component.

A object's value can't be copied from another image by means of
an intrinsic assignment statement if it has a derived type that
contains a pointer subobject ultimate component.
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Mar 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 6, 2025

@llvm/pr-subscribers-flang-semantics

Author: Peter Klausler (klausler)

Changes

A object's value can't be copied from another image by means of an intrinsic assignment statement if it has a derived type that contains a pointer subobject ultimate component.


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

2 Files Affected:

  • (modified) flang/lib/Semantics/assignment.cpp (+10-4)
  • (modified) flang/test/Semantics/call12.f90 (+7)
diff --git a/flang/lib/Semantics/assignment.cpp b/flang/lib/Semantics/assignment.cpp
index 8de20d3126a6c..935f5a03bdb6a 100644
--- a/flang/lib/Semantics/assignment.cpp
+++ b/flang/lib/Semantics/assignment.cpp
@@ -134,10 +134,16 @@ static std::optional<std::string> GetPointerComponentDesignatorName(
 // Checks C1594(5,6); false if check fails
 bool CheckCopyabilityInPureScope(parser::ContextualMessages &messages,
     const SomeExpr &expr, const Scope &scope) {
-  if (const Symbol * base{GetFirstSymbol(expr)}) {
-    if (const char *why{
-            WhyBaseObjectIsSuspicious(base->GetUltimate(), scope)}) {
-      if (auto pointer{GetPointerComponentDesignatorName(expr)}) {
+  if (auto pointer{GetPointerComponentDesignatorName(expr)}) {
+    if (const Symbol * base{GetFirstSymbol(expr)}) {
+      const char *why{WhyBaseObjectIsSuspicious(base->GetUltimate(), scope)};
+      if (!why) {
+        if (auto coarray{evaluate::ExtractCoarrayRef(expr)}) {
+          base = &coarray->GetLastSymbol();
+          why = "coindexed";
+        }
+      }
+      if (why) {
         evaluate::SayWithDeclaration(messages, *base,
             "A pure subprogram may not copy the value of '%s' because it is %s"
             " and has the POINTER potential subobject component '%s'"_err_en_US,
diff --git a/flang/test/Semantics/call12.f90 b/flang/test/Semantics/call12.f90
index cd4006a53b3e7..e7c0fd8b9b8cb 100644
--- a/flang/test/Semantics/call12.f90
+++ b/flang/test/Semantics/call12.f90
@@ -104,4 +104,11 @@ pure subroutine internal
       localhp = hasPtr(z%a)
     end subroutine
   end function
+  pure subroutine test2(hpd, hhpd)
+    use used
+    type(hasHiddenPtr), intent(in out) :: hpd, hhpd[*]
+    hpd = hhpd ! ok
+    !ERROR: A pure subprogram may not copy the value of 'hhpd' because it is coindexed and has the POINTER potential subobject component '%a%p'
+    hpd = hhpd[1]
+  end subroutine
 end module

pure subroutine test2(hpd, hhpd)
use used
type(hasHiddenPtr), intent(in out) :: hpd, hhpd[*]
hpd = hhpd ! ok
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is ok, because hhpd was passed as in out? It wouldn't be ok, if it were passed as in?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right, and that's a distinct error. You can't use pointer components to bypass the restrictions of INTENT(IN) in a pure subprogram.

@klausler klausler merged commit 40d9096 into llvm:main Mar 10, 2025
14 checks passed
@klausler klausler deleted the fix259 branch March 10, 2025 20:20
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