Debian Codegraph @

gcc/testsuite/gfortran.dg/alloc_comp_assign_17.f90

raw
1 ! { dg-do run }
2 ! PR fortran/121616
3 !
4 ! Test fix for intrinsic assignment to allocatable scalar polymorphic component
5
6 program p
7 call pr121616 ()
8 call test_ts ()
9 end
10
11 ! Derived from original PR (contributed by Jean Vézina)
12 subroutine pr121616 ()
13 implicit none
14 integer :: i
15 type general
16 class(*), allocatable :: x
17 end type general
18 type(general) :: a(4), b(4)
19 ! Intrinsic assignment to a variable of unlimited polymorphic type
20 a(1)%x = 1
21 a(2)%x = 3.14
22 a(3)%x = .true.
23 a(4)%x = 'abc'
24 ! The workaround was to use a structure constructor
25 b(1) = general(1)
26 b(2) = general(3.14)
27 b(3) = general(.true.)
28 b(4) = general('abc')
29 do i = 1, 4
30 if (.not. allocated (a(i)%x)) stop 10+i
31 if (.not. allocated (b(i)%x)) stop 20+i
32 call prt (a(i)%x, b(i)%x)
33 end do
34 do i = 1, 4
35 deallocate (a(i)%x, b(i)%x)
36 end do
37 contains
38 subroutine prt (x, y)
39 class(*), intent(in) :: x, y
40 select type (v=>x)
41 type is (integer)
42 print *,v
43 type is (real)
44 print *,v
45 type is (logical)
46 print *,v
47 type is (character(*))
48 print *,v
49 class default
50 error stop 99
51 end select
52 if (.not. same_type_as (x, y)) stop 30+i
53 end subroutine prt
54 end
55
56 ! Contributed by a friend (private communication)
57 subroutine test_ts ()
58 implicit none
59
60 type :: t_inner
61 integer :: i
62 end type
63
64 type :: t_outer
65 class(t_inner), allocatable :: inner
66 end type
67
68 class(t_inner), allocatable :: inner
69 type(t_outer), allocatable :: outer(:)
70 integer :: i
71
72 allocate(t_inner :: inner)
73 inner% i = 0
74
75 !------------------------------------------------
76 ! Size of outer must be > 1 for the bug to appear
77 !------------------------------------------------
78 allocate(outer(2))
79
80 !------------------------------
81 ! Loop is necessary for the bug
82 !------------------------------
83 do i = 1, size(outer)
84 write(*,*) i
85 !----------------------------------------------------
86 ! Expect intrinsic assignment to polymorphic variable
87 !----------------------------------------------------
88 outer(i)% inner = inner
89 deallocate (outer(i)% inner)
90 end do
91
92 write(*,*) 'Loop DONE'
93 deallocate(outer)
94 deallocate(inner)
95 write(*,*) 'Dellocation DONE'
96 end