xref: /llvm-project/flang/test/Semantics/allocate09.f90 (revision 1c91d9bdea3b6c38e8fbce46ec8181a9c0aa26f8)
1! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic
2! Check for semantic errors in ALLOCATE statements
3
4subroutine C946(param_ca_4_assumed, param_ta_4_assumed, param_ca_4_deferred)
5! If source-expr appears, the kind type parameters of each allocate-object shall
6! have the same values as the corresponding type parameters of source-expr.
7
8  real(kind=4), allocatable :: x1, x2(:)
9
10  type WithParam(k1, l1)
11    integer, kind :: k1=1
12    integer, len :: l1=2
13    real x
14  end type
15
16  type, extends(WithParam) :: WithParamExtent(k2, l2)
17    integer, kind :: k2
18    integer, len :: l2
19  end type
20
21  type, extends(WithParamExtent) :: WithParamExtent2(k3, l3)
22    integer, kind :: k3 = 8
23    integer, len :: l3
24  end type
25
26  real(kind=4) srcx, srcx_array(10)
27  real(kind=8) srcx8, srcx8_array(10)
28  class(WithParam(4, 2)), allocatable :: src_a_4_2
29  type(WithParam(8, 2)) src_a_8_2
30  class(WithParam(4, :)), allocatable :: src_a_4_def
31  class(WithParam(8, :)), allocatable :: src_a_8_def
32  type(WithParamExtent(4, 2, 8, 3)) src_b_4_2_8_3
33  class(WithParamExtent(4, :, 8, 3)), allocatable :: src_b_4_def_8_3
34  type(WithParamExtent(8, 2, 8, 3)) src_b_8_2_8_3
35  class(WithParamExtent(8, :, 8, 3)), allocatable :: src_b_8_def_8_3
36  type(WithParamExtent2(k1=4, l1=5, k2=5, l2=6, l3=8 )) src_c_4_5_5_6_8_8
37  class(WithParamExtent2(k1=4, l1=2, k2=5, l2=6, k3=5, l3=8)), &
38      allocatable :: src_c_4_2_5_6_5_8
39  class(WithParamExtent2(k2=5, l2=6, k3=5, l3=8)), &
40      allocatable :: src_c_1_2_5_6_5_8
41  type(WithParamExtent2(k1=5, l1=5, k2=5, l2=6, l3=8 )) src_c_5_5_5_6_8_8
42  type(WithParamExtent2(k1=5, l1=2, k2=5, l2=6, k3=5, l3=8)) src_c_5_2_5_6_5_8
43
44
45  type(WithParam(4, 2)), allocatable :: param_ta_4_2
46  class(WithParam(4, 2)), pointer :: param_ca_4_2
47
48  type(WithParam(4, *)), pointer :: param_ta_4_assumed
49  class(WithParam(4, *)), allocatable :: param_ca_4_assumed
50
51  type(WithParam(4, :)), allocatable :: param_ta_4_deferred
52  class(WithParam(4, :)), pointer :: param_ca_4_deferred
53  class(WithParam), allocatable :: param_defaulted
54  integer, allocatable :: integer_default(:)
55
56  type(WithParamExtent2(k1=4, l1=:, k2=5, l2=:, l3=8 )), pointer :: extended2
57
58  class(*), pointer :: whatever
59
60  character(:), allocatable :: deferredChar
61  character(2), allocatable :: char2
62
63  ! Nominal test cases
64  allocate(x1, x2(10), source=srcx)
65  allocate(x2(10), source=srcx_array)
66  allocate(param_ta_4_2, param_ca_4_2, mold=src_a_4_2)
67  allocate(param_ca_4_2, source=src_b_4_2_8_3)
68  allocate(param_ta_4_2, param_ca_4_2, mold=src_a_4_def) ! no C935 equivalent for source-expr
69  allocate(param_ca_4_2, source=src_b_4_def_8_3) ! no C935 equivalent for source-expr
70  allocate(param_ta_4_assumed, param_ca_4_assumed, source=src_a_4_def)
71  allocate(param_ca_4_assumed, mold=src_b_4_def_8_3)
72  allocate(param_ta_4_assumed, param_ca_4_assumed, source=src_a_4_2) ! no C935 equivalent for source-expr
73  allocate(param_ca_4_assumed, mold=src_b_4_2_8_3) ! no C935 equivalent for source-expr
74  allocate(param_ta_4_deferred, param_ca_4_deferred, source =src_a_4_2)
75  allocate(param_ca_4_deferred, mold=src_b_4_def_8_3)
76
77  allocate(extended2, source=src_c_4_5_5_6_8_8)
78  allocate(param_ca_4_2, mold= src_c_4_2_5_6_5_8)
79  allocate(param_defaulted, mold=WithParam(5))
80  allocate(param_defaulted, source=WithParam(k1=1)(x=5))
81  allocate(param_defaulted, mold=src_c_1_2_5_6_5_8)
82  allocate(whatever, source=src_c_1_2_5_6_5_8)
83
84  allocate(integer_default, source=[(i,i=0,9)])
85
86  allocate(deferredChar, source="abcd")
87  allocate(deferredChar, mold=deferredChar)
88  !PORTABILITY: Character length of allocatable object in ALLOCATE should be the same as the SOURCE or MOLD
89  allocate(char2, source="a")
90  !PORTABILITY: Character length of allocatable object in ALLOCATE should be the same as the SOURCE or MOLD
91  allocate(char2, source="abc")
92  allocate(char2, mold=deferredChar)
93
94  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
95  allocate(x1, source=cos(0._8))
96  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
97  allocate(x2(10), source=srcx8)
98  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
99  allocate(x2(10), mold=srcx8_array)
100  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
101  allocate(param_ta_4_2, source=src_a_8_2)
102  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
103  allocate(param_ca_4_2, mold=src_a_8_2)
104  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
105  allocate(param_ta_4_2, source=src_a_8_def)
106  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
107  allocate(param_ca_4_2, source=src_b_8_2_8_3)
108  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
109  allocate(param_ca_4_2, mold=src_b_8_def_8_3)
110  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
111  allocate(param_ta_4_assumed, source=src_a_8_def)
112  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
113  allocate(param_ta_4_assumed, mold=src_a_8_2)
114  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
115  allocate(param_ca_4_assumed, mold=src_a_8_def)
116  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
117  allocate(param_ca_4_assumed, source=src_b_8_2_8_3)
118  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
119  allocate(param_ta_4_deferred, mold=src_a_8_2)
120  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
121  allocate(param_ca_4_deferred, source=src_a_8_def)
122  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
123  allocate(param_ca_4_deferred, mold=src_b_8_2_8_3)
124  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
125  allocate(extended2, source=src_c_5_5_5_6_8_8)
126  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
127  allocate(param_ca_4_2, mold=src_c_5_2_5_6_5_8)
128  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
129  allocate(extended2, source=WithParamExtent2(k1=4, l1=5, k2=5, l2=6, k3=5, l3=8)(x=5))
130  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
131  allocate(param_ca_4_2, mold=param_defaulted)
132  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
133  allocate(param_defaulted, source=param_ca_4_2)
134  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
135  allocate(param_defaulted, mold=WithParam(k1=2)(x=5))
136  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
137  allocate(param_defaulted, source=src_c_5_2_5_6_5_8)
138  !ERROR: Derived type parameters of allocatable object must be the same as the corresponding ones of SOURCE or MOLD expression
139  allocate(integer_default, source=[(i, integer(8)::i=0,9)])
140end subroutine
141