| 1 |
! { dg-do run } |
| 2 |
! Check that finalizable intent(out) dummy arguments are first finalized |
| 3 |
! and then correctly default-initialized (PR116829) |
| 4 |
! |
| 5 |
! Contributed by Tomas Trnka <[email protected]> |
| 6 |
! |
| 7 |
module FinalizableIntentOutTestModule |
| 8 |
implicit none |
| 9 |
|
| 10 |
type :: AapType |
| 11 |
integer :: i = 0 |
| 12 |
contains |
| 13 |
final :: Finalizer |
| 14 |
end type |
| 15 |
integer :: ctr = 0 |
| 16 |
logical :: err1 = .false. |
| 17 |
logical :: err2 = .false. |
| 18 |
contains |
| 19 |
|
| 20 |
subroutine Finalizer(self) |
| 21 |
type(AapType), intent(inout) :: self |
| 22 |
|
| 23 |
! Fail if Finalizer gets called again on an already finalized object |
| 24 |
if (self%i == 42) err1 = .true. |
| 25 |
|
| 26 |
self%i = 42 ! Nobody should ever see this value after finalization |
| 27 |
ctr = ctr + 1 |
| 28 |
end subroutine |
| 29 |
|
| 30 |
end module |
| 31 |
|
| 32 |
|
| 33 |
program test |
| 34 |
use FinalizableIntentOutTestModule |
| 35 |
|
| 36 |
implicit none |
| 37 |
|
| 38 |
type(AapType) :: aap |
| 39 |
|
| 40 |
! Set "i" to nonzero so that initialization in MakeAap has something to do |
| 41 |
aap%i = 1 |
| 42 |
|
| 43 |
call MakeAap(aap) |
| 44 |
|
| 45 |
if (err1) stop 1 |
| 46 |
if (err2) stop 2 ! This was failing |
| 47 |
if (ctr /= 1) stop 3 ! Belt and braces to ensure number of final calls correct. |
| 48 |
|
| 49 |
contains |
| 50 |
|
| 51 |
subroutine MakeAap(a) |
| 52 |
type(AapType), intent(out) :: a |
| 53 |
|
| 54 |
! Fail if "a" wasn't initialized properly |
| 55 |
if (a%i /= 0) err2 = .true. |
| 56 |
end subroutine |
| 57 |
|
| 58 |
end program |