Debian Codegraph @

gcc/testsuite/gfortran.dg/finalize_58.f90

raw
1 ! { dg-do run }
2 !
3 ! Test fix for PR116388 in which an artificial variable in the finalization
4 ! wrapper was generating an invalid finalization.
5 !
6 ! Contributed by Tomas Trnka <[email protected]>
7 !
8 module FinalizerTestModule
9
10 use, intrinsic :: ISO_C_BINDING
11
12 implicit none
13
14 type, public :: AType
15 type(C_ptr) :: cptr = C_null_ptr
16 logical :: cptr_invalid = .true.
17 integer, allocatable :: x(:)
18 contains
19 final :: FinalizerA
20 end type
21
22 type, public :: BType
23 type(C_ptr) :: cptr = C_null_ptr
24 type(AType) :: a
25 contains
26 procedure, public :: New => NewB
27 final :: FinalizerB
28 end type
29
30 type, public :: CType
31 type(BType) :: b
32 contains
33 procedure, public :: New => NewC
34 end type
35
36 integer :: final_A = 0
37 integer :: final_B = 0
38 contains
39
40 impure elemental subroutine FinalizerA(self)
41 type(AType), intent(inout) :: self
42 final_A = final_A + 1
43 if (.not. self%cptr_invalid) stop 1
44 end subroutine
45
46 subroutine NewB(self)
47 class(BType), intent(out) :: self
48
49 end subroutine
50
51 impure elemental subroutine FinalizerB(self)
52 type(BType), intent(inout) :: self
53 final_B = final_B + 1
54 if (transfer (self%cptr, C_LONG_LONG) /= 0) stop 2
55 end subroutine
56
57 subroutine NewC(self, b)
58 class(CType), intent(out) :: self
59 type(BType), intent(in) :: b
60
61 self%b = b
62 end subroutine
63
64 end module
65
66 program finalizing_uninitialized
67 use FinalizerTestModule
68 implicit none
69
70 type(BType) :: b
71 type(CType) :: c
72
73 call b%New()
74 call c%New(b)
75 if (final_A /= 3) stop 3
76 if (final_B /= 3) stop 4
77 end program