1! RUN: %B/test/Semantics/test_errors.sh %s %flang %t
2! Invalid operand types when user-defined operator is available
3module m1
4  type :: t
5  end type
6  interface operator(==)
7    logical function eq_tt(x, y)
8      import :: t
9      type(t), intent(in) :: x, y
10    end
11  end interface
12  interface operator(+)
13    logical function add_tr(x, y)
14      import :: t
15      type(t), intent(in) :: x
16      real, intent(in) :: y
17    end
18    logical function plus_t(x)
19      import :: t
20      type(t), intent(in) :: x
21    end
22    logical function add_12(x, y)
23      real, intent(in) :: x(:), y(:,:)
24    end
25  end interface
26  interface operator(.and.)
27    logical function and_tr(x, y)
28      import :: t
29      type(t), intent(in) :: x
30      real, intent(in) :: y
31    end
32  end interface
33  interface operator(//)
34    logical function concat_tt(x, y)
35      import :: t
36      type(t), intent(in) :: x, y
37    end
38  end interface
39  interface operator(.not.)
40    logical function not_r(x)
41      real, intent(in) :: x
42    end
43  end interface
44  type(t) :: x, y
45  real :: r
46  logical :: l
47contains
48  subroutine test_relational()
49    l = x == y  !OK
50    l = x .eq. y  !OK
51    !ERROR: No intrinsic or user-defined OPERATOR(==) matches operand types TYPE(t) and REAL(4)
52    l = x == r
53  end
54  subroutine test_numeric()
55    l = x + r  !OK
56    !ERROR: No intrinsic or user-defined OPERATOR(+) matches operand types REAL(4) and TYPE(t)
57    l = r + x
58  end
59  subroutine test_logical()
60    l = x .and. r  !OK
61    !ERROR: No intrinsic or user-defined OPERATOR(.AND.) matches operand types REAL(4) and TYPE(t)
62    l = r .and. x
63  end
64  subroutine test_unary()
65    l = +x  !OK
66    !ERROR: No intrinsic or user-defined OPERATOR(+) matches operand type LOGICAL(4)
67    l = +l
68    l = .not. r  !OK
69    !ERROR: No intrinsic or user-defined OPERATOR(.NOT.) matches operand type TYPE(t)
70    l = .not. x
71  end
72  subroutine test_concat()
73    l = x // y  !OK
74    !ERROR: No intrinsic or user-defined OPERATOR(//) matches operand types TYPE(t) and REAL(4)
75    l = x // r
76  end
77  subroutine test_conformability(x, y)
78    real :: x(10), y(10,10)
79    l = x + y  !OK
80    !ERROR: No intrinsic or user-defined OPERATOR(+) matches rank 2 array of REAL(4) and rank 1 array of REAL(4)
81    l = y + x
82  end
83end
84
85! Invalid operand types when user-defined operator is not available
86module m2
87  type :: t
88  end type
89  type(t) :: x, y
90  real :: r
91  logical :: l
92contains
93  subroutine test_relational()
94    !ERROR: Operands of .EQ. must have comparable types; have TYPE(t) and REAL(4)
95    l = x == r
96  end
97  subroutine test_numeric()
98    !ERROR: Operands of + must be numeric; have REAL(4) and TYPE(t)
99    l = r + x
100  end
101  subroutine test_logical()
102    !ERROR: Operands of .AND. must be LOGICAL; have REAL(4) and TYPE(t)
103    l = r .and. x
104  end
105  subroutine test_unary()
106    !ERROR: Operand of unary + must be numeric; have LOGICAL(4)
107    l = +l
108    !ERROR: Operand of .NOT. must be LOGICAL; have TYPE(t)
109    l = .not. x
110  end
111  subroutine test_concat(a, b)
112    character(4,kind=1) :: a
113    character(4,kind=2) :: b
114    character(4) :: c
115    !ERROR: Operands of // must be CHARACTER with the same kind; have CHARACTER(KIND=1) and CHARACTER(KIND=2)
116    c = a // b
117    !ERROR: Operands of // must be CHARACTER with the same kind; have TYPE(t) and REAL(4)
118    l = x // r
119  end
120  subroutine test_conformability(x, y)
121    real :: x(10), y(10,10)
122    !ERROR: Operands of + are not conformable; have rank 2 and rank 1
123    l = y + x
124  end
125end
126
127! Invalid untyped operands: user-defined operator doesn't affect errors
128module m3
129  interface operator(+)
130    logical function add(x, y)
131      logical, intent(in) :: x
132      integer, value :: y
133    end
134  end interface
135contains
136  subroutine s1(x, y)
137    logical :: x
138    integer :: y
139    logical :: l
140    complex :: z
141    y = y + z'1'  !OK
142    !ERROR: Operands of + must be numeric; have untyped and COMPLEX(4)
143    z = z'1' + z
144    y = +z'1'  !OK
145    !ERROR: Operand of unary - must be numeric; have untyped
146    y = -z'1'
147    !ERROR: Operands of + must be numeric; have LOGICAL(4) and untyped
148    y = x + z'1'
149    !ERROR: Operands of .NE. must have comparable types; have LOGICAL(4) and untyped
150    l = x /= null()
151  end
152end
153
154! Test alternate operators. They aren't enabled by default so should be
155! treated as defined operators, not intrinsic ones.
156module m4
157contains
158  subroutine s1(x, y, z)
159    logical :: x
160    real :: y, z
161    !ERROR: No operator .A. defined for REAL(4) and REAL(4)
162    x = y .a. z
163    !ERROR: No operator .O. defined for REAL(4) and REAL(4)
164    x = y .o. z
165    !ERROR: No operator .N. defined for REAL(4)
166    x = .n. y
167    !ERROR: No operator .XOR. defined for REAL(4) and REAL(4)
168    x = y .xor. z
169    !ERROR: No operator .X. defined for REAL(4)
170    x = .x. y
171  end
172end
173
174! Like m4 in resolve63 but compiled with different options.
175! .A. is a defined operator.
176module m5
177  interface operator(.A.)
178    logical function f1(x, y)
179      integer, intent(in) :: x, y
180    end
181  end interface
182  interface operator(.and.)
183    logical function f2(x, y)
184      real, intent(in) :: x, y
185    end
186  end interface
187contains
188  subroutine s1(x, y, z)
189    logical :: x
190    complex :: y, z
191    !ERROR: No intrinsic or user-defined OPERATOR(.AND.) matches operand types COMPLEX(4) and COMPLEX(4)
192    x = y .and. z
193    !ERROR: No intrinsic or user-defined .A. matches operand types COMPLEX(4) and COMPLEX(4)
194    x = y .a. z
195  end
196end
197
198! Type-bound operators
199module m6
200  type :: t1
201  contains
202    procedure, pass(x) :: p1 => f1
203    generic :: operator(+) => p1
204  end type
205  type, extends(t1) :: t2
206  contains
207    procedure, pass(y) :: p2 => f2
208    generic :: operator(+) => p2
209  end type
210  type :: t3
211  contains
212    procedure, nopass :: p1 => f1
213    !ERROR: OPERATOR(+) procedure 'p1' may not have NOPASS attribute
214    generic :: operator(+) => p1
215  end type
216contains
217  integer function f1(x, y)
218    class(t1), intent(in) :: x
219    integer, intent(in) :: y
220  end
221  integer function f2(x, y)
222    class(t1), intent(in) :: x
223    class(t2), intent(in) :: y
224  end
225  subroutine test(x, y, z)
226    class(t1) :: x
227    class(t2) :: y
228    integer :: i
229    i = x + y
230    i = x + i
231    i = y + i
232    !ERROR: No intrinsic or user-defined OPERATOR(+) matches operand types TYPE(t2) and TYPE(t1)
233    i = y + x
234    !ERROR: No intrinsic or user-defined OPERATOR(+) matches operand types INTEGER(4) and TYPE(t1)
235    i = i + x
236  end
237end
238