| 1 |
! { dg-do run } |
| 2 |
! PR fortran/108581 - issues with rank-2 deferred-length character arrays |
| 3 |
! PR fortran/121939 - ICE in gfc_conv_string_parameter |
| 4 |
|
| 5 |
program p |
| 6 |
call pr108581 |
| 7 |
call test2 |
| 8 |
end |
| 9 |
|
| 10 |
! Derived from original testcase |
| 11 |
subroutine pr108581 |
| 12 |
integer, parameter :: xmin = 0, xmax = 0 |
| 13 |
integer, parameter :: ymin = 0, ymax = 1 |
| 14 |
integer, parameter :: l = 2 |
| 15 |
integer :: x, y |
| 16 |
character(8) :: line1, line2, line3 |
| 17 |
character(*), parameter :: expect(ymin:ymax) = ['A.','B*'] |
| 18 |
character(len=:), pointer :: a(:,:) => NULL() |
| 19 |
|
| 20 |
allocate (character(len=l) :: a(xmin:xmax, ymin:ymax)) |
| 21 |
a(xmin:xmax, ymin) = expect(ymin) |
| 22 |
a(xmin:xmax, ymax) = expect(ymax) |
| 23 |
|
| 24 |
do y = ymin, ymax |
| 25 |
write(line1,'(4A)') (a(x, y), x = xmin, xmax) |
| 26 |
write(line2,'(4A)') a(xmin:xmax, y) |
| 27 |
write(line3,'(4A)') a( : , y) |
| 28 |
if (line1 /= expect(y) .or. & |
| 29 |
line2 /= expect(y) .or. & |
| 30 |
line3 /= expect(y) ) then |
| 31 |
write(*,*) (a(x, y), x = xmin, xmax) |
| 32 |
write(*,*) a(xmin:xmax, y) |
| 33 |
write(*,*) a( : , y) |
| 34 |
stop 1 + y |
| 35 |
end if |
| 36 |
enddo |
| 37 |
call chk (a) |
| 38 |
deallocate (a) |
| 39 |
contains |
| 40 |
subroutine chk (z) |
| 41 |
character(len=:), pointer :: z(:,:) |
| 42 |
integer :: y |
| 43 |
do y = lbound(z,2), ubound (z,2) |
| 44 |
write(line2,'(4A)') z(xmin:xmax, y) |
| 45 |
write(line3,'(4A)') z( : , y) |
| 46 |
if (line2 /= expect(y) .or. & |
| 47 |
line3 /= expect(y) ) then |
| 48 |
write(*,*) z(xmin:xmax, y) |
| 49 |
write(*,*) z( : , y) |
| 50 |
stop 5 + y |
| 51 |
end if |
| 52 |
enddo |
| 53 |
end subroutine chk |
| 54 |
end |
| 55 |
|
| 56 |
! Exercise character kinds, strides, ... |
| 57 |
subroutine test2 |
| 58 |
implicit none |
| 59 |
integer, parameter :: l = 3 |
| 60 |
integer :: i |
| 61 |
|
| 62 |
character(len=l,kind=1), parameter :: str1(*) = & |
| 63 |
[ "123", "456", "789", "0AB" ] |
| 64 |
character(len=l,kind=4), parameter :: str4(*) = & |
| 65 |
[ 4_"123", 4_"456", 4_"789", 4_"0AB" ] |
| 66 |
|
| 67 |
character(len=l,kind=1), parameter :: str2(*,*) = & |
| 68 |
reshape ([(str1(i),str1(5-i),i=1,4)], shape=[2,4]) |
| 69 |
character(len=l,kind=4), parameter :: str5(*,*) = & |
| 70 |
reshape ([(str4(i),str4(5-i),i=1,4)], shape=[2,4]) |
| 71 |
|
| 72 |
character(len=l,kind=1), pointer :: a(:,:) => NULL(), e(:,:) => NULL() |
| 73 |
character(len=:,kind=1), pointer :: b(:,:) => NULL(), f(:,:) => NULL() |
| 74 |
character(len=l,kind=4), pointer :: c(:,:) => NULL(), g(:,:) => NULL() |
| 75 |
character(len=:,kind=4), pointer :: d(:,:) => NULL(), h(:,:) => NULL() |
| 76 |
|
| 77 |
character(len=16) :: s0, s1, s2, s3, s4 |
| 78 |
|
| 79 |
! Simple case: shape=[1,4] |
| 80 |
allocate (a, source = reshape (str1,[1,size(str1)])) |
| 81 |
allocate (b, source = reshape (str1,[1,size(str1)])) |
| 82 |
allocate (c, source = reshape (str4,[1,size(str4)])) |
| 83 |
allocate (d, source = c) ! fixed with pr121939 |
| 84 |
! d => c |
| 85 |
! Positive non-unit stride |
| 86 |
s0 = concat (str1(1::2)) |
| 87 |
write(s1,'(4A)') a(1,1::2) |
| 88 |
write(s2,'(4A)') b(1,1::2) |
| 89 |
write(s3,'(4A)') c(1,1::2) |
| 90 |
write(s4,'(4A)') d(1,1::2) |
| 91 |
! print *, s0, s1, s2, s3, s4 |
| 92 |
if (s1 /= s0) stop 11 |
| 93 |
if (s2 /= s0) stop 12 |
| 94 |
if (s3 /= s0) stop 13 |
| 95 |
if (s4 /= s0) stop 14 |
| 96 |
s0 = concat (str1(2::2)) |
| 97 |
write(s1,'(4A)') a(1,2::2) |
| 98 |
write(s2,'(4A)') b(1,2::2) |
| 99 |
write(s3,'(4A)') c(1,2::2) |
| 100 |
write(s4,'(4A)') d(1,2::2) |
| 101 |
! print *, s0, s1, s2, s3, s4 |
| 102 |
if (s1 /= s0) stop 15 |
| 103 |
if (s2 /= s0) stop 16 |
| 104 |
if (s3 /= s0) stop 17 |
| 105 |
if (s4 /= s0) stop 18 |
| 106 |
|
| 107 |
! Negative non-unit stride |
| 108 |
s0 = concat (str1(3:1:-2)) |
| 109 |
write(s1,'(4A)') a(1,3:1:-2) |
| 110 |
write(s2,'(4A)') b(1,3:1:-2) |
| 111 |
write(s3,'(4A)') c(1,3:1:-2) |
| 112 |
write(s4,'(4A)') d(1,3:1:-2) |
| 113 |
! print *, s0, s1, s2, s3, s4 |
| 114 |
if (s1 /= s0) stop 21 |
| 115 |
if (s2 /= s0) stop 22 |
| 116 |
if (s3 /= s0) stop 23 |
| 117 |
if (s4 /= s0) stop 24 |
| 118 |
s0 = concat (str1(4:1:-2)) |
| 119 |
write(s1,'(4A)') a(1,4:1:-2) |
| 120 |
write(s2,'(4A)') b(1,4:1:-2) |
| 121 |
write(s3,'(4A)') c(1,4:1:-2) |
| 122 |
write(s4,'(4A)') d(1,4:1:-2) |
| 123 |
! print *, s0, s1, s2, s3, s4 |
| 124 |
if (s1 /= s0) stop 25 |
| 125 |
if (s2 /= s0) stop 26 |
| 126 |
if (s3 /= s0) stop 27 |
| 127 |
if (s4 /= s0) stop 28 |
| 128 |
deallocate (a,b,c,d) |
| 129 |
|
| 130 |
! More complex cases with shape=[2,4] |
| 131 |
allocate (e, source = reshape (str2,[2,size(str2,2)])) |
| 132 |
allocate (f, source = reshape (str2,[2,size(str2,2)])) |
| 133 |
allocate (g, source = reshape (str5,[2,size(str5,2)])) |
| 134 |
allocate (h, source = reshape (str5,[2,size(str5,2)])) ! fixed with pr121939 |
| 135 |
! h => g |
| 136 |
s0 = concat (str2(1,3:1:-2)) |
| 137 |
write(s1,'(4A)') e(1,3:1:-2) |
| 138 |
write(s2,'(4A)') f(1,3:1:-2) |
| 139 |
write(s3,'(4A)') g(1,3:1:-2) |
| 140 |
write(s4,'(4A)') h(1,3:1:-2) |
| 141 |
! print *, s0, s1, s2, s3, s4 |
| 142 |
if (s1 /= s0) stop 31 |
| 143 |
if (s2 /= s0) stop 32 |
| 144 |
if (s3 /= s0) stop 33 |
| 145 |
if (s4 /= s0) stop 34 |
| 146 |
s0 = concat (str2(1,4:1:-2)) |
| 147 |
write(s1,'(4A)') e(1,4:1:-2) |
| 148 |
write(s2,'(4A)') f(1,4:1:-2) |
| 149 |
write(s3,'(4A)') g(1,4:1:-2) |
| 150 |
write(s4,'(4A)') h(1,4:1:-2) |
| 151 |
! print *, s0, s1, s2, s3, s4 |
| 152 |
if (s1 /= s0) stop 35 |
| 153 |
if (s2 /= s0) stop 36 |
| 154 |
if (s3 /= s0) stop 37 |
| 155 |
if (s4 /= s0) stop 38 |
| 156 |
|
| 157 |
s0 = concat (str2(2,3:1:-2)) |
| 158 |
write(s1,'(4A)') e(2,3:1:-2) |
| 159 |
write(s2,'(4A)') f(2,3:1:-2) |
| 160 |
write(s3,'(4A)') g(2,3:1:-2) |
| 161 |
write(s4,'(4A)') h(2,3:1:-2) |
| 162 |
! print *, s0, s1, s2, s3, s4 |
| 163 |
if (s1 /= s0) stop 41 |
| 164 |
if (s2 /= s0) stop 42 |
| 165 |
if (s3 /= s0) stop 43 |
| 166 |
if (s4 /= s0) stop 44 |
| 167 |
s0 = concat (str2(2,4:1:-2)) |
| 168 |
write(s1,'(4A)') e(2,4:1:-2) |
| 169 |
write(s2,'(4A)') f(2,4:1:-2) |
| 170 |
write(s3,'(4A)') g(2,4:1:-2) |
| 171 |
write(s4,'(4A)') h(2,4:1:-2) |
| 172 |
! print *, s0, s1, s2, s3, s4 |
| 173 |
if (s1 /= s0) stop 45 |
| 174 |
if (s2 /= s0) stop 46 |
| 175 |
if (s3 /= s0) stop 47 |
| 176 |
if (s4 /= s0) stop 48 |
| 177 |
|
| 178 |
! Check pointer association with negative stride |
| 179 |
a => e(2:1:-1,4:1:-1) |
| 180 |
b => f(2:1:-1,4:1:-1) |
| 181 |
c => g(2:1:-1,4:1:-1) |
| 182 |
d => h(2:1:-1,4:1:-1) |
| 183 |
|
| 184 |
s0 = concat (str2(2,4:1:-2)) |
| 185 |
write(s1,'(4A)') a(1,1::2) |
| 186 |
write(s2,'(4A)') b(1,1::2) |
| 187 |
write(s3,'(4A)') c(1,1::2) |
| 188 |
write(s4,'(4A)') d(1,1::2) |
| 189 |
! print *, s0, s1, s2, s3, s4 |
| 190 |
if (s1 /= s0) stop 51 |
| 191 |
if (s2 /= s0) stop 52 |
| 192 |
if (s3 /= s0) stop 53 |
| 193 |
if (s4 /= s0) stop 54 |
| 194 |
s0 = concat (str2(2,3:1:-2)) |
| 195 |
write(s1,'(4A)') a(1,2::2) |
| 196 |
write(s2,'(4A)') b(1,2::2) |
| 197 |
write(s3,'(4A)') c(1,2::2) |
| 198 |
write(s4,'(4A)') d(1,2::2) |
| 199 |
! print *, s0, s1, s2, s3, s4 |
| 200 |
if (s1 /= s0) stop 55 |
| 201 |
if (s2 /= s0) stop 56 |
| 202 |
if (s3 /= s0) stop 57 |
| 203 |
if (s4 /= s0) stop 58 |
| 204 |
|
| 205 |
s0 = concat (str2(1,4:1:-2)) |
| 206 |
write(s1,'(4A)') a(2,1::2) |
| 207 |
write(s2,'(4A)') b(2,1::2) |
| 208 |
write(s3,'(4A)') c(2,1::2) |
| 209 |
write(s4,'(4A)') d(2,1::2) |
| 210 |
! print *, s0, s1, s2, s3, s4 |
| 211 |
if (s1 /= s0) stop 61 |
| 212 |
if (s2 /= s0) stop 62 |
| 213 |
if (s3 /= s0) stop 63 |
| 214 |
if (s4 /= s0) stop 64 |
| 215 |
s0 = concat (str2(1,3:1:-2)) |
| 216 |
write(s1,'(4A)') a(2,2::2) |
| 217 |
write(s2,'(4A)') b(2,2::2) |
| 218 |
write(s3,'(4A)') c(2,2::2) |
| 219 |
write(s4,'(4A)') d(2,2::2) |
| 220 |
! print *, s0, s1, s2, s3, s4 |
| 221 |
if (s1 /= s0) stop 65 |
| 222 |
if (s2 /= s0) stop 66 |
| 223 |
if (s3 /= s0) stop 67 |
| 224 |
if (s4 /= s0) stop 68 |
| 225 |
deallocate (e,f,g,h) |
| 226 |
|
| 227 |
contains |
| 228 |
|
| 229 |
! Helper function to concatenate string array to scalar string |
| 230 |
function concat (s) |
| 231 |
character(len=:), allocatable :: concat |
| 232 |
character(len=*), intent(in) :: s(:) |
| 233 |
integer :: i, l, n |
| 234 |
n = size (s) |
| 235 |
l = len (s) |
| 236 |
allocate (character(len=l*n) :: concat) |
| 237 |
do i = 1, n |
| 238 |
concat(1+(i-1)*l:i*l) = s(i) |
| 239 |
end do |
| 240 |
end function concat |
| 241 |
end |