compare-cxx2a.cpp 17 KB

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