pop_cfun ();
- cgraph_node::add_new_function (fndecl, false);
+ /* Use finalize_function with no_collect=true to skip the ggc_collect
+ call that add_new_function would trigger. This function is called
+ during tree lowering of structure_alloc_comps where caller stack
+ frames hold locally-computed tree nodes (COMPONENT_REFs etc.) that
+ are not yet attached to any GC root. A collection at this point
+ would free those nodes and cause segfaults. PR124235. */
+ cgraph_node::finalize_function (fndecl, true);
return build1 (ADDR_EXPR, get_copy_helper_pointer_type (), fndecl);
}
--- /dev/null
+! { dg-do compile }
+! PR fortran/124235 - ICE in ALLOCATE of sub-objects with recursive types
+!
+! Mutually-referencing derived types with a mix of allocatable and
+! fixed-size array components. Allocating a sub-object of an
+! already-allocated component triggered a segfault during tree lowering
+! because garbage collection during deep-copy wrapper generation
+! invalidated locally-computed COMPONENT_REF tree nodes.
+
+program pr124235
+ implicit none
+
+ type :: alpha_t
+ integer, allocatable :: vals(:)
+ type(alpha_t), allocatable :: a_kids(:)
+ type(gamma_t), allocatable :: g_ref(:)
+ integer :: tag
+ type(beta_t), allocatable :: b_ref(:)
+ end type
+
+ type :: beta_t
+ integer, allocatable :: vals(:)
+ type(alpha_t) :: a_fixed(4)
+ type(beta_t), allocatable :: b_kids(:)
+ integer :: tag
+ type(gamma_t), allocatable :: g_ref(:)
+ end type
+
+ type :: gamma_t
+ integer, allocatable :: vals(:)
+ type(beta_t) :: b_fixed(4)
+ integer :: tag
+ type(gamma_t), allocatable :: g_kids(:)
+ end type
+
+ type :: container_t
+ type(gamma_t), allocatable :: items(:)
+ type(gamma_t), allocatable :: spares(:)
+ end type
+
+ type(container_t) :: box
+
+ allocate(box%items(6))
+ allocate(box%items(2)%g_kids(3))
+
+end program