blob: 43da6983d717f7aeee770f6ecf15853d2c7da95d [file] [log] [blame]
Richard Smithc70f1d62017-12-14 15:16:18 +00001// Force x86-64 because some of our heuristics are actually based
2// on integer sizes.
3
Eric Fiselier0683c0e2018-05-07 21:07:10 +00004// RUN: %clang_cc1 -triple x86_64-apple-darwin -fcxx-exceptions -fsyntax-only -pedantic -verify -Wsign-compare -std=c++2a %s
5
6#include "Inputs/std-compare.h"
7
8#define ASSERT_TYPE(...) static_assert(__is_same(__VA_ARGS__))
9#define ASSERT_EXPR_TYPE(Expr, Expect) static_assert(__is_same(decltype(Expr), Expect));
Richard Smithc70f1d62017-12-14 15:16:18 +000010
Richard Smith32f39732018-01-07 22:03:44 +000011void self_compare() {
12 int a;
Eric Fiselier0683c0e2018-05-07 21:07:10 +000013 int *b = nullptr;
14
Richard Smith32f39732018-01-07 22:03:44 +000015 (void)(a <=> a); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
16 (void)(b <=> b); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
Richard Smith32f39732018-01-07 22:03:44 +000017}
18
Richard Smithc70f1d62017-12-14 15:16:18 +000019void test0(long a, unsigned long b) {
Eric Fiselier0683c0e2018-05-07 21:07:10 +000020 enum EnumA : int {A};
Richard Smithc70f1d62017-12-14 15:16:18 +000021 enum EnumB {B};
22 enum EnumC {C = 0x10000};
Richard Smithc70f1d62017-12-14 15:16:18 +000023
Eric Fiselier0683c0e2018-05-07 21:07:10 +000024 (void)((short)a <=> (unsigned short)b);
25
26 // (a,b)
27 (void)(a <=> (unsigned long)b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
Richard Smithc70f1d62017-12-14 15:16:18 +000028 (void)(a <=> (unsigned int) b);
29 (void)(a <=> (unsigned short) b);
30 (void)(a <=> (unsigned char) b);
Eric Fiselier0683c0e2018-05-07 21:07:10 +000031 (void)((long)a <=> b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
32 (void)((int)a <=> b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
33 (void)((short)a <=> b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
34 (void)((signed char)a <=> b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
35 (void)((long)a <=> (unsigned long)b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
36 (void)((int)a <=> (unsigned int)b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
Richard Smithc70f1d62017-12-14 15:16:18 +000037 (void)((short) a <=> (unsigned short) b);
38 (void)((signed char) a <=> (unsigned char) b);
39
Eric Fiselier0683c0e2018-05-07 21:07:10 +000040 (void)(A < 42);
Richard Smithc70f1d62017-12-14 15:16:18 +000041 // (A,b)
42 (void)(A <=> (unsigned long) b);
43 (void)(A <=> (unsigned int) b);
44 (void)(A <=> (unsigned short) b);
45 (void)(A <=> (unsigned char) b);
46 (void)((long) A <=> b);
47 (void)((int) A <=> b);
48 (void)((short) A <=> b);
49 (void)((signed char) A <=> b);
50 (void)((long) A <=> (unsigned long) b);
51 (void)((int) A <=> (unsigned int) b);
52 (void)((short) A <=> (unsigned short) b);
53 (void)((signed char) A <=> (unsigned char) b);
54
55 // (a,B)
Eric Fiselier0683c0e2018-05-07 21:07:10 +000056 (void)(a <=> (unsigned long) B); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'long' to 'unsigned long'}}
Richard Smithc70f1d62017-12-14 15:16:18 +000057 (void)(a <=> (unsigned int) B);
58 (void)(a <=> (unsigned short) B);
59 (void)(a <=> (unsigned char) B);
60 (void)((long) a <=> B);
61 (void)((int) a <=> B);
62 (void)((short) a <=> B);
63 (void)((signed char) a <=> B);
Eric Fiselier0683c0e2018-05-07 21:07:10 +000064 (void)((long) a <=> (unsigned long) B); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'long' to 'unsigned long'}}
65 (void)((int) a <=> (unsigned int) B); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'int' to 'unsigned int'}}
Richard Smithc70f1d62017-12-14 15:16:18 +000066 (void)((short) a <=> (unsigned short) B);
67 (void)((signed char) a <=> (unsigned char) B);
68
69 // (C,b)
70 (void)(C <=> (unsigned long) b);
71 (void)(C <=> (unsigned int) b);
72 (void)(C <=> (unsigned short) b); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always 'std::strong_ordering::greater'}}
73 (void)(C <=> (unsigned char) b); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always 'std::strong_ordering::greater'}}
74 (void)((long) C <=> b);
75 (void)((int) C <=> b);
76 (void)((short) C <=> b);
77 (void)((signed char) C <=> b);
78 (void)((long) C <=> (unsigned long) b);
79 (void)((int) C <=> (unsigned int) b);
80 (void)((short) C <=> (unsigned short) b);
81 (void)((signed char) C <=> (unsigned char) b);
82
83 // (a,C)
Eric Fiselier0683c0e2018-05-07 21:07:10 +000084 (void)(a <=> (unsigned long) C); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'long' to 'unsigned long'}}
Richard Smithc70f1d62017-12-14 15:16:18 +000085 (void)(a <=> (unsigned int) C);
86 (void)(a <=> (unsigned short) C);
87 (void)(a <=> (unsigned char) C);
88 (void)((long) a <=> C);
89 (void)((int) a <=> C);
90 (void)((short) a <=> C); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always 'std::strong_ordering::less'}}
91 (void)((signed char) a <=> C); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always 'std::strong_ordering::less'}}
Eric Fiselier0683c0e2018-05-07 21:07:10 +000092 (void)((long) a <=> (unsigned long) C); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'long' to 'unsigned long'}}
93 (void)((int) a <=> (unsigned int) C); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'int' to 'unsigned int'}}
Richard Smithc70f1d62017-12-14 15:16:18 +000094 (void)((short) a <=> (unsigned short) C);
95 (void)((signed char) a <=> (unsigned char) C);
Richard Smithc70f1d62017-12-14 15:16:18 +000096
97 // (0x80000,b)
98 (void)(0x80000 <=> (unsigned long) b);
99 (void)(0x80000 <=> (unsigned int) b);
100 (void)(0x80000 <=> (unsigned short) b); // expected-warning {{result of comparison of constant 524288 with expression of type 'unsigned short' is always 'std::strong_ordering::greater'}}
101 (void)(0x80000 <=> (unsigned char) b); // expected-warning {{result of comparison of constant 524288 with expression of type 'unsigned char' is always 'std::strong_ordering::greater'}}
102 (void)((long) 0x80000 <=> b);
103 (void)((int) 0x80000 <=> b);
104 (void)((short) 0x80000 <=> b);
105 (void)((signed char) 0x80000 <=> b);
106 (void)((long) 0x80000 <=> (unsigned long) b);
107 (void)((int) 0x80000 <=> (unsigned int) b);
108 (void)((short) 0x80000 <=> (unsigned short) b);
109 (void)((signed char) 0x80000 <=> (unsigned char) b);
110
111 // (a,0x80000)
Eric Fiselier0683c0e2018-05-07 21:07:10 +0000112 (void)(a <=> (unsigned long)0x80000); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
Richard Smithc70f1d62017-12-14 15:16:18 +0000113 (void)(a <=> (unsigned int) 0x80000);
114 (void)(a <=> (unsigned short) 0x80000);
115 (void)(a <=> (unsigned char) 0x80000);
116 (void)((long) a <=> 0x80000);
117 (void)((int) a <=> 0x80000);
118 (void)((short) a <=> 0x80000); // expected-warning {{comparison of constant 524288 with expression of type 'short' is always 'std::strong_ordering::less'}}
119 (void)((signed char) a <=> 0x80000); // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always 'std::strong_ordering::less'}}
Eric Fiselier0683c0e2018-05-07 21:07:10 +0000120 (void)((long)a <=> (unsigned long)0x80000); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
121 (void)((int)a <=> (unsigned int)0x80000); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
Richard Smithc70f1d62017-12-14 15:16:18 +0000122 (void)((short) a <=> (unsigned short) 0x80000);
123 (void)((signed char) a <=> (unsigned char) 0x80000);
124}
125
Eric Fiselier0683c0e2018-05-07 21:07:10 +0000126void test5(bool b, bool b2) {
127 enum EnumA { A };
128 (void)(b <=> b2); // OK
129 (void)(true <=> b); // OK
130 (void)(b <=> -10); // expected-error {{invalid operands to binary expression ('bool' and 'int')}}
131 (void)(b <=> char(1)); // expected-error {{invalid operands to binary expression ('bool' and 'char')}}
132 (void)(b <=> A); // expected-error {{invalid operands to binary expression ('bool' and 'EnumA')}}
133
134 // FIXME: Should this be accepted when narrowing doesn't occur?
135 (void)(b <=> 0); // expected-error {{invalid operands to binary expression ('bool' and 'int')}}
136 (void)(b <=> 1); // expected-error {{invalid operands to binary expression ('bool' and 'int')}}
Richard Smithc70f1d62017-12-14 15:16:18 +0000137}
138
139void test6(signed char sc) {
140 (void)(sc <=> 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always 'std::strong_ordering::less'}}
141 (void)(200 <=> sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always 'std::strong_ordering::greater'}}
142}
143
144// Test many signedness combinations.
145void test7(unsigned long other) {
146 // Common unsigned, other unsigned, constant unsigned
147 (void)((unsigned)other <=> (unsigned long)(0x1'ffff'ffff)); // expected-warning{{less}}
148 (void)((unsigned)other <=> (unsigned long)(0xffff'ffff));
149 (void)((unsigned long)other <=> (unsigned)(0x1'ffff'ffff));
150 (void)((unsigned long)other <=> (unsigned)(0xffff'ffff));
151
152 // Common unsigned, other signed, constant unsigned
Eric Fiselier0683c0e2018-05-07 21:07:10 +0000153 (void)((int)other <=> (unsigned long)(0xffff'ffff'ffff'ffff)); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
154 (void)((int)other <=> (unsigned long)(0x0000'0000'ffff'ffff)); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
155 (void)((int)other <=> (unsigned long)(0x0000'0000'0fff'ffff)); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
156 (void)((int)other <=> (unsigned)(0x8000'0000)); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
Richard Smithc70f1d62017-12-14 15:16:18 +0000157
158 // Common unsigned, other unsigned, constant signed
Eric Fiselier0683c0e2018-05-07 21:07:10 +0000159 (void)((unsigned long)other <=> (int)(0xffff'ffff)); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
Richard Smithc70f1d62017-12-14 15:16:18 +0000160
161 // Common unsigned, other signed, constant signed
162 // Should not be possible as the common type should also be signed.
163
164 // Common signed, other signed, constant signed
165 (void)((int)other <=> (long)(0xffff'ffff)); // expected-warning{{less}}
166 (void)((int)other <=> (long)(0xffff'ffff'0000'0000)); // expected-warning{{greater}}
167 (void)((int)other <=> (long)(0x0fff'ffff));
168 (void)((int)other <=> (long)(0xffff'ffff'f000'0000));
169
170 // Common signed, other signed, constant unsigned
171 (void)((int)other <=> (unsigned char)(0xffff));
172 (void)((int)other <=> (unsigned char)(0xff));
173
174 // Common signed, other unsigned, constant signed
175 (void)((unsigned char)other <=> (int)(0xff));
176 (void)((unsigned char)other <=> (int)(0xffff)); // expected-warning{{less}}
177
178 // Common signed, other unsigned, constant unsigned
179 (void)((unsigned char)other <=> (unsigned short)(0xff));
180 (void)((unsigned char)other <=> (unsigned short)(0x100)); // expected-warning{{less}}
181 (void)((unsigned short)other <=> (unsigned char)(0xff));
182}
Eric Fiselier0683c0e2018-05-07 21:07:10 +0000183
184void test8(void *vp, const void *cvp, int *ip) {
185 (void)(vp <=> cvp); // OK, void* comparisons are allowed.
186 (void)(vp <=> ip);
187 (void)(ip <=> cvp);
188}
189
190void test9(long double ld, double d, float f, int i, long long ll) {
191 (void)(f <=> ll); // OK, floating-point to integer is OK
192 (void)(d <=> ld);
193 (void)(i <=> f);
194}
195
196typedef int *INTPTR;
197void test_typedef_bug(int *x, INTPTR y) {
198 (void)(x <=> y);
199}
200
201using nullptr_t = decltype(nullptr);
202
203struct Class {};
204struct ClassB : Class {};
205struct Class2 {};
206using FnTy = void(int);
207using FnTy2 = long(int);
208using MemFnTy = void (Class::*)() const;
209using MemFnTyB = void (ClassB::*)() const;
210using MemFnTy2 = void (Class::*)();
211using MemFnTy3 = void (Class2::*)() const;
212using MemDataTy = long(Class::*);
213
214void test_nullptr(int *x, FnTy *fp, MemFnTy memp, MemDataTy memdp) {
215 auto r1 = (nullptr <=> nullptr);
216 ASSERT_EXPR_TYPE(r1, std::strong_equality);
217
218 auto r2 = (nullptr <=> x);
219 ASSERT_EXPR_TYPE(r2, std::strong_equality);
220
221 auto r3 = (fp <=> nullptr);
222 ASSERT_EXPR_TYPE(r3, std::strong_equality);
223
224 auto r4 = (0 <=> fp);
225 ASSERT_EXPR_TYPE(r4, std::strong_equality);
226
227 auto r5 = (nullptr <=> memp);
228 ASSERT_EXPR_TYPE(r5, std::strong_equality);
229
230 auto r6 = (0 <=> memdp);
231 ASSERT_EXPR_TYPE(r6, std::strong_equality);
232
233 auto r7 = (0 <=> nullptr);
234 ASSERT_EXPR_TYPE(r7, std::strong_equality);
235}
236
237void test_compatible_pointer(FnTy *f1, FnTy2 *f2, MemFnTy mf1, MemFnTyB mfb,
238 MemFnTy2 mf2, MemFnTy3 mf3) {
239 (void)(f1 <=> f2); // expected-error {{distinct pointer types}}
240
241 auto r1 = (mf1 <=> mfb); // OK
242 ASSERT_EXPR_TYPE(r1, std::strong_equality);
243 ASSERT_EXPR_TYPE((mf1 <=> mfb), std::strong_equality);
244
245 (void)(mf1 <=> mf2); // expected-error {{distinct pointer types}}
246 (void)(mf3 <=> mf1); // expected-error {{distinct pointer types}}
247}
248
249// Test that variable narrowing is deferred for value dependent expressions
250template <int Val>
251auto test_template_overflow() {
252 // expected-error@+1 {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
253 return (Val <=> (unsigned long)0);
254}
255template auto test_template_overflow<0>();
256template auto test_template_overflow<-1>(); // expected-note {{requested here}}
257
258void test_enum_integral_compare() {
259 enum EnumA : int {A, ANeg = -1, AMax = __INT_MAX__};
260 enum EnumB : unsigned {B, BMax = __UINT32_MAX__ };
261 enum EnumC : int {C = -1, C0 = 0};
262
263 (void)(A <=> C); // expected-error {{invalid operands to binary expression ('EnumA' and 'EnumC')}}
264
265 (void)(A <=> (unsigned)0);
266 (void)((unsigned)0 <=> A);
267 (void)(ANeg <=> (unsigned)0); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}
268 (void)((unsigned)0 <=> ANeg); // expected-error {{cannot be narrowed}}
269
270 (void)(B <=> 42);
271 (void)(42 <=> B);
272 (void)(B <=> (unsigned long long)42);
273 (void)(B <=> -1); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}
274 (void)(BMax <=> (unsigned long)-1);
275
276 (void)(C0 <=> (unsigned)42);
277 (void)(C <=> (unsigned)42); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}
278}
279
280namespace EnumCompareTests {
281
282enum class EnumA { A, A2 };
283enum class EnumB { B };
284enum class EnumC : unsigned { C };
285
286void test_enum_enum_compare_no_builtin() {
287 auto r1 = (EnumA::A <=> EnumA::A2); // OK
288 ASSERT_EXPR_TYPE(r1, std::strong_ordering);
289 (void)(EnumA::A <=> EnumA::A); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
290 (void)(EnumA::A <=> EnumB::B); // expected-error {{invalid operands to binary expression ('EnumCompareTests::EnumA' and 'EnumCompareTests::EnumB')}}
291 (void)(EnumB::B <=> EnumA::A); // expected-error {{invalid operands}}
292}
293
294template <int>
295struct Tag {};
296// expected-note@+1 {{candidate}}
297Tag<0> operator<=>(EnumA, EnumA) {
298 return {};
299}
300Tag<1> operator<=>(EnumA, EnumB) {
301 return {};
302}
303
304void test_enum_ovl_provided() {
305 auto r1 = (EnumA::A <=> EnumA::A);
306 ASSERT_EXPR_TYPE(r1, Tag<0>);
307 auto r2 = (EnumA::A <=> EnumB::B);
308 ASSERT_EXPR_TYPE(r2, Tag<1>);
309 (void)(EnumB::B <=> EnumA::A); // expected-error {{invalid operands to binary expression ('EnumCompareTests::EnumB' and 'EnumCompareTests::EnumA')}}
310}
311
312void enum_float_test() {
313 enum EnumA { A };
314 (void)(A <=> (float)0); // expected-error {{invalid operands to binary expression ('EnumA' and 'float')}}
315 (void)((double)0 <=> A); // expected-error {{invalid operands to binary expression ('double' and 'EnumA')}}
316 (void)((long double)0 <=> A); // expected-error {{invalid operands to binary expression ('long double' and 'EnumA')}}
317}
318
319enum class Bool1 : bool { Zero,
320 One };
321enum Bool2 : bool { B2_Zero,
322 B2_One };
323
324void test_bool_enum(Bool1 A1, Bool1 A2, Bool2 B1, Bool2 B2) {
325 (void)(A1 <=> A2);
326 (void)(B1 <=> B2);
327}
328
329} // namespace EnumCompareTests
330
331namespace TestUserDefinedConvSeq {
332
333template <class T, T Val>
334struct Conv {
335 constexpr operator T() const { return Val; }
336 operator T() { return Val; }
337};
338
339void test_user_conv() {
340 {
341 using C = Conv<int, 0>;
342 C c;
343 const C cc;
344 (void)(0 <=> c);
345 (void)(c <=> -1);
346 (void)((unsigned)0 <=> cc);
347 (void)((unsigned)0 <=> c); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'int' to 'unsigned int'}}
348 }
349 {
350 using C = Conv<int, -1>;
351 C c;
352 const C cc;
353 (void)(c <=> 0);
354 (void)(cc <=> (unsigned)0); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}
355 (void)(c <=> (unsigned)0); // expected-error {{cannot be narrowed from type 'int' to 'unsigned int'}}
356 }
357}
358
359} // namespace TestUserDefinedConvSeq
360
361void test_array_conv() {
362 int arr[5];
363 int *ap = arr + 2;
364 int arr2[3];
365 (void)(arr <=> arr); // expected-error {{invalid operands to binary expression ('int [5]' and 'int [5]')}}
366 (void)(+arr <=> arr);
367}
368
369void test_mixed_float_int(float f, double d, long double ld) {
370 extern int i;
371 extern unsigned u;
372 extern long l;
373 extern short s;
374 extern unsigned short us;
375 auto r1 = (f <=> i);
376 ASSERT_EXPR_TYPE(r1, std::partial_ordering);
377
378 auto r2 = (us <=> ld);
379 ASSERT_EXPR_TYPE(r2, std::partial_ordering);
380
381 auto r3 = (s <=> f);
382 ASSERT_EXPR_TYPE(r3, std::partial_ordering);
383
384 auto r4 = (0.0 <=> i);
385 ASSERT_EXPR_TYPE(r4, std::partial_ordering);
386}
387
388namespace NullptrTest {
389using nullptr_t = decltype(nullptr);
390void foo(nullptr_t x, nullptr_t y) {
391 auto r = x <=> y;
392 ASSERT_EXPR_TYPE(r, std::strong_equality);
393}
394} // namespace NullptrTest
395
396namespace ComplexTest {
397
398enum class StrongE {};
399enum WeakE { E_One,
400 E_Two };
401
402void test_diag(_Complex int ci, _Complex float cf, _Complex double cd, int i, float f, StrongE E1, WeakE E2, int *p) {
403 (void)(ci <=> (_Complex int &)ci);
404 (void)(ci <=> cf);
405 (void)(ci <=> i);
406 (void)(ci <=> f);
407 (void)(cf <=> i);
408 (void)(cf <=> f);
409 (void)(ci <=> p); // expected-error {{invalid operands}}
410 (void)(ci <=> E1); // expected-error {{invalid operands}}
411 (void)(E2 <=> cf); // expected-error {{invalid operands}}
412}
413
414void test_int(_Complex int x, _Complex int y) {
415 auto r = x <=> y;
416 ASSERT_EXPR_TYPE(r, std::strong_equality);
417}
418
419void test_double(_Complex double x, _Complex double y) {
420 auto r = x <=> y;
421 ASSERT_EXPR_TYPE(r, std::weak_equality);
422}
423
424} // namespace ComplexTest