]> git.ipfire.org Git - thirdparty/gcc.git/commit
fortran: [PR121628]
authorJerry DeLisle <jvdelisle@gcc.gnu.org>
Thu, 6 Nov 2025 20:44:18 +0000 (12:44 -0800)
committerJerry DeLisle <jvdelisle@gcc.gnu.org>
Thu, 6 Nov 2025 20:58:45 +0000 (12:58 -0800)
commit9636d90e4326003e6da1ea86df7c730852629920
tree6f633693d55906e0ec47f65bbe47bc490bd11ba5
parent3dbca5ff67b830c2f0d512f048d0f334212a3191
fortran: [PR121628]

This patch fixes PR121628 by implementing proper deep copy semantics for
derived types containing recursive allocatable array components, in
compliance with Fortran 2018+ standards.

The original implementation would generate infinitely recursive code at
compile time when encountering self-referential derived types with
allocatable components (e.g., type(t) containing allocatable type(t)
arrays). This patch solves the problem by generating a runtime helper
function that performs element-wise deep copying, avoiding compile-time
recursion while maintaining correct assignment semantics.

The trans-intrinsic.cc change enhances handling of constant values in
coarray atomic operations to ensure temporary variables are created when
needed, avoiding invalid address-of-constant expressions.

gcc/fortran/ChangeLog:

PR fortran/121628
* trans-array.cc (get_copy_helper_function_type): New function to
create function type for element copy helpers.
(get_copy_helper_pointer_type): New function to create pointer type
for element copy helpers.
(generate_element_copy_wrapper): New function to generate runtime
helper for element-wise deep copying of recursive types.
(structure_alloc_comps): Detect recursive allocatable array
components and use runtime helper instead of inline recursion.
Add includes for cgraph.h and function.h.
* trans-decl.cc (gfor_fndecl_cfi_deep_copy_array): New declaration
for runtime deep copy helper.
(gfc_build_builtin_function_decls): Initialize the runtime helper
declaration.
* trans-intrinsic.cc (conv_intrinsic_atomic_op): Enhance handling of
constant values in coarray atomic operations by detecting and
materializing address-of-constant expressions.
* trans.h (gfor_fndecl_cfi_deep_copy_array): Add external declaration.

libgfortran/ChangeLog:

PR fortran/121628
* Makefile.am: Add runtime/deep_copy.c to source files.
* Makefile.in: Regenerate.
* gfortran.map: Export _gfortran_cfi_deep_copy_array symbol.
* libgfortran.h: Add prototype for internal_deep_copy_array.
* runtime/deep_copy.c: New file implementing runtime deep copy
helper for recursive allocatable array components.

gcc/testsuite/ChangeLog:

PR fortran/121628
* gfortran.dg/alloc_comp_deep_copy_5.f90: New test for recursive
allocatable array deep copy.
* gfortran.dg/alloc_comp_deep_copy_6.f90: New test for multi-level
recursive allocatable deep copy.
* gfortran.dg/array_memcpy_2.f90: Fix test with proper allocation.

Signed-off-by: Christopher Albert <albert@tugraz.at>
12 files changed:
gcc/fortran/trans-array.cc
gcc/fortran/trans-decl.cc
gcc/fortran/trans-intrinsic.cc
gcc/fortran/trans.h
gcc/testsuite/gfortran.dg/alloc_comp_deep_copy_5.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/alloc_comp_deep_copy_6.f90 [new file with mode: 0644]
gcc/testsuite/gfortran.dg/array_memcpy_2.f90
libgfortran/Makefile.am
libgfortran/Makefile.in
libgfortran/gfortran.map
libgfortran/libgfortran.h
libgfortran/runtime/deep_copy.c [new file with mode: 0644]