2 ! { dg-options "-fdump-tree-original" }
5 ! Missing deep copy for data pointer returning functions when the type
6 ! has allocatable components
8 ! Original testcase by Thomas Henlich <thenlich@users.sourceforge.net>
9 ! Reduced by Tobias Burnus <burnus@net-b.de>
14 integer, dimension(:), allocatable :: i
17 procedure(find_x), nopass, pointer :: ppc => null()
19 procedure, nopass :: tbp => find_x
25 type(tx), target :: that
26 type(tx), pointer :: find_x
36 block ! Start new scoping unit as PROGRAM implies SAVE
38 type(tx), target :: that
39 type(tx), pointer :: p
46 this = that ! (1) direct assignment: works (deep copy)
49 if(any (this%i /= [3, 7])) call abort()
50 this = p ! (2) using a pointer works as well
53 if(any (this%i /= [2, -5])) call abort()
54 this = find_x(that) ! (3) pointer function: used to fail (deep copy missing)
57 if(any (this%i /= [10, 1])) call abort()
58 this = tab%tbp(that) ! other case: typebound procedure
61 if(any (this%i /= [4, 6])) call abort()
63 this = tab%ppc(that) ! other case: procedure pointer component
66 if(any (this%i /= [8, 9])) call abort()
72 ! We add another check for deep copy by looking at the dump.
73 ! We use realloc on assignment here: if we do a deep copy for the assignment
74 ! to `this', we have a reallocation of `this%i'.
75 ! Thus, the total number of malloc calls should be the number of assignment to
76 ! `that%i' + the number of assignments to `this' + the number of allocate
78 ! It is assumed that if the number of allocate is right, the number of
79 ! deep copies is right too.
80 ! { dg-final { scan-tree-dump-times "__builtin_malloc" 15 "original" } }
83 ! Realloc are only used for assignments to `that%i'. Don't know why.
84 ! { dg-final { scan-tree-dump-times "__builtin_realloc" 6 "original" } }
87 ! No leak: Only assignments to `this' use malloc. Assignments to `that%i'
88 ! take the realloc path after the first assignment, so don't count as a malloc.
89 ! { dg-final { scan-tree-dump-times "__builtin_free" 10 "original" } }