https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121628
--- Comment #17 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jerry DeLisle <[email protected]>: https://gcc.gnu.org/g:9636d90e4326003e6da1ea86df7c730852629920 commit r16-5067-g9636d90e4326003e6da1ea86df7c730852629920 Author: Jerry DeLisle <[email protected]> Date: Thu Nov 6 12:44:18 2025 -0800 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 <[email protected]>
