1! RUN: %S/test_errors.sh %s %t %f18
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  type is (integer)
123    print *,'integer ',a
124  end select
125end
126
127subroutine CheckC1159c
128  !ERROR: Selector 'x' in SELECT TYPE statement must be polymorphic
129  select type (a => x)
130  type is (integer)
131    print *,'integer ',a
132  end select
133end
134
135subroutine s(arg)
136  class(*) :: arg
137    select type (arg)
138        type is (integer)
139    end select
140end
141
142subroutine CheckC1161
143  use m1
144  shape_lim_polymorphic => rect_obj
145  select type(shape_lim_polymorphic)
146    !ERROR: The type specification statement must not specify a type with a SEQUENCE attribute or a BIND attribute
147    type is (withSequence)
148    !ERROR: The type specification statement must not specify a type with a SEQUENCE attribute or a BIND attribute
149    type is (withBind)
150  end select
151end
152
153subroutine CheckC1162
154  use m1
155  class(rectangle), pointer :: rectangle_polymorphic
156  !not unlimited polymorphic objects
157  select type (rectangle_polymorphic)
158    !ERROR: Type specification 'shape' must be an extension of TYPE 'rectangle'
159    type is (shape)
160    !ERROR: Type specification 'unrelated' must be an extension of TYPE 'rectangle'
161    type is (unrelated)
162    !all are ok
163    type is (square)
164    type is (extsquare)
165    !Handle same types
166    type is (rectangle)
167  end select
168
169  !Unlimited polymorphic objects are allowed.
170  unlim_polymorphic => rect_obj
171  select type (unlim_polymorphic)
172    type is (shape)
173    type is (unrelated)
174  end select
175end
176
177subroutine CheckC1163
178  use m1
179  !assign dynamically
180  shape_lim_polymorphic => rect_obj
181  unlim_polymorphic => shape_obj
182  select type (shape_lim_polymorphic)
183    type is (shape)
184    !ERROR: Type specification 'shape' conflicts with previous type specification
185    type is (shape)
186    class is (square)
187    !ERROR: Type specification 'square' conflicts with previous type specification
188    class is (square)
189  end select
190end
191
192subroutine CheckC1164
193  use m1
194  shape_lim_polymorphic => rect_obj
195  unlim_polymorphic => shape_obj
196  select type (shape_lim_polymorphic)
197    CLASS DEFAULT
198    !ERROR: Type specification 'DEFAULT' conflicts with previous type specification
199    CLASS DEFAULT
200    TYPE IS (shape)
201    TYPE IS (rectangle)
202    !ERROR: Type specification 'DEFAULT' conflicts with previous type specification
203    CLASS DEFAULT
204  end select
205
206  !Saving computation if some error in guard by not computing RepeatingCases
207  select type (shape_lim_polymorphic)
208    CLASS DEFAULT
209    CLASS DEFAULT
210    !ERROR: The type specification statement must not specify a type with a SEQUENCE attribute or a BIND attribute
211    TYPE IS(withSequence)
212  end select
213end subroutine
214
215subroutine WorkingPolymorphism
216  use m1
217  !assign dynamically
218  shape_lim_polymorphic => rect_obj
219  unlim_polymorphic => shape_obj
220  select type (shape_lim_polymorphic)
221    type is  (shape)
222      print *, "hello shape"
223    type is  (rectangle)
224      print *, "hello rect"
225    type is  (square)
226      print *, "hello square"
227    CLASS DEFAULT
228      print *, "default"
229  end select
230  print *, "unlim polymorphism"
231  select type (unlim_polymorphic)
232    type is  (shape)
233      print *, "hello shape"
234    type is  (rectangle)
235      print *, "hello rect"
236    type is  (square)
237      print *, "hello square"
238    CLASS DEFAULT
239      print *, "default"
240  end select
241end
242