Debian Codegraph @

gcc/testsuite/gfortran.dg/finalize_45.f90

raw
1 ! { dg-do run }
2 !
3 ! Test the fix for PR84472 in which the finalizations around the
4 ! assignment in 'mymain' were not happening.
5 !
6 ! Contributed by Vipul Parekh <[email protected]>
7 !
8 module m
9
10 use, intrinsic :: iso_fortran_env, only : output_unit
11
12 implicit none
13
14 ! private
15
16 integer, public :: final_counts = 0
17 integer, public :: assoc_counts = 0
18
19 type :: t
20 private
21 character(len=:), pointer :: m_s => null()
22 contains
23 private
24 final :: final_t
25 procedure, pass(this), public :: clean => clean_t
26 procedure, pass(this), public :: init => init_t
27 procedure, public :: assign_t
28 generic, public :: ASSIGNMENT(=) => assign_t
29 end type
30
31 interface t
32 module procedure :: construct_t
33 end interface
34
35 public :: t, assign_t
36
37 contains
38
39 impure elemental subroutine assign_t (to, from)
40 class(t), intent(out) :: to
41 class(t), intent(in) :: from
42 if (associated (from%m_s)) then
43 allocate(to%m_s, source = from%m_s)
44 else
45 allocate(to%m_s, source = "new")
46 endif
47 end subroutine assign_t
48
49 function construct_t( name ) result(new_t)
50
51 ! argument list
52 character(len=*), intent(in), optional :: name
53 ! function result
54 type(t) :: new_t
55
56 if ( present(name) ) then
57 call new_t%init( name )
58 end if
59
60 end function
61
62 subroutine final_t( this )
63
64 ! argument list
65 type(t), intent(inout) :: this
66
67 final_counts = final_counts + 1
68 if ( associated(this%m_s) ) then
69 assoc_counts = assoc_counts + 1
70 endif
71 call clean_t( this )
72
73 end subroutine
74
75 subroutine clean_t( this )
76
77 ! argument list
78 class(t), intent(inout) :: this
79
80 if ( associated(this%m_s) ) then
81 print *, this%m_s
82 deallocate( this%m_s )
83 end if
84 this%m_s => null()
85
86 end subroutine
87
88 subroutine init_t( this, mname )
89
90 ! argument list
91 class(t), intent(inout) :: this
92 character(len=*), intent(in) :: mname
93
94 call this%clean()
95 allocate(character(len(mname)) :: this%m_s)
96 this%m_s = mname
97
98 end subroutine
99
100 end module
101 use m, only : final_counts, assoc_counts
102 call mymain
103 ! See comment below.
104 if (final_counts /= 3) stop 1
105 if (assoc_counts /= 2) stop 2
106
107 contains
108 subroutine mymain
109
110 use m, only : t
111
112 implicit none
113
114 character(3), allocatable, target :: myname
115
116 type(t) :: foo
117
118 call foo%init( mname="123" )
119
120 myname = "foo"
121 foo = t( myname )
122
123 call foo%clean()
124
125 ! NAGFOR has assoc_counts =2, which is probably correct. If nullification
126 ! of the pointer component is not done in gfortran, function finalization
127 ! results in a double free. TODO fix this.
128 if (final_counts /= 2) stop 3
129 if (assoc_counts /= 2) stop 4
130 end
131 end
132