xref: /llvm-project/flang/test/Semantics/allocate12.f90 (revision 6c1ac141d3c98af9738bc77fcb55602cbff7751f)
1! RUN: %python %S/test_errors.py %s %flang_fc1
2! Check for semantic errors in ALLOCATE statements
3
4subroutine C941_C942b_C950(xsrc, x1, a2, b2, cx1, ca2, cb1, cb2, c1, c2)
5! C941: An allocate-coarray-spec shall appear if and only if the allocate-object
6! is a coarray.
7  type type0
8    real, allocatable :: array(:)
9  end type
10  type type1
11    class(type0), pointer :: t0
12  end type
13
14  type type2
15    type(type1), pointer :: t1(:)
16  end type
17
18  type A
19    real x(10)
20  end type
21
22  type B
23    real, allocatable :: x(:)
24  end type
25
26  type C
27    class(type2), allocatable :: ct2(:, :)[:, :, :]
28    class(A), allocatable :: cx(:, :)[:, :, :]
29    class(A), allocatable :: x(:, :)
30  end type
31
32  real :: xsrc(10)
33  real, allocatable :: x1, x2(:)
34  class(A), pointer :: a1, a2(:)
35
36  real, allocatable :: cx1[:], cx2(:)[:, :]
37  class(A), allocatable :: ca1[:, :], ca2(:)[:]
38
39  type(B) :: b1, b2(*)
40  type(B) :: cb1[5:*], cb2(*)[2, -1:*]
41
42  type(C) :: c1
43  pointer :: c2(:, :)
44  pointer :: varLocal(:)
45
46  class(*), allocatable :: var(:), cvar(:)[:]
47
48  ! Valid constructs
49  allocate(x1, x2(10), cx1[*], cx2(10)[2, -1:*])
50  allocate(a1, a2(10), ca1[2, -1:*], ca2(10)[*])
51  allocate(b1%x, b2(1)%x, cb1%x, cb2(1)%x, SOURCE=xsrc)
52  allocate(c1%x(-1:10, 1:5), c1%cx(-1:10, 1:5)[-1:5, 1:2, 2:*])
53  allocate(c2(9, 27))
54  allocate(varLocal(64))
55  allocate(A:: var(5), cvar(10)[*])
56
57
58  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
59  allocate(x1[*])
60  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
61  allocate(x2(10)[*])
62  !ERROR: Coarray specification must appear in ALLOCATE when allocatable object is a coarray
63  allocate(cx1)
64  !ERROR: Coarray specification must appear in ALLOCATE when allocatable object is a coarray
65  allocate(cx2(10))
66
67  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
68  allocate(cx1[*], a1[*])
69  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
70  allocate(cx1[*], a2(10)[*])
71  !ERROR: Coarray specification must appear in ALLOCATE when allocatable object is a coarray
72  allocate(x1, ca1)
73  !ERROR: Coarray specification must appear in ALLOCATE when allocatable object is a coarray
74  allocate(ca1[2, -1:*], ca2(10))
75
76  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
77  allocate(b1%x[5:*] , SOURCE=xsrc)
78  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
79  allocate(b2(1)%x[2, -1:*], SOURCE=xsrc)
80  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
81  allocate(cb1%x[5:*] , SOURCE=xsrc)
82  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
83  allocate(cb2(1)%x[2, -1:*], SOURCE=xsrc)
84
85  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
86  allocate(c1%x(-1:10, 1:5)[-1:5, 1:2, 2:*])
87  !ERROR: Coarray specification must appear in ALLOCATE when allocatable object is a coarray
88  allocate(c1%cx(-1:10, 1:5))
89
90  !ERROR: Coarray specification must not appear in ALLOCATE when allocatable object is not a coarray
91  allocate(A:: var(5)[*], cvar(10)[*])
92  !ERROR: Coarray specification must appear in ALLOCATE when allocatable object is a coarray
93  allocate(A:: var(5), cvar(10))
94
95! C942b: [... (shape related stuff not tested here) ...]. The number of
96! allocate-coshape-specs in an allocate-coarray-spec shall be one less
97! than the corank of the allocate-object.
98
99  ! Valid constructs already tested above
100
101  !ERROR: Corank of coarray specification in ALLOCATE must match corank of alloctable coarray
102  allocate(cx1[2,-1:*], cx2(10)[2, -1:*])
103  !ERROR: Corank of coarray specification in ALLOCATE must match corank of alloctable coarray
104  allocate(ca1[*], ca2(10)[*])
105  !ERROR: Corank of coarray specification in ALLOCATE must match corank of alloctable coarray
106  allocate(c1%cx(-1:10, 1:5)[-1:5, 1:*])
107  !ERROR: Corank of coarray specification in ALLOCATE must match corank of alloctable coarray
108  allocate(A:: cvar(10)[2,2,*])
109
110! C950: An allocate-object shall not be a coindexed object.
111
112  ! Valid construct
113  allocate(c1%ct2(2,5)%t1(2)%t0%array(10))
114
115  !ERROR: Allocatable object must not be coindexed in ALLOCATE
116  allocate(b1%x, b2(1)%x, cb1[2]%x, SOURCE=xsrc)
117  !ERROR: Allocatable object must not be coindexed in ALLOCATE
118  allocate(b1%x, b2(1)%x, cb2(1)[2,-1]%x, MOLD=xsrc)
119  !ERROR: Allocatable object must not be coindexed in ALLOCATE
120  allocate(c1%ct2(2,5)[1,1,1]%t1(2)%t0%array(10))
121
122end subroutine
123