1! RUN: %B/test/Semantics/test_errors.sh %s %flang %t
2! 15.4.3.4.5 Restrictions on generic declarations
3! Specific procedures of generic interfaces must be distinguishable.
4
5module m1
6  !ERROR: Generic 'g' may not have specific procedures 's2' and 's4' as their interfaces are not distinguishable
7  interface g
8    procedure s1
9    procedure s2
10    procedure s3
11    procedure s4
12  end interface
13contains
14  subroutine s1(x)
15    integer(8) x
16  end
17  subroutine s2(x)
18    integer x
19  end
20  subroutine s3
21  end
22  subroutine s4(x)
23    integer x
24  end
25end
26
27module m2
28  !ERROR: Generic 'g' may not have specific procedures 's1' and 's2' as their interfaces are not distinguishable
29  interface g
30    subroutine s1(x)
31    end subroutine
32    subroutine s2(x)
33      real x
34    end subroutine
35  end interface
36end
37
38module m3
39  !ERROR: Generic 'g' may not have specific procedures 'f1' and 'f2' as their interfaces are not distinguishable
40  interface g
41    integer function f1()
42    end function
43    real function f2()
44    end function
45  end interface
46end
47
48module m4
49  type :: t1
50  end type
51  type, extends(t1) :: t2
52  end type
53  interface g
54    subroutine s1(x)
55      import :: t1
56      type(t1) :: x
57    end
58    subroutine s2(x)
59      import :: t2
60      type(t2) :: x
61    end
62  end interface
63end
64
65! These are all different ranks so they are distinguishable
66module m5
67  interface g
68    subroutine s1(x)
69      real x
70    end subroutine
71    subroutine s2(x)
72      real x(:)
73    end subroutine
74    subroutine s3(x)
75      real x(:,:)
76    end subroutine
77  end interface
78end
79
80module m6
81  use m5
82  !ERROR: Generic 'g' may not have specific procedures 's1' and 's4' as their interfaces are not distinguishable
83  interface g
84    subroutine s4(x)
85    end subroutine
86  end interface
87end
88
89module m7
90  use m5
91  !ERROR: Generic 'g' may not have specific procedures 's1' and 's5' as their interfaces are not distinguishable
92  !ERROR: Generic 'g' may not have specific procedures 's2' and 's5' as their interfaces are not distinguishable
93  !ERROR: Generic 'g' may not have specific procedures 's3' and 's5' as their interfaces are not distinguishable
94  interface g
95    subroutine s5(x)
96      real x(..)
97    end subroutine
98  end interface
99end
100
101
102! Two procedures that differ only by attributes are not distinguishable
103module m8
104  !ERROR: Generic 'g' may not have specific procedures 's1' and 's2' as their interfaces are not distinguishable
105  interface g
106    pure subroutine s1(x)
107      real, intent(in) :: x
108    end subroutine
109    subroutine s2(x)
110      real, intent(in) :: x
111    end subroutine
112  end interface
113end
114
115module m9
116  !ERROR: Generic 'g' may not have specific procedures 's1' and 's2' as their interfaces are not distinguishable
117  interface g
118    subroutine s1(x)
119      real :: x(10)
120    end subroutine
121    subroutine s2(x)
122      real :: x(100)
123    end subroutine
124  end interface
125end
126
127module m10
128  !ERROR: Generic 'g' may not have specific procedures 's1' and 's2' as their interfaces are not distinguishable
129  interface g
130    subroutine s1(x)
131      real :: x(10)
132    end subroutine
133    subroutine s2(x)
134      real :: x(..)
135    end subroutine
136  end interface
137end
138
139program m11
140  interface g1
141    subroutine s1(x)
142      real, pointer, intent(out) :: x
143    end subroutine
144    subroutine s2(x)
145      real, allocatable :: x
146    end subroutine
147  end interface
148  !ERROR: Generic 'g2' may not have specific procedures 's3' and 's4' as their interfaces are not distinguishable
149  interface g2
150    subroutine s3(x)
151      real, pointer, intent(in) :: x
152    end subroutine
153    subroutine s4(x)
154      real, allocatable :: x
155    end subroutine
156  end interface
157end
158
159module m12
160  !ERROR: Generic 'g1' may not have specific procedures 's1' and 's2' as their interfaces are not distinguishable
161  generic :: g1 => s1, s2  ! rank-1 and assumed-rank
162  !ERROR: Generic 'g2' may not have specific procedures 's2' and 's3' as their interfaces are not distinguishable
163  generic :: g2 => s2, s3  ! scalar and assumed-rank
164  !ERROR: Generic 'g3' may not have specific procedures 's1' and 's4' as their interfaces are not distinguishable
165  generic :: g3 => s1, s4  ! different shape, same rank
166contains
167  subroutine s1(x)
168    real :: x(10)
169  end
170  subroutine s2(x)
171    real :: x(..)
172  end
173  subroutine s3(x)
174    real :: x
175  end
176  subroutine s4(x)
177    real :: x(100)
178  end
179end
180
181! Procedures that are distinguishable by return type of a dummy argument
182module m13
183  interface g1
184    procedure s1
185    procedure s2
186  end interface
187  interface g2
188    procedure s1
189    procedure s3
190  end interface
191contains
192  subroutine s1(x)
193    procedure(real), pointer :: x
194  end
195  subroutine s2(x)
196    procedure(integer), pointer :: x
197  end
198  subroutine s3(x)
199    interface
200      function x()
201        procedure(real), pointer :: x
202      end function
203    end interface
204  end
205end
206
207! Check user-defined operators
208module m14
209  interface operator(*)
210    module procedure f1
211    module procedure f2
212  end interface
213  !ERROR: Generic 'operator(+)' may not have specific procedures 'f1' and 'f3' as their interfaces are not distinguishable
214  interface operator(+)
215    module procedure f1
216    module procedure f3
217  end interface
218  interface operator(.foo.)
219    module procedure f1
220    module procedure f2
221  end interface
222  !ERROR: Generic operator '.bar.' may not have specific procedures 'f1' and 'f3' as their interfaces are not distinguishable
223  interface operator(.bar.)
224    module procedure f1
225    module procedure f3
226  end interface
227contains
228  real function f1(x, y)
229    real, intent(in) :: x
230    logical, intent(in) :: y
231  end
232  integer function f2(x, y)
233    integer, intent(in) :: x
234    logical, intent(in) :: y
235  end
236  real function f3(x, y)
237    real, value :: x
238    logical, value :: y
239  end
240end module
241
242! Types distinguished by kind (but not length) parameters
243module m15
244  type :: t1(k1, l1)
245    integer, kind :: k1 = 1
246    integer, len :: l1 = 101
247  end type
248
249  type, extends(t1) :: t2(k2a, l2, k2b)
250    integer, kind :: k2a = 2
251    integer, kind :: k2b = 3
252    integer, len :: l2 = 102
253  end type
254
255  type, extends(t2) :: t3(l3, k3)
256    integer, kind :: k3 = 4
257    integer, len :: l3 = 103
258  end type
259
260  interface g1
261    procedure s1
262    procedure s2
263  end interface
264  !ERROR: Generic 'g2' may not have specific procedures 's1' and 's3' as their interfaces are not distinguishable
265  interface g2
266    procedure s1
267    procedure s3
268  end interface
269  !ERROR: Generic 'g3' may not have specific procedures 's4' and 's5' as their interfaces are not distinguishable
270  interface g3
271    procedure s4
272    procedure s5
273  end interface
274  interface g4
275    procedure s5
276    procedure s6
277    procedure s9
278  end interface
279  interface g5
280    procedure s4
281    procedure s7
282    procedure s9
283  end interface
284  interface g6
285    procedure s5
286    procedure s8
287    procedure s9
288  end interface
289  !ERROR: Generic 'g7' may not have specific procedures 's6' and 's7' as their interfaces are not distinguishable
290  interface g7
291    procedure s6
292    procedure s7
293  end interface
294  !ERROR: Generic 'g8' may not have specific procedures 's6' and 's8' as their interfaces are not distinguishable
295  interface g8
296    procedure s6
297    procedure s8
298  end interface
299  !ERROR: Generic 'g9' may not have specific procedures 's7' and 's8' as their interfaces are not distinguishable
300  interface g9
301    procedure s7
302    procedure s8
303  end interface
304
305contains
306  subroutine s1(x)
307    type(t1(1, 4)) :: x
308  end
309  subroutine s2(x)
310    type(t1(2, 4)) :: x
311  end
312  subroutine s3(x)
313    type(t1(l1=5)) :: x
314  end
315  subroutine s4(x)
316    type(t3(1, 101, 2, 102, 3, 103, 4)) :: x
317  end subroutine
318  subroutine s5(x)
319    type(t3) :: x
320  end subroutine
321  subroutine s6(x)
322    type(t3(1, 99, k2b=2, k2a=3, l2=*, l3=97, k3=4)) :: x
323  end subroutine
324  subroutine s7(x)
325    type(t3(k1=1, l1=99, k2a=3, k2b=2, k3=4)) :: x
326  end subroutine
327  subroutine s8(x)
328    type(t3(1, :, 3, :, 2, :, 4)), allocatable :: x
329  end subroutine
330  subroutine s9(x)
331    type(t3(k1=2)) :: x
332  end subroutine
333end
334
335
336! Check that specifics for type-bound generics can be distinguished
337module m16
338  type :: t
339  contains
340    procedure, nopass :: s1
341    procedure, nopass :: s2
342    procedure, nopass :: s3
343    generic :: g1 => s1, s2
344    !ERROR: Generic 'g2' may not have specific procedures 's1' and 's3' as their interfaces are not distinguishable
345    generic :: g2 => s1, s3
346  end type
347contains
348  subroutine s1(x)
349    real :: x
350  end
351  subroutine s2(x)
352    integer :: x
353  end
354  subroutine s3(x)
355    real :: x
356  end
357end
358
359! Check polymorphic types
360module m17
361  type :: t
362  end type
363  type, extends(t) :: t1
364  end type
365  type, extends(t) :: t2
366  end type
367  type, extends(t2) :: t2a
368  end type
369  interface g1
370    procedure s1
371    procedure s2
372  end interface
373  !ERROR: Generic 'g2' may not have specific procedures 's3' and 's4' as their interfaces are not distinguishable
374  interface g2
375    procedure s3
376    procedure s4
377  end interface
378  interface g3
379    procedure s1
380    procedure s4
381  end interface
382  !ERROR: Generic 'g4' may not have specific procedures 's2' and 's3' as their interfaces are not distinguishable
383  interface g4
384    procedure s2
385    procedure s3
386  end interface
387  !ERROR: Generic 'g5' may not have specific procedures 's2' and 's5' as their interfaces are not distinguishable
388  interface g5
389    procedure s2
390    procedure s5
391  end interface
392  !ERROR: Generic 'g6' may not have specific procedures 's2' and 's6' as their interfaces are not distinguishable
393  interface g6
394    procedure s2
395    procedure s6
396  end interface
397contains
398  subroutine s1(x)
399    type(t) :: x
400  end
401  subroutine s2(x)
402    type(t2a) :: x
403  end
404  subroutine s3(x)
405    class(t) :: x
406  end
407  subroutine s4(x)
408    class(t2) :: x
409  end
410  subroutine s5(x)
411    class(*) :: x
412  end
413  subroutine s6(x)
414    type(*) :: x
415  end
416end
417
418! Test C1514 rule 3 -- distinguishable passed-object dummy arguments
419module m18
420  type :: t(k)
421    integer, kind :: k
422  contains
423    procedure, pass(x) :: p1 => s
424    procedure, pass    :: p2 => s
425    procedure          :: p3 => s
426    procedure, pass(y) :: p4 => s
427    generic :: g1 => p1, p4
428    generic :: g2 => p2, p4
429    generic :: g3 => p3, p4
430  end type
431contains
432  subroutine s(x, y)
433    class(t(1)) :: x
434    class(t(2)) :: y
435  end
436end
437
438! C1511 - rules for operators
439module m19
440  interface operator(.foo.)
441    module procedure f1
442    module procedure f2
443  end interface
444  !ERROR: Generic operator '.bar.' may not have specific procedures 'f2' and 'f3' as their interfaces are not distinguishable
445  interface operator(.bar.)
446    module procedure f2
447    module procedure f3
448  end interface
449contains
450  integer function f1(i)
451    integer :: i
452  end
453  integer function f2(i, j)
454    integer :: i, j
455  end
456  integer function f3(i, j)
457    integer :: i, j
458  end
459end
460