3 ! Test finalization on intrinsic assignment (F2018 (7.5.6.3))
4 ! With -std=gnu, no finalization of array or structure constructors should occur.
5 ! See finalize_38a.f90 for the result with f2008.
6 ! Tests fix for PR64290 as well.
14 final
:: destructor1
, destructor2
17 type, extends(simple
) :: complicated
20 final
:: destructor3
, destructor4
23 integer :: check_scalar
24 integer :: check_array(4)
26 real :: check_rarray(4)
27 integer :: final_count
= 0
31 subroutine destructor1(self
)
32 type(simple
), intent(inout
) :: self
33 check_scalar
= self
%ind
35 final_count
= final_count
+ 1
36 end subroutine destructor1
38 subroutine destructor2(self
)
39 type(simple
), intent(inout
) :: self(:)
41 check_array(1:size(self
, 1)) = self
%ind
42 final_count
= final_count
+ 1
43 end subroutine destructor2
45 subroutine destructor3(self
)
46 type(complicated
), intent(inout
) :: self
47 check_real
= self
%rind
49 final_count
= final_count
+ 1
50 end subroutine destructor3
52 subroutine destructor4(self
)
53 type(complicated
), intent(inout
) :: self(:)
55 check_rarray(1:size(self
, 1)) = self
%rind
56 final_count
= final_count
+ 1
57 end subroutine destructor4
59 function constructor1(ind
) result(res
)
60 class(simple
), allocatable
:: res
61 integer, intent(in
) :: ind
62 allocate (res
, source
= simple (ind
))
63 end function constructor1
65 function constructor2(ind
, rind
) result(res
)
66 class(simple
), allocatable
:: res(:)
67 integer, intent(in
) :: ind(:)
68 real, intent(in
), optional
:: rind(:)
69 type(complicated
), allocatable
:: src(:)
72 if (present (rind
)) then
73 sz
= min (size (ind
, 1), size (rind
, 1))
74 src
= [(complicated (ind(i
), rind(i
)), i
= 1, sz
)]
75 allocate (res
, source
= src
)
78 allocate (res
, source
= [(simple (ind(i
)), i
= 1, sz
)])
80 end function constructor2
82 subroutine test (cnt
, scalar
, array
, off
, rind
, rarray
)
87 real, optional
:: rind
88 real, optional
:: rarray(:)
89 if (final_count
.ne
. cnt
) then
92 if (check_scalar
.ne
. scalar
) then
95 if (any (check_array(1:size (array
, 1)) .ne
. array
)) then
98 if (present (rind
)) then
101 if (present (rarray
)) then
102 if (any (check_rarray(1:size (rarray
, 1)) .ne
. rarray
)) then
114 type(simple
), allocatable
:: MyType
, MyType2
115 type(simple
), allocatable
:: MyTypeArray(:)
116 type(simple
) :: ThyType
= simple(21), ThyType2
= simple(22)
117 class(simple
), allocatable
:: MyClass
118 class(simple
), allocatable
:: MyClassArray(:)
120 ! ************************
121 ! Derived type assignments
122 ! ************************
124 ! The original PR - no finalization of 'var' before (re)allocation
125 ! because it is deallocated on scope entry (para 1 of F2018 7.5.6.3.)
127 call test(0, 0, [0,0], 0)
129 if (.not
. allocated(MyType
)) allocate(MyType
)
134 ! This should result in a final call with self = simple(1) (para 1 of F2018 7.5.6.3.).
136 call test(1, 1, [0,0], 10)
138 allocate(MyTypeArray(2))
139 MyTypeArray
%ind
= [42, 43]
140 ! This should result no calls.
141 call test(0, 1, [0,0], 20)
143 ! This should result in a final call 'var' = initialization = simple(22).
144 ThyType2
= simple(99)
145 call test(1, 22, [0,0], 30)
147 ! This should result in a final call for 'var' with self = simple(21).
149 call test(1, 21, [0,0], 40)
151 ! This should result in two final calls; the last is for Mytype2 = simple(2).
152 deallocate (MyType
, MyType2
)
153 call test(2, 2, [0,0], 50)
155 ! This should result in one final call; MyTypeArray = [simple(42),simple(43)].
156 deallocate (MyTypeArray
)
157 call test(1, 0, [42,43], 60)
159 ! The lhs is finalized before assignment.
160 ! The function result is finalized after the assignment.
161 ! NAGFOR doesn't finalize the function result.
162 allocate (MyType
, source
= simple (11))
163 MyType
= constructor1 (99)
164 call test(2, 99, [0,0], 70)
172 ! This should result in a final call for MyClass, which is simple(3).
173 allocate (MyClass
, source
= simple (3))
175 call test(1, 3, [0,0], 100)
177 ! This should result in a final call with the assigned value of simple(4).
179 call test(1, 4, [0,0], 110)
182 allocate (MyClassArray
, source
= [simple (5), simple (6)])
183 ! Make sure that there is no final call since MyClassArray is not allocated.
184 call test(0, 4, [0,0], 120)
186 MyClassArray
= [simple (7), simple (8)]
187 ! The only final call should finalize 'var'.
188 ! NAGFOR does something strange here: makes a scalar final call with value
190 call test(1, 0, [5,6], 130)
192 ! This should result in a final call with the assigned value.
193 deallocate (MyClassArray
)
194 call test(1, 0, [7,8], 140)
196 ! This should produce no final calls since MyClassArray was deallocated.
197 allocate (MyClassArray
, source
= [complicated(1, 2.0),complicated(3, 4.0)])
199 ! This should produce calls to destructor4 then destructor2.
200 if (allocated (MyClassArray
)) deallocate (MyClassArray
)
202 ! F2018 7.5.6.3: "If the entity is of extended type and the parent type is
203 ! finalizable, the parent component is finalized.
204 call test(2, 0, [1, 3], 150, rarray
= [2.0, 4.0])
206 ! This produces 2 final calls in turn for 'src' as it goes out of scope, for
207 ! MyClassArray before it is assigned to and the result of 'constructor2' after
208 ! the assignment, for which the result should be should be [10,20] & [10.0,20.0].
209 MyClassArray
= constructor2 ([10,20], [10.0,20.0])
210 call test(4, 0, [10,20], 160, rarray
= [10.0,20.0])
212 ! This produces two final calls with the contents of 'MyClassArray. and its
214 deallocate (MyClassArray
)
215 call test(2, 0, [10, 20], 170, rarray
= [10.0,20.0])
217 ! Clean up for valgrind testing
218 if (allocated (MyType
)) deallocate (MyType
)
219 if (allocated (MyType2
)) deallocate (MyType2
)
220 if (allocated (MyTypeArray
)) deallocate (MyTypeArray
)
221 if (allocated (MyClass
)) deallocate (MyClass
)
222 end program test_final