1! RUN: %python %S/test_errors.py %s %flang_fc1
2! Test for checking select type constraints,
3module m1
4  use ISO_C_BINDING
5  type shape
6    integer :: color
7    logical :: filled
8    integer :: x
9    integer :: y
10  end type shape
11
12  type, extends(shape) :: rectangle
13    integer :: length
14    integer :: width
15  end type rectangle
16
17  type, extends(rectangle) :: square
18  end type square
19
20  type, extends(square) :: extsquare
21  end type
22
23  type :: unrelated
24    logical :: some_logical
25  end type
26
27  type withSequence
28    SEQUENCE
29    integer :: x
30  end type
31
32  type, BIND(C) :: withBind
33    INTEGER(c_int) ::int_in_c
34  end type
35
36  TYPE(shape), TARGET :: shape_obj
37  TYPE(rectangle), TARGET :: rect_obj
38  TYPE(square), TARGET :: squr_obj
39  !define polymorphic objects
40  class(*), pointer :: unlim_polymorphic
41  class(shape), pointer :: shape_lim_polymorphic
42end
43module m
44  type :: t(n)
45    integer, len :: n
46  end type
47contains
48  subroutine CheckC1160( a )
49    class(*), intent(in) :: a
50    select type ( a )
51      !ERROR: The type specification statement must have LEN type parameter as assumed
52      type is ( character(len=10) ) !<-- assumed length-type
53      ! OK
54      type is ( character(len=*) )
55      !ERROR: The type specification statement must have LEN type parameter as assumed
56      type is ( t(n=10) )
57      ! OK
58      type is ( t(n=*) )   !<-- assumed length-type
59      !ERROR: Derived type 'character' not found
60      class is ( character(len=10) ) !<-- assumed length-type
61    end select
62  end subroutine
63
64  subroutine s()
65    type derived(param)
66      integer, len :: param
67      class(*), allocatable :: x
68    end type
69    TYPE(derived(10)) :: a
70    select type (ax => a%x)
71      class is (derived(param=*))
72        print *, "hello"
73    end select
74  end subroutine s
75end module
76
77subroutine CheckC1157
78  use m1
79  integer, parameter :: const_var=10
80  !ERROR: Selector is not a named variable: 'associate-name =>' is required
81  select type(10)
82  end select
83  !ERROR: Selector is not a named variable: 'associate-name =>' is required
84  select type(const_var)
85  end select
86  !ERROR: Selector is not a named variable: 'associate-name =>' is required
87  select type (4.999)
88  end select
89  !ERROR: Selector is not a named variable: 'associate-name =>' is required
90  select type (shape_obj%x)
91  end select
92end subroutine
93
94!CheckPloymorphicSelectorType
95subroutine CheckC1159a
96  integer :: int_variable
97  real :: real_variable
98  complex :: complex_var = cmplx(3.0, 4.0)
99  logical :: log_variable
100  character (len=10) :: char_variable = "OM"
101  !ERROR: Selector 'int_variable' in SELECT TYPE statement must be polymorphic
102  select type (int_variable)
103  end select
104  !ERROR: Selector 'real_variable' in SELECT TYPE statement must be polymorphic
105  select type (real_variable)
106  end select
107  !ERROR: Selector 'complex_var' in SELECT TYPE statement must be polymorphic
108  select type(complex_var)
109  end select
110  !ERROR: Selector 'logical_variable' in SELECT TYPE statement must be polymorphic
111  select type(logical_variable)
112  end select
113  !ERROR: Selector 'char_variable' in SELECT TYPE statement must be polymorphic
114  select type(char_variable)
115  end select
116end
117
118subroutine CheckC1159b
119  integer :: x
120  !ERROR: Selector 'x' in SELECT TYPE statement must be polymorphic
121  select type (a => x)
122  !ERROR: If selector is not unlimited polymorphic, an intrinsic type specification must not be specified in the type guard statement
123  type is (integer)
124    print *,'integer ',a
125  end select
126end
127
128subroutine CheckC1159c
129  !ERROR: Selector 'x' in SELECT TYPE statement must be polymorphic
130  select type (a => x)
131  !ERROR: If selector is not unlimited polymorphic, an intrinsic type specification must not be specified in the type guard statement
132  type is (integer)
133    print *,'integer ',a
134  end select
135end
136
137subroutine s(arg)
138  class(*) :: arg
139    select type (arg)
140        type is (integer)
141    end select
142end
143
144subroutine CheckC1161
145  use m1
146  shape_lim_polymorphic => rect_obj
147  select type(shape_lim_polymorphic)
148    !ERROR: The type specification statement must not specify a type with a SEQUENCE attribute or a BIND attribute
149    type is (withSequence)
150    !ERROR: The type specification statement must not specify a type with a SEQUENCE attribute or a BIND attribute
151    type is (withBind)
152  end select
153end
154
155subroutine CheckC1162
156  use m1
157  class(rectangle), pointer :: rectangle_polymorphic
158  !not unlimited polymorphic objects
159  select type (rectangle_polymorphic)
160    !ERROR: Type specification 'shape' must be an extension of TYPE 'rectangle'
161    type is (shape)
162    !ERROR: Type specification 'unrelated' must be an extension of TYPE 'rectangle'
163    type is (unrelated)
164    !all are ok
165    type is (square)
166    type is (extsquare)
167    !Handle same types
168    type is (rectangle)
169    !ERROR: If selector is not unlimited polymorphic, an intrinsic type specification must not be specified in the type guard statement
170    type is(integer)
171    !ERROR: If selector is not unlimited polymorphic, an intrinsic type specification must not be specified in the type guard statement
172    type is(real)
173    !ERROR: If selector is not unlimited polymorphic, an intrinsic type specification must not be specified in the type guard statement
174    type is(logical)
175    !ERROR: If selector is not unlimited polymorphic, an intrinsic type specification must not be specified in the type guard statement
176    type is(character(len=*))
177    !ERROR: If selector is not unlimited polymorphic, an intrinsic type specification must not be specified in the type guard statement
178    type is(complex)
179  end select
180
181  !Unlimited polymorphic objects are allowed.
182  unlim_polymorphic => rect_obj
183  select type (unlim_polymorphic)
184    type is (shape)
185    type is (unrelated)
186  end select
187end
188
189module c1162a
190  type pdt(kind,len)
191    integer, kind :: kind
192    integer, len :: len
193  end type
194 contains
195  subroutine foo(x)
196    class(pdt(kind=1,len=:)), allocatable :: x
197    select type (x)
198    type is (pdt(kind=1, len=*))
199    !ERROR: Type specification 'pdt(kind=2_4,len=*)' must be an extension of TYPE 'pdt(kind=1_4,len=:)'
200    type is (pdt(kind=2, len=*))
201    !ERROR: Type specification 'pdt(kind=*,len=*)' must be an extension of TYPE 'pdt(kind=1_4,len=:)'
202    type is (pdt(kind=*, len=*))
203    end select
204  end subroutine
205end module
206
207subroutine CheckC1163
208  use m1
209  !assign dynamically
210  shape_lim_polymorphic => rect_obj
211  unlim_polymorphic => shape_obj
212  select type (shape_lim_polymorphic)
213    type is (shape)
214    !ERROR: Type specification 'shape' conflicts with previous type specification
215    type is (shape)
216    class is (square)
217    !ERROR: Type specification 'square' conflicts with previous type specification
218    class is (square)
219  end select
220  select type (unlim_polymorphic)
221    type is (INTEGER(4))
222    type is (shape)
223    !ERROR: Type specification 'INTEGER(4)' conflicts with previous type specification
224    type is (INTEGER(4))
225  end select
226end
227
228subroutine CheckC1164
229  use m1
230  shape_lim_polymorphic => rect_obj
231  unlim_polymorphic => shape_obj
232  select type (shape_lim_polymorphic)
233    CLASS DEFAULT
234    !ERROR: Type specification 'DEFAULT' conflicts with previous type specification
235    CLASS DEFAULT
236    TYPE IS (shape)
237    TYPE IS (rectangle)
238    !ERROR: Type specification 'DEFAULT' conflicts with previous type specification
239    CLASS DEFAULT
240  end select
241
242  !Saving computation if some error in guard by not computing RepeatingCases
243  select type (shape_lim_polymorphic)
244    CLASS DEFAULT
245    CLASS DEFAULT
246    !ERROR: The type specification statement must not specify a type with a SEQUENCE attribute or a BIND attribute
247    TYPE IS(withSequence)
248  end select
249end subroutine
250
251subroutine WorkingPolymorphism
252  use m1
253  !assign dynamically
254  shape_lim_polymorphic => rect_obj
255  unlim_polymorphic => shape_obj
256  select type (shape_lim_polymorphic)
257    type is  (shape)
258      print *, "hello shape"
259    type is  (rectangle)
260      print *, "hello rect"
261    type is  (square)
262      print *, "hello square"
263    CLASS DEFAULT
264      print *, "default"
265  end select
266  print *, "unlim polymorphism"
267  select type (unlim_polymorphic)
268    type is  (shape)
269      print *, "hello shape"
270    type is  (rectangle)
271      print *, "hello rect"
272    type is  (square)
273      print *, "hello square"
274    CLASS DEFAULT
275      print *, "default"
276  end select
277end
278