• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Force x86-64 because some of our heuristics are actually based
2 // on integer sizes.
3 
4 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fcxx-exceptions -fsyntax-only -pedantic -verify -Wsign-compare -std=c++2a %s
5 
6 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fcxx-exceptions -std=c++2a -x c++ %S/Inputs/std-compare.h -emit-pch -o %t.pch
7 // RUN: %clang_cc1 -triple x86_64-apple-darwin -fcxx-exceptions -fsyntax-only -pedantic -verify -Wsign-compare -std=c++2a %s -include-pch %t.pch
8 
9 #include "Inputs/std-compare.h"
10 
11 #define ASSERT_TYPE(...) static_assert(__is_same(__VA_ARGS__))
12 #define ASSERT_EXPR_TYPE(Expr, Expect) static_assert(__is_same(decltype(Expr), Expect));
13 
14 struct S {
15   static int x[5];
16 };
17 
self_compare()18 void self_compare() {
19   int a;
20   int *b = nullptr;
21   S s;
22 
23   (void)(a <=> a); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
24   (void)(b <=> b); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
25   (void)(s.x[a] <=> S::x[a]); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
26 }
27 
test0(long a,unsigned long b)28 void test0(long a, unsigned long b) {
29   enum EnumA : int {A};
30   enum EnumB {B};
31   enum EnumC {C = 0x10000};
32 
33   (void)((short)a <=> (unsigned short)b);
34 
35   // (a,b)
36   (void)(a <=> (unsigned long)b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
37   (void)(a <=> (unsigned int) b);
38   (void)(a <=> (unsigned short) b);
39   (void)(a <=> (unsigned char) b);
40   (void)((long)a <=> b);                // expected-error {{argument to 'operator<=>' cannot be narrowed}}
41   (void)((int)a <=> b);                 // expected-error {{argument to 'operator<=>' cannot be narrowed}}
42   (void)((short)a <=> b);               // expected-error {{argument to 'operator<=>' cannot be narrowed}}
43   (void)((signed char)a <=> b);         // expected-error {{argument to 'operator<=>' cannot be narrowed}}
44   (void)((long)a <=> (unsigned long)b); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
45   (void)((int)a <=> (unsigned int)b);   // expected-error {{argument to 'operator<=>' cannot be narrowed}}
46   (void)((short) a <=> (unsigned short) b);
47   (void)((signed char) a <=> (unsigned char) b);
48 
49   // (A,b)
50   (void)(A <=> (unsigned long) b);
51   (void)(A <=> (unsigned int) b);
52   (void)(A <=> (unsigned short) b);
53   (void)(A <=> (unsigned char) b);
54   (void)((long) A <=> b);
55   (void)((int) A <=> b);
56   (void)((short) A <=> b);
57   (void)((signed char) A <=> b);
58   (void)((long) A <=> (unsigned long) b);
59   (void)((int) A <=> (unsigned int) b);
60   (void)((short) A <=> (unsigned short) b);
61   (void)((signed char) A <=> (unsigned char) b);
62 
63   // (a,B)
64   (void)(a <=> (unsigned long) B); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'long' to 'unsigned long'}}
65   (void)(a <=> (unsigned int) B);
66   (void)(a <=> (unsigned short) B);
67   (void)(a <=> (unsigned char) B);
68   (void)((long) a <=> B);
69   (void)((int) a <=> B);
70   (void)((short) a <=> B);
71   (void)((signed char) a <=> B);
72   (void)((long) a <=> (unsigned long) B); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'long' to 'unsigned long'}}
73   (void)((int) a <=> (unsigned int) B); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'int' to 'unsigned int'}}
74   (void)((short) a <=> (unsigned short) B);
75   (void)((signed char) a <=> (unsigned char) B);
76 
77   // (C,b)
78   (void)(C <=> (unsigned long) b);
79   (void)(C <=> (unsigned int) b);
80   (void)(C <=> (unsigned short) b); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned short' is always 'std::strong_ordering::greater'}}
81   (void)(C <=> (unsigned char) b);  // expected-warning {{comparison of constant 'C' (65536) with expression of type 'unsigned char' is always 'std::strong_ordering::greater'}}
82   (void)((long) C <=> b);
83   (void)((int) C <=> b);
84   (void)((short) C <=> b);
85   (void)((signed char) C <=> b);
86   (void)((long) C <=> (unsigned long) b);
87   (void)((int) C <=> (unsigned int) b);
88   (void)((short) C <=> (unsigned short) b);
89   (void)((signed char) C <=> (unsigned char) b);
90 
91   // (a,C)
92   (void)(a <=> (unsigned long) C); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'long' to 'unsigned long'}}
93   (void)(a <=> (unsigned int) C);
94   (void)(a <=> (unsigned short) C);
95   (void)(a <=> (unsigned char) C);
96   (void)((long) a <=> C);
97   (void)((int) a <=> C);
98   (void)((short) a <=> C); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'short' is always 'std::strong_ordering::less'}}
99   (void)((signed char) a <=> C); // expected-warning {{comparison of constant 'C' (65536) with expression of type 'signed char' is always 'std::strong_ordering::less'}}
100   (void)((long) a <=> (unsigned long) C); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'long' to 'unsigned long'}}
101   (void)((int) a <=> (unsigned int) C); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'int' to 'unsigned int'}}
102   (void)((short) a <=> (unsigned short) C);
103   (void)((signed char) a <=> (unsigned char) C);
104 
105   // (0x80000,b)
106   (void)(0x80000 <=> (unsigned long) b);
107   (void)(0x80000 <=> (unsigned int) b);
108   (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'}}
109   (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'}}
110   (void)((long) 0x80000 <=> b);
111   (void)((int) 0x80000 <=> b);
112   (void)((short) 0x80000 <=> b);
113   (void)((signed char) 0x80000 <=> b);
114   (void)((long) 0x80000 <=> (unsigned long) b);
115   (void)((int) 0x80000 <=> (unsigned int) b);
116   (void)((short) 0x80000 <=> (unsigned short) b);
117   (void)((signed char) 0x80000 <=> (unsigned char) b);
118 
119   // (a,0x80000)
120   (void)(a <=> (unsigned long)0x80000); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
121   (void)(a <=> (unsigned int) 0x80000);
122   (void)(a <=> (unsigned short) 0x80000);
123   (void)(a <=> (unsigned char) 0x80000);
124   (void)((long) a <=> 0x80000);
125   (void)((int) a <=> 0x80000);
126   (void)((short) a <=> 0x80000); // expected-warning {{comparison of constant 524288 with expression of type 'short' is always 'std::strong_ordering::less'}}
127   (void)((signed char) a <=> 0x80000); // expected-warning {{comparison of constant 524288 with expression of type 'signed char' is always 'std::strong_ordering::less'}}
128   (void)((long)a <=> (unsigned long)0x80000); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
129   (void)((int)a <=> (unsigned int)0x80000);   // expected-error {{argument to 'operator<=>' cannot be narrowed}}
130   (void)((short) a <=> (unsigned short) 0x80000);
131   (void)((signed char) a <=> (unsigned char) 0x80000);
132 }
133 
test5(bool b,bool b2)134 void test5(bool b, bool b2) {
135   enum EnumA { A };
136   (void)(b <=> b2);      // OK
137   (void)(true <=> b);    // OK
138   (void)(b <=> -10);     // expected-error {{invalid operands to binary expression ('bool' and 'int')}}
139   (void)(b <=> char(1)); // expected-error {{invalid operands to binary expression ('bool' and 'char')}}
140   (void)(b <=> A);       // expected-error {{invalid operands to binary expression ('bool' and 'EnumA')}}
141 
142   // FIXME: Should this be accepted when narrowing doesn't occur?
143   (void)(b <=> 0); // expected-error {{invalid operands to binary expression ('bool' and 'int')}}
144   (void)(b <=> 1); // expected-error {{invalid operands to binary expression ('bool' and 'int')}}
145 }
146 
test6(signed char sc)147 void test6(signed char sc) {
148   (void)(sc <=> 200); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always 'std::strong_ordering::less'}}
149   (void)(200 <=> sc); // expected-warning{{comparison of constant 200 with expression of type 'signed char' is always 'std::strong_ordering::greater'}}
150 }
151 
152 // Test many signedness combinations.
test7(unsigned long other)153 void test7(unsigned long other) {
154   // Common unsigned, other unsigned, constant unsigned
155   (void)((unsigned)other <=> (unsigned long)(0x1'ffff'ffff)); // expected-warning{{less}}
156   (void)((unsigned)other <=> (unsigned long)(0xffff'ffff));
157   (void)((unsigned long)other <=> (unsigned)(0x1'ffff'ffff));
158   (void)((unsigned long)other <=> (unsigned)(0xffff'ffff));
159 
160   // Common unsigned, other signed, constant unsigned
161   (void)((int)other <=> (unsigned long)(0xffff'ffff'ffff'ffff)); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
162   (void)((int)other <=> (unsigned long)(0x0000'0000'ffff'ffff)); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
163   (void)((int)other <=> (unsigned long)(0x0000'0000'0fff'ffff)); // expected-error {{argument to 'operator<=>' cannot be narrowed}}
164   (void)((int)other <=> (unsigned)(0x8000'0000));                // expected-error {{argument to 'operator<=>' cannot be narrowed}}
165 
166   // Common unsigned, other unsigned, constant signed
167   (void)((unsigned long)other <=> (int)(0xffff'ffff)); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
168 
169   // Common unsigned, other signed, constant signed
170   // Should not be possible as the common type should also be signed.
171 
172   // Common signed, other signed, constant signed
173   (void)((int)other <=> (long)(0xffff'ffff));           // expected-warning{{less}}
174   (void)((int)other <=> (long)(0xffff'ffff'0000'0000)); // expected-warning{{greater}}
175   (void)((int)other <=> (long)(0x0fff'ffff));
176   (void)((int)other <=> (long)(0xffff'ffff'f000'0000));
177 
178   // Common signed, other signed, constant unsigned
179   (void)((int)other <=> (unsigned char)(0xffff));
180   (void)((int)other <=> (unsigned char)(0xff));
181 
182   // Common signed, other unsigned, constant signed
183   (void)((unsigned char)other <=> (int)(0xff));
184   (void)((unsigned char)other <=> (int)(0xffff));  // expected-warning{{less}}
185 
186   // Common signed, other unsigned, constant unsigned
187   (void)((unsigned char)other <=> (unsigned short)(0xff));
188   (void)((unsigned char)other <=> (unsigned short)(0x100)); // expected-warning{{less}}
189   (void)((unsigned short)other <=> (unsigned char)(0xff));
190 }
191 
test8(void * vp,const void * cvp,int * ip)192 void test8(void *vp, const void *cvp, int *ip) {
193   (void)(vp <=> cvp); // OK, void* comparisons are allowed.
194   (void)(vp <=> ip);
195   (void)(ip <=> cvp);
196 }
197 
test9(long double ld,double d,float f,int i,long long ll)198 void test9(long double ld, double d, float f, int i, long long ll) {
199   (void)(f <=> ll); // OK, floating-point to integer is OK
200   (void)(d <=> ld);
201   (void)(i <=> f);
202 }
203 
204 typedef int *INTPTR;
test_typedef_bug(int * x,INTPTR y)205 void test_typedef_bug(int *x, INTPTR y) {
206   (void)(x <=> y);
207 }
208 
209 using nullptr_t = decltype(nullptr);
210 
211 struct Class {};
212 struct ClassB : Class {};
213 struct Class2 {};
214 using FnTy = void(int);
215 using FnTy2 = long(int);
216 using FnTy3 = void(int) noexcept;
217 using MemFnTy = void (Class::*)() const;
218 using MemDataTy = long(Class::*);
219 
test_nullptr(int * x,FnTy * fp,MemFnTy memp,MemDataTy memdp)220 void test_nullptr(int *x, FnTy *fp, MemFnTy memp, MemDataTy memdp) {
221   auto r1 = (nullptr <=> nullptr); // expected-error {{invalid operands}}
222   auto r2 = (nullptr <=> x); // expected-error {{invalid operands}}
223   auto r3 = (fp <=> nullptr); // expected-error {{invalid operands}}
224   auto r4 = (0 <=> fp); // expected-error {{ordered comparison between pointer and zero}}
225   auto r5 = (nullptr <=> memp); // expected-error {{invalid operands}}
226   auto r6 = (0 <=> memdp); // expected-error {{invalid operands}}
227   auto r7 = (0 <=> nullptr); // expected-error {{invalid operands}}
228 }
229 
test_memptr(MemFnTy mf,MemDataTy md)230 void test_memptr(MemFnTy mf, MemDataTy md) {
231   (void)(mf <=> mf); // expected-error {{invalid operands}} expected-warning {{self-comparison}}
232   (void)(md <=> md); // expected-error {{invalid operands}} expected-warning {{self-comparison}}
233 }
234 
test_compatible_pointer(FnTy * f1,FnTy2 * f2,FnTy3 * f3)235 void test_compatible_pointer(FnTy *f1, FnTy2 *f2, FnTy3 *f3) {
236   (void)(f1 <=> f2); // expected-error {{distinct pointer types}}
237   (void)(f1 <=> f3); // expected-error {{invalid operands}}
238 }
239 
240 // Test that variable narrowing is deferred for value dependent expressions
241 template <int Val>
test_template_overflow()242 auto test_template_overflow() {
243   // expected-error@+1 {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}
244   return (Val <=> (unsigned long)0);
245 }
246 template auto test_template_overflow<0>();
247 template auto test_template_overflow<-1>(); // expected-note {{requested here}}
248 
test_enum_integral_compare()249 void test_enum_integral_compare() {
250   enum EnumA : int {A, ANeg = -1, AMax = __INT_MAX__};
251   enum EnumB : unsigned {B, BMax = __UINT32_MAX__ };
252   enum EnumC : int {C = -1, C0 = 0};
253 
254   (void)(A <=> C); // expected-error {{invalid operands to binary expression ('EnumA' and 'EnumC')}}
255 
256   (void)(A <=> (unsigned)0);
257   (void)((unsigned)0 <=> A);
258   (void)(ANeg <=> (unsigned)0); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}
259   (void)((unsigned)0 <=> ANeg); // expected-error {{cannot be narrowed}}
260 
261   (void)(B <=> 42);
262   (void)(42 <=> B);
263   (void)(B <=> (unsigned long long)42);
264   (void)(B <=> -1); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}
265   (void)(BMax <=> (unsigned long)-1);
266 
267   (void)(C0 <=> (unsigned)42);
268   (void)(C <=> (unsigned)42); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}
269 }
270 
271 namespace EnumCompareTests {
272 
273 enum class EnumA { A, A2 };
274 enum class EnumB { B };
275 enum class EnumC : unsigned { C };
276 
test_enum_enum_compare_no_builtin()277 void test_enum_enum_compare_no_builtin() {
278   auto r1 = (EnumA::A <=> EnumA::A2); // OK
279   ASSERT_EXPR_TYPE(r1, std::strong_ordering);
280   (void)(EnumA::A <=> EnumA::A); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}
281   (void)(EnumA::A <=> EnumB::B); // expected-error {{invalid operands to binary expression ('EnumCompareTests::EnumA' and 'EnumCompareTests::EnumB')}}
282   (void)(EnumB::B <=> EnumA::A); // expected-error {{invalid operands}}
283 }
284 
285 template <int>
286 struct Tag {};
operator <=>(EnumA,EnumA)287 Tag<0> operator<=>(EnumA, EnumA) { // expected-note {{not viable}}
288   return {};
289 }
290 // expected-note@+1 {{while rewriting comparison as call to 'operator<=>' declared here}}
operator <=>(EnumA,EnumB)291 Tag<1> operator<=>(EnumA, EnumB) { // expected-note {{not viable}}
292   return {};
293 }
294 
test_enum_ovl_provided()295 void test_enum_ovl_provided() {
296   auto r1 = (EnumA::A <=> EnumA::A);
297   ASSERT_EXPR_TYPE(r1, Tag<0>);
298   auto r2 = (EnumA::A <=> EnumB::B);
299   ASSERT_EXPR_TYPE(r2, Tag<1>);
300   (void)(EnumB::B <=> EnumA::A); // expected-error {{invalid operands to binary expression ('int' and 'Tag<1>')}}
301 }
302 
enum_float_test()303 void enum_float_test() {
304   enum EnumA { A };
305   (void)(A <=> (float)0);       // expected-error {{invalid operands to binary expression ('EnumA' and 'float')}}
306   (void)((double)0 <=> A);      // expected-error {{invalid operands to binary expression ('double' and 'EnumA')}}
307   (void)((long double)0 <=> A); // expected-error {{invalid operands to binary expression ('long double' and 'EnumA')}}
308 }
309 
310 enum class Bool1 : bool { Zero,
311                           One };
312 enum Bool2 : bool { B2_Zero,
313                     B2_One };
314 
test_bool_enum(Bool1 A1,Bool1 A2,Bool2 B1,Bool2 B2)315 void test_bool_enum(Bool1 A1, Bool1 A2, Bool2 B1, Bool2 B2) {
316   (void)(A1 <=> A2);
317   (void)(B1 <=> B2);
318 }
319 
320 } // namespace EnumCompareTests
321 
322 namespace TestUserDefinedConvSeq {
323 
324 template <class T, T Val>
325 struct Conv {
operator TTestUserDefinedConvSeq::Conv326   constexpr operator T() const { return Val; }
operator TTestUserDefinedConvSeq::Conv327   operator T() { return Val; }
328 };
329 
test_user_conv()330 void test_user_conv() {
331   {
332     using C = Conv<int, 0>;
333     C c;
334     const C cc;
335     (void)(0 <=> c);
336     (void)(c <=> -1);
337     (void)((unsigned)0 <=> cc);
338     (void)((unsigned)0 <=> c); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'int' to 'unsigned int'}}
339   }
340   {
341     using C = Conv<int, -1>;
342     C c;
343     const C cc;
344     (void)(c <=> 0);
345     (void)(cc <=> (unsigned)0); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}
346     (void)(c <=> (unsigned)0);  // expected-error {{cannot be narrowed from type 'int' to 'unsigned int'}}
347   }
348 }
349 
350 struct X {
operator <=>TestUserDefinedConvSeq::X351   constexpr const Conv<int, -1> operator<=>(X) { return {}; }
352 };
353 static_assert(X() < X());
354 
355 } // namespace TestUserDefinedConvSeq
356 
test_array_conv()357 void test_array_conv() {
358   int arr[5];
359   int *ap = arr + 2;
360   int arr2[3];
361   (void)(arr <=> arr); // expected-error {{invalid operands to binary expression ('int [5]' and 'int [5]')}}
362   (void)(+arr <=> arr);
363 }
364 
test_mixed_float_int(float f,double d,long double ld)365 void test_mixed_float_int(float f, double d, long double ld) {
366   extern int i;
367   extern unsigned u;
368   extern long l;
369   extern short s;
370   extern unsigned short us;
371   auto r1 = (f <=> i);
372   ASSERT_EXPR_TYPE(r1, std::partial_ordering);
373 
374   auto r2 = (us <=> ld);
375   ASSERT_EXPR_TYPE(r2, std::partial_ordering);
376 
377   auto r3 = (s <=> f);
378   ASSERT_EXPR_TYPE(r3, std::partial_ordering);
379 
380   auto r4 = (0.0 <=> i);
381   ASSERT_EXPR_TYPE(r4, std::partial_ordering);
382 }
383 
384 namespace NullptrTest {
385 using nullptr_t = decltype(nullptr);
foo(nullptr_t x,nullptr_t y)386 void foo(nullptr_t x, nullptr_t y) {
387   auto r = x <=> y; // expected-error {{invalid operands}}
388 }
389 } // namespace NullptrTest
390 
391 namespace ComplexTest {
392 
393 enum class StrongE {};
394 enum WeakE { E_One,
395              E_Two };
396 
test_diag(_Complex int ci,_Complex float cf,_Complex double cd,int i,float f,StrongE E1,WeakE E2,int * p)397 void test_diag(_Complex int ci, _Complex float cf, _Complex double cd, int i, float f, StrongE E1, WeakE E2, int *p) {  // expected-warning 3 {{'_Complex' is a C99 extension}}
398   (void)(ci <=> (_Complex int &)ci); // expected-warning {{'_Complex' is a C99 extension}} expected-error {{invalid operands}}
399   (void)(ci <=> cf); // expected-error {{invalid operands}}
400   (void)(ci <=> i); // expected-error {{invalid operands}}
401   (void)(ci <=> f); // expected-error {{invalid operands}}
402   (void)(cf <=> i); // expected-error {{invalid operands}}
403   (void)(cf <=> f); // expected-error {{invalid operands}}
404   (void)(ci <=> p); // expected-error {{invalid operands}}
405   (void)(ci <=> E1); // expected-error {{invalid operands}}
406   (void)(E2 <=> cf); // expected-error {{invalid operands}}
407 }
408 
test_int(_Complex int x,_Complex int y)409 void test_int(_Complex int x, _Complex int y) { // expected-warning 2 {{'_Complex' is a C99 extension}}
410   auto r = x <=> y; // expected-error {{invalid operands}}
411 }
412 
test_double(_Complex double x,_Complex double y)413 void test_double(_Complex double x, _Complex double y) { // expected-warning 2 {{'_Complex' is a C99 extension}}
414   auto r = x <=> y; // expected-error {{invalid operands}}
415 }
416 
417 } // namespace ComplexTest
418 
419 namespace Vector {
420   typedef __attribute__((ext_vector_type(4))) int V;
f(V v1,V v2)421   void f(V v1, V v2) {
422     // This would logically result in a vector of std::strong_ordering, but we
423     // don't support vectors of class type. We could model this as a vector of
424     // int (-1 / 0 / 1), but that doesn't extend to floating-point types (how
425     // to represent 'unordered')? For now, just reject.
426     (void)(v1 <=> v2); // expected-error {{three-way comparison between vectors is not supported}}
427   }
428 }
429 
430 namespace PR44992 {
431   extern "C++" struct s {
432     friend auto operator<=>(s const &, s const &) = default;
433   };
434 }
435