| 1 |
! { dg-do run } |
| 2 |
! { dg-options "-fdump-tree-original" } |
| 3 |
! |
| 4 |
! Tests the fix for PR89645 and 99065, in which derived type or class functions, |
| 5 |
! used as associate selectors and which were parsed after the containing scope |
| 6 |
! of the associate statement, caused "no IMPLICIT type" and "Syntax" errors. |
| 7 |
! |
| 8 |
! Contributed by Ian Harvey <[email protected]> |
| 9 |
! |
| 10 |
module m |
| 11 |
implicit none |
| 12 |
type t |
| 13 |
integer :: i = 0 |
| 14 |
end type t |
| 15 |
integer :: i = 0 |
| 16 |
type(t), parameter :: test_array (2) = [t(42),t(84)], & |
| 17 |
test_scalar = t(99) |
| 18 |
end module m |
| 19 |
|
| 20 |
! DERIVED TYPE VERSION OF THE PROBLEM, AS REPORTED IN THE PRs |
| 21 |
module type_selectors |
| 22 |
use m |
| 23 |
implicit none |
| 24 |
private |
| 25 |
public foo1 |
| 26 |
contains |
| 27 |
! Since these functions are parsed first, the symbols are available for |
| 28 |
! parsing in 'foo'. |
| 29 |
function bar1() result(res) ! The array version caused syntax errors in foo |
| 30 |
type(t), allocatable :: res(:) |
| 31 |
allocate (res, source = test_array) |
| 32 |
end |
| 33 |
function bar2() result(res) ! Scalar class functions were OK - test anyway |
| 34 |
type(t), allocatable :: res |
| 35 |
allocate (res, source = test_scalar) |
| 36 |
end |
| 37 |
subroutine foo1() |
| 38 |
! First the array selector |
| 39 |
associate (var1 => bar1()) |
| 40 |
if (any (var1%i .ne. test_array%i)) stop 1 |
| 41 |
if (var1(2)%i .ne. test_array(2)%i) stop 2 |
| 42 |
end associate |
| 43 |
! Now the scalar selector |
| 44 |
associate (var2 => bar2()) |
| 45 |
if (var2%i .ne. test_scalar%i) stop 3 |
| 46 |
end associate |
| 47 |
|
| 48 |
! Now the array selector that needed fixing up because the function follows.... |
| 49 |
associate (var1 => bar3()) |
| 50 |
if (any (var1%i .ne. test_array%i)) stop 4 |
| 51 |
if (var1(2)%i .ne. test_array(2)%i) stop 5 |
| 52 |
end associate |
| 53 |
! ....and equivalent scalar selector |
| 54 |
associate (var2 => bar4()) |
| 55 |
if (var2%i .ne. test_scalar%i) stop 6 |
| 56 |
end associate |
| 57 |
end subroutine foo1 |
| 58 |
|
| 59 |
! These functions are parsed after 'foo' so the symbols were not available |
| 60 |
! for the selectors and the fixup, tested here, was necessary. |
| 61 |
function bar3() result(res) |
| 62 |
class(t), allocatable :: res(:) |
| 63 |
allocate (res, source = test_array) |
| 64 |
end |
| 65 |
|
| 66 |
function bar4() result(res) |
| 67 |
class(t), allocatable :: res |
| 68 |
allocate (res, source = t(99)) |
| 69 |
end |
| 70 |
end module type_selectors |
| 71 |
|
| 72 |
! CLASS VERSION OF THE PROBLEM, WHICH REQUIRED MOST OF THE WORK! |
| 73 |
module class_selectors |
| 74 |
use m |
| 75 |
implicit none |
| 76 |
private |
| 77 |
public foo2 |
| 78 |
contains |
| 79 |
|
| 80 |
! Since these functions are parsed first, the symbols are available for |
| 81 |
! parsing in 'foo'. |
| 82 |
function bar1() result(res) ! The array version caused syntax errors in foo |
| 83 |
class(t), allocatable :: res(:) |
| 84 |
allocate (res, source = test_array) |
| 85 |
end |
| 86 |
|
| 87 |
function bar2() result(res) ! Scalar class functions were OK - test anyway |
| 88 |
class(t), allocatable :: res |
| 89 |
allocate (res, source = t(99)) |
| 90 |
end |
| 91 |
|
| 92 |
subroutine foo2() |
| 93 |
! First the array selector |
| 94 |
associate (var1 => bar1()) |
| 95 |
if (any (var1%i .ne. test_array%i)) stop 7 |
| 96 |
if (var1(2)%i .ne. test_array(2)%i) stop 8 |
| 97 |
select type (x => var1) |
| 98 |
type is (t) |
| 99 |
if (any (x%i .ne. test_array%i)) stop 9 |
| 100 |
if (x(1)%i .ne. test_array(1)%i) stop 10 |
| 101 |
class default |
| 102 |
stop 11 |
| 103 |
end select |
| 104 |
end associate |
| 105 |
|
| 106 |
! Now scalar selector |
| 107 |
associate (var2 => bar2()) |
| 108 |
select type (z => var2) |
| 109 |
type is (t) |
| 110 |
if (z%i .ne. test_scalar%i) stop 12 |
| 111 |
class default |
| 112 |
stop 13 |
| 113 |
end select |
| 114 |
end associate |
| 115 |
|
| 116 |
! This is the array selector that needed the fixup. |
| 117 |
associate (var1 => bar3()) |
| 118 |
if (any (var1%i .ne. test_array%i)) stop 14 |
| 119 |
if (var1(2)%i .ne. test_array(2)%i) stop 15 |
| 120 |
select type (x => var1) |
| 121 |
type is (t) |
| 122 |
if (any (x%i .ne. test_array%i)) stop 16 |
| 123 |
if (x(1)%i .ne. test_array(1)%i) stop 17 |
| 124 |
class default |
| 125 |
stop 18 |
| 126 |
end select |
| 127 |
end associate |
| 128 |
|
| 129 |
! Now the equivalent scalar selector |
| 130 |
associate (var2 => bar4()) |
| 131 |
select type (z => var2) |
| 132 |
type is (t) |
| 133 |
if (z%i .ne. test_scalar%i) stop 19 |
| 134 |
class default |
| 135 |
stop 20 |
| 136 |
end select |
| 137 |
end associate |
| 138 |
|
| 139 |
end subroutine foo2 |
| 140 |
|
| 141 |
! These functions are parsed after 'foo' so the symbols were not available |
| 142 |
! for the selectors and the fixup, tested here, was necessary. |
| 143 |
function bar3() result(res) |
| 144 |
class(t), allocatable :: res(:) |
| 145 |
allocate (res, source = test_array) |
| 146 |
end |
| 147 |
|
| 148 |
function bar4() result(res) |
| 149 |
class(t), allocatable :: res |
| 150 |
allocate (res, source = t(99)) |
| 151 |
end |
| 152 |
end module class_selectors |
| 153 |
|
| 154 |
! THESE TESTS CAUSED PROBLEMS DURING DEVELOPMENT FOR BOTH PARSING ORDERS. |
| 155 |
module problem_selectors |
| 156 |
implicit none |
| 157 |
private |
| 158 |
public foo3, foo4 |
| 159 |
type t |
| 160 |
integer :: i |
| 161 |
end type t |
| 162 |
type s |
| 163 |
integer :: i |
| 164 |
type(t) :: dt |
| 165 |
end type s |
| 166 |
type(t), parameter :: test_array (2) = [t(42),t(84)], & |
| 167 |
test_scalar = t(99) |
| 168 |
type(s), parameter :: test_sarray (2) = [s(142,t(42)),s(184,t(84))] |
| 169 |
contains |
| 170 |
|
| 171 |
subroutine foo3() |
| 172 |
integer :: i |
| 173 |
block |
| 174 |
associate (var1 => bar7()) |
| 175 |
if (any (var1%i .ne. test_array%i)) stop 21 |
| 176 |
if (var1(2)%i .ne. test_array(2)%i) stop 22 |
| 177 |
associate (z => var1(1)%i) |
| 178 |
if (z .ne. 42) stop 23 |
| 179 |
end associate |
| 180 |
end associate |
| 181 |
end block |
| 182 |
|
| 183 |
associate (var2 => bar8()) |
| 184 |
i = var2(2)%i |
| 185 |
associate (var3 => var2%dt) |
| 186 |
if (any (var3%i .ne. test_sarray%dt%i)) stop 24 |
| 187 |
end associate |
| 188 |
associate (var4 => var2(2)) |
| 189 |
if (var4%i .ne. 184) stop 25 |
| 190 |
end associate |
| 191 |
end associate |
| 192 |
end subroutine foo3 |
| 193 |
|
| 194 |
function bar7() result(res) |
| 195 |
type(t), allocatable :: res(:) |
| 196 |
allocate (res, source = test_array) |
| 197 |
end |
| 198 |
|
| 199 |
function bar8() result(res) |
| 200 |
type(s), allocatable :: res(:) |
| 201 |
allocate (res, source = test_sarray) |
| 202 |
end |
| 203 |
|
| 204 |
subroutine foo4() |
| 205 |
integer :: i |
| 206 |
block |
| 207 |
associate (var1 => bar7()) |
| 208 |
if (any (var1%i .ne. test_array%i)) stop 26 |
| 209 |
if (var1(2)%i .ne. test_array(2)%i) stop 27 |
| 210 |
associate (z => var1(1)%i) |
| 211 |
if (z .ne. 42) stop 28 |
| 212 |
end associate |
| 213 |
end associate |
| 214 |
end block |
| 215 |
|
| 216 |
associate (var2 => bar8()) |
| 217 |
i = var2(2)%i |
| 218 |
associate (var3 => var2%dt) |
| 219 |
if (any (var3%i .ne. test_sarray%dt%i)) stop 29 |
| 220 |
end associate |
| 221 |
associate (var4 => var2(2)) |
| 222 |
if (var4%i .ne. 184) stop 30 |
| 223 |
end associate |
| 224 |
end associate |
| 225 |
end subroutine foo4 |
| 226 |
|
| 227 |
end module problem_selectors |
| 228 |
|
| 229 |
module more_problem_selectors |
| 230 |
implicit none |
| 231 |
private |
| 232 |
public foo5, foo6 |
| 233 |
type t |
| 234 |
integer :: i = 0 |
| 235 |
end type t |
| 236 |
type s |
| 237 |
integer :: i = 0 |
| 238 |
type(t) :: dt |
| 239 |
end type s |
| 240 |
contains |
| 241 |
! In this version, the order of declarations of 't' and 's' is such that |
| 242 |
! parsing var%i sets the type of var to 't' and this is corrected to 's' |
| 243 |
! on parsing var%dt%i |
| 244 |
subroutine foo5() |
| 245 |
associate (var3 => bar3()) |
| 246 |
if (var3%i .ne. 42) stop 31 |
| 247 |
if (var3%dt%i .ne. 84) stop 32 |
| 248 |
end associate |
| 249 |
|
| 250 |
! Repeat with class version |
| 251 |
associate (var4 => bar4()) |
| 252 |
if (var4%i .ne. 84) stop 33 |
| 253 |
if (var4%dt%i .ne. 168) stop 34 |
| 254 |
select type (x => var4) |
| 255 |
type is (s) |
| 256 |
if (x%i .ne. var4%i) stop 35 |
| 257 |
if (x%dt%i .ne. var4%dt%i) stop 36 |
| 258 |
class default |
| 259 |
stop 37 |
| 260 |
end select |
| 261 |
end associate |
| 262 |
|
| 263 |
! Ditto with no type component clues for select type |
| 264 |
associate (var5 => bar4()) |
| 265 |
select type (z => var5) |
| 266 |
type is (s) |
| 267 |
if (z%i .ne. 84) stop 38 |
| 268 |
if (z%dt%i .ne. 168) stop 39 |
| 269 |
class default |
| 270 |
stop 40 |
| 271 |
end select |
| 272 |
end associate |
| 273 |
end subroutine foo5 |
| 274 |
|
| 275 |
! Now the array versions |
| 276 |
subroutine foo6() |
| 277 |
class(s), allocatable :: elem |
| 278 |
associate (var6 => bar5()) |
| 279 |
if (var6(1)%i .ne. 42) stop 41 |
| 280 |
if (any (var6%dt%i .ne. [84])) stop 42 |
| 281 |
end associate |
| 282 |
|
| 283 |
! Class version with an assignment to a named variable |
| 284 |
associate (var7 => bar6()) |
| 285 |
elem = var7(2) |
| 286 |
if (any (var7%i .ne. [84, 168])) stop 43 |
| 287 |
if (any (var7%dt%i .ne. [168, 336])) stop 44 |
| 288 |
end associate |
| 289 |
if (elem%i .ne. 168) stop 45 |
| 290 |
if (elem%dt%i .ne. 336) stop 46 |
| 291 |
|
| 292 |
select type (z => elem) |
| 293 |
type is (s) |
| 294 |
if (z%i .ne. 168) stop 47 |
| 295 |
if (z%dt%i .ne. 336) stop 48 |
| 296 |
class default |
| 297 |
stop 49 |
| 298 |
end select |
| 299 |
|
| 300 |
! Array version without type clues before select type |
| 301 |
associate (var8 => bar6()) |
| 302 |
select type (z => var8) |
| 303 |
type is (s) |
| 304 |
if (any (z%i .ne. [84,168])) stop 50 |
| 305 |
if (any (z%dt%i .ne. [168,336])) stop 51 |
| 306 |
class default |
| 307 |
stop 52 |
| 308 |
end select |
| 309 |
end associate |
| 310 |
end subroutine foo6 |
| 311 |
|
| 312 |
type(s) function bar3() |
| 313 |
bar3= s(42, t(84)) |
| 314 |
end |
| 315 |
|
| 316 |
function bar4() result(res) |
| 317 |
class(s), allocatable :: res |
| 318 |
res = s(84, t(168)) |
| 319 |
end |
| 320 |
|
| 321 |
function bar5() result (res) |
| 322 |
type(s), allocatable :: res(:) |
| 323 |
res = [s(42, t(84))] |
| 324 |
end |
| 325 |
|
| 326 |
function bar6() result (res) |
| 327 |
class(s), allocatable :: res(:) |
| 328 |
res = [s(84, t(168)),s(168, t(336))] |
| 329 |
end |
| 330 |
|
| 331 |
end module more_problem_selectors |
| 332 |
|
| 333 |
program test |
| 334 |
use type_selectors |
| 335 |
use class_selectors |
| 336 |
use problem_selectors |
| 337 |
use more_problem_selectors |
| 338 |
call foo1() |
| 339 |
call foo2() |
| 340 |
call foo3() |
| 341 |
call foo4() |
| 342 |
call foo5() |
| 343 |
call foo6() |
| 344 |
end program test |
| 345 |
! { dg-final { scan-tree-dump-times "__builtin_free" 18 "original" } } |