Hello All,

Please find attached the patch for the above PR, which is one of the
few bugs found so far in the shared memory implementation of coarrays.

Passes regtesting on FC43/x86_64. OK for mainline and, after a decent
interval, backporting to 16-branch?

Regards

Paul (and Andre)
From a699ffd6073a855f9ef3399004bf36e65993e085 Mon Sep 17 00:00:00 2001
From: Paul Thomas <[email protected]>
Date: Thu, 7 May 2026 18:48:05 +0100
Subject: [PATCH] Fortran: Allow access to coarray elements within modules.
 [PR125051]

The parts of this patch is fix the problem are chunks 2 and 3. Chunk3 prevents
gfc_conv_intrinsic_caf_get from working in the module namespace, when the array
symbol is in a module. Equally, though, gfc_current_ns is not necessarily in
the referencing procedure namespace. The second chunk makes sure that this is
the case. As an aside, it seems to us that it makes considerably more sense that
gfc_current_ns be that of the current procedure. The first chunk makes sure that
result symbol initialization does not occur outside the function.

Passes regtesting with FC44/x86_64.

2026-05-07  Andre Vehreschild  <[email protected]>
	    Paul Thomas  <[email protected]>

gcc/fortran
	PR fortran/125051
	* trans-decl.cc (gfc_get_symbol_decl): gfc_defer_symbol_init
	must not be called for PDT types, classes or types with PDT
	(gfc_generate_function_code): If gfc_current_ns is not the same
	as the function namespace, stash it,change it to the function
	namespace and restore after translation of the code.
	* trans-intrinsic.cc (gfc_conv_intrinsic_caf_get): If the array
	is in a module, use the symbol namespace.
	* trans-openmp (gfc_trans_omp_array_reduction_or_udr): If the
	current namespace is not that of the procedure, change to the
	procedure namspace and revert on leaving this function.

gcc/testsuite/
	PR fortran/125051
	* gfortran.dg/pr125051.f90: New test.
---
 .../gfortran.dg/coarray/pr125051.f90          | 34 +++++++++++++++++++
 1 file changed, 34 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray/pr125051.f90

diff --git a/gcc/testsuite/gfortran.dg/coarray/pr125051.f90 b/gcc/testsuite/gfortran.dg/coarray/pr125051.f90
new file mode 100644
index 00000000000..9097340b706
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/pr125051.f90
@@ -0,0 +1,34 @@
+!{ dg-do link }
+
+! Contributed by Paul Thomas  <[email protected]>
+! Check PR fortran/125051 is fixed.
+
+module m
+  integer, parameter :: ncells = 8, nsize = ncells+2
+  integer, parameter :: head =2, tail = ncells + 1
+  real :: second_derivative(ncells+2, ncells+2)
+  type :: density_t
+    real :: density(nsize)
+    real:: derivative(nsize)
+  end type
+  type (density_t) :: n[*]
+  real :: n_tail[*] = 0.0
+  real :: n_head[*] = 0.0
+contains
+  subroutine sub
+    integer :: image_no
+    image_no = this_image()
+    sync all
+    if (image_no > 1) n_head = n[image_no -1]%density(tail)
+    if (image_no < num_images()) n_tail = n[image_no +1]%density(head)
+    sync all
+  end
+end module
+
+program main
+  use m
+  implicit none
+  call sub
+contains
+end program
+
-- 
2.54.0

Reply via email to