brintos

brintos / llvm-project-archived public Read only

0
0
Text · 20.9 KiB · 15a0bac Raw
505 lines · cpp
1// Force x86-64 because some of our heuristics are actually based2// on integer sizes.3 4// RUN: %clang_cc1 -triple x86_64-apple-darwin -fcxx-exceptions -fsyntax-only -pedantic -verify -Wsign-compare -std=c++2a %s5 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.pch7// RUN: %clang_cc1 -triple x86_64-apple-darwin -fcxx-exceptions -fsyntax-only -pedantic -verify -Wsign-compare -std=c++2a %s -include-pch %t.pch8 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 14struct S {15  static int x[5];16};17 18void 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 28void 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 134void test5(bool b, bool b2) {135  enum EnumA { A };136  (void)(b <=> b2);      // OK137  (void)(true <=> b);    // OK138  (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 147void 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.153void test7(unsigned long other) {154  // Common unsigned, other unsigned, constant unsigned155  (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 unsigned161  (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 signed167  (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 signed170  // Should not be possible as the common type should also be signed.171 172  // Common signed, other signed, constant signed173  (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 unsigned179  (void)((int)other <=> (unsigned char)(0xffff));180  (void)((int)other <=> (unsigned char)(0xff));181 182  // Common signed, other unsigned, constant signed183  (void)((unsigned char)other <=> (int)(0xff));184  (void)((unsigned char)other <=> (int)(0xffff));  // expected-warning{{less}}185 186  // Common signed, other unsigned, constant unsigned187  (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 192void 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 198void test9(long double ld, double d, float f, int i, long long ll) {199  (void)(f <=> ll); // OK, floating-point to integer is OK200  (void)(d <=> ld);201  (void)(i <=> f);202}203 204typedef int *INTPTR;205void test_typedef_bug(int *x, INTPTR y) {206  (void)(x <=> y);207}208 209using nullptr_t = decltype(nullptr);210 211struct Class {};212struct ClassB : Class {};213struct Class2 {};214using FnTy = void(int);215using MemFnTy = void (Class::*)() const;216using MemDataTy = long(Class::*);217 218void test_nullptr(int *x, FnTy *fp, MemFnTy memp, MemDataTy memdp) {219  auto r1 = (nullptr <=> nullptr); // expected-error {{invalid operands}}220  auto r2 = (nullptr <=> x); // expected-error {{invalid operands}}221  auto r3 = (fp <=> nullptr); // expected-error {{invalid operands}}222  auto r4 = (0 <=> fp); // expected-error {{ordered comparison between pointer and zero}}223  auto r5 = (nullptr <=> memp); // expected-error {{invalid operands}}224  auto r6 = (0 <=> memdp); // expected-error {{invalid operands}}225  auto r7 = (0 <=> nullptr); // expected-error {{invalid operands}}226}227 228void test_memptr(MemFnTy mf, MemDataTy md) {229  (void)(mf <=> mf); // expected-error {{invalid operands}} expected-warning {{self-comparison}}230  (void)(md <=> md); // expected-error {{invalid operands}} expected-warning {{self-comparison}}231}232 233// Test that variable narrowing is deferred for value dependent expressions234template <int Val>235auto test_template_overflow() {236  // expected-error@+1 {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned long'}}237  return (Val <=> (unsigned long)0);238}239template auto test_template_overflow<0>();240template auto test_template_overflow<-1>(); // expected-note {{requested here}}241 242void test_enum_integral_compare() {243  enum EnumA : int {A, ANeg = -1, AMax = __INT_MAX__};244  enum EnumB : unsigned {B, BMax = __UINT32_MAX__ };245  enum EnumC : int {C = -1, C0 = 0};246 247  (void)(A <=> C); // expected-error {{invalid operands to binary expression ('EnumA' and 'EnumC')}}248 249  (void)(A <=> (unsigned)0);250  (void)((unsigned)0 <=> A);251  (void)(ANeg <=> (unsigned)0); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}252  (void)((unsigned)0 <=> ANeg); // expected-error {{cannot be narrowed}}253 254  (void)(B <=> 42);255  (void)(42 <=> B);256  (void)(B <=> (unsigned long long)42);257  (void)(B <=> -1); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}258  (void)(BMax <=> (unsigned long)-1);259 260  (void)(C0 <=> (unsigned)42);261  (void)(C <=> (unsigned)42); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}262}263 264namespace EnumCompareTests {265 266enum class EnumA { A, A2 };267enum class EnumB { B };268enum class EnumC : unsigned { C };269 270void test_enum_enum_compare_no_builtin() {271  auto r1 = (EnumA::A <=> EnumA::A2); // OK272  ASSERT_EXPR_TYPE(r1, std::strong_ordering);273  (void)(EnumA::A <=> EnumA::A); // expected-warning {{self-comparison always evaluates to 'std::strong_ordering::equal'}}274  (void)(EnumA::A <=> EnumB::B); // expected-error {{invalid operands to binary expression ('EnumCompareTests::EnumA' and 'EnumCompareTests::EnumB')}}275  (void)(EnumB::B <=> EnumA::A); // expected-error {{invalid operands}}276}277 278template <int>279struct Tag {};280Tag<0> operator<=>(EnumA, EnumA) { // expected-note {{not viable}}281  return {};282}283// expected-note@+1 {{while rewriting comparison as call to 'operator<=>' declared here}}284Tag<1> operator<=>(EnumA, EnumB) { // expected-note {{not viable}}285  return {};286}287 288void test_enum_ovl_provided() {289  auto r1 = (EnumA::A <=> EnumA::A);290  ASSERT_EXPR_TYPE(r1, Tag<0>);291  auto r2 = (EnumA::A <=> EnumB::B);292  ASSERT_EXPR_TYPE(r2, Tag<1>);293  (void)(EnumB::B <=> EnumA::A); // expected-error {{invalid operands to binary expression ('int' and 'Tag<1>')}}294}295 296void enum_float_test() {297  enum EnumA { A };298  (void)(A <=> (float)0);       // expected-error {{invalid operands to binary expression ('EnumA' and 'float')}}299  (void)((double)0 <=> A);      // expected-error {{invalid operands to binary expression ('double' and 'EnumA')}}300  (void)((long double)0 <=> A); // expected-error {{invalid operands to binary expression ('long double' and 'EnumA')}}301}302 303enum class Bool1 : bool { Zero,304                          One };305enum Bool2 : bool { B2_Zero,306                    B2_One };307 308void test_bool_enum(Bool1 A1, Bool1 A2, Bool2 B1, Bool2 B2) {309  (void)(A1 <=> A2);310  (void)(B1 <=> B2);311}312 313} // namespace EnumCompareTests314 315namespace TestUserDefinedConvSeq {316 317template <class T, T Val>318struct Conv {319  constexpr operator T() const { return Val; }320  operator T() { return Val; }321};322 323void test_user_conv() {324  {325    using C = Conv<int, 0>;326    C c;327    const C cc;328    (void)(0 <=> c);329    (void)(c <=> -1);330    (void)((unsigned)0 <=> cc);331    (void)((unsigned)0 <=> c); // expected-error {{argument to 'operator<=>' cannot be narrowed from type 'int' to 'unsigned int'}}332  }333  {334    using C = Conv<int, -1>;335    C c;336    const C cc;337    (void)(c <=> 0);338    (void)(cc <=> (unsigned)0); // expected-error {{argument to 'operator<=>' evaluates to -1, which cannot be narrowed to type 'unsigned int'}}339    (void)(c <=> (unsigned)0);  // expected-error {{cannot be narrowed from type 'int' to 'unsigned int'}}340  }341}342 343struct X {344  constexpr const Conv<int, -1> operator<=>(X) { return {}; }345};346static_assert(X() < X());347 348} // namespace TestUserDefinedConvSeq349 350void test_array_conv() {351  int arr[5];352  int *ap = arr + 2;353  int arr2[3];354  (void)(arr <=> arr); // expected-error {{invalid operands to binary expression ('int[5]' and 'int[5]')}}355  (void)(+arr <=> arr);356}357 358void test_mixed_float_int(float f, double d, long double ld) {359  extern int i;360  extern unsigned u;361  extern long l;362  extern short s;363  extern unsigned short us;364  auto r1 = (f <=> i);365  ASSERT_EXPR_TYPE(r1, std::partial_ordering);366 367  auto r2 = (us <=> ld);368  ASSERT_EXPR_TYPE(r2, std::partial_ordering);369 370  auto r3 = (s <=> f);371  ASSERT_EXPR_TYPE(r3, std::partial_ordering);372 373  auto r4 = (0.0 <=> i);374  ASSERT_EXPR_TYPE(r4, std::partial_ordering);375}376 377namespace NullptrTest {378using nullptr_t = decltype(nullptr);379void foo(nullptr_t x, nullptr_t y) {380  auto r = x <=> y; // expected-error {{invalid operands}}381}382} // namespace NullptrTest383 384namespace ComplexTest {385 386enum class StrongE {};387enum WeakE { E_One,388             E_Two };389 390void 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}}391  (void)(ci <=> (_Complex int &)ci); // expected-warning {{'_Complex' is a C99 extension}} expected-error {{invalid operands}}392  (void)(ci <=> cf); // expected-error {{invalid operands}}393  (void)(ci <=> i); // expected-error {{invalid operands}}394  (void)(ci <=> f); // expected-error {{invalid operands}}395  (void)(cf <=> i); // expected-error {{invalid operands}}396  (void)(cf <=> f); // expected-error {{invalid operands}}397  (void)(ci <=> p); // expected-error {{invalid operands}}398  (void)(ci <=> E1); // expected-error {{invalid operands}}399  (void)(E2 <=> cf); // expected-error {{invalid operands}}400}401 402void test_int(_Complex int x, _Complex int y) { // expected-warning 2 {{'_Complex' is a C99 extension}}403  auto r = x <=> y; // expected-error {{invalid operands}}404}405 406void test_double(_Complex double x, _Complex double y) { // expected-warning 2 {{'_Complex' is a C99 extension}}407  auto r = x <=> y; // expected-error {{invalid operands}}408}409 410} // namespace ComplexTest411 412namespace Vector {413  typedef __attribute__((ext_vector_type(4))) int V;414  void f(V v1, V v2) {415    // This would logically result in a vector of std::strong_ordering, but we416    // don't support vectors of class type. We could model this as a vector of417    // int (-1 / 0 / 1), but that doesn't extend to floating-point types (how418    // to represent 'unordered')? For now, just reject.419    (void)(v1 <=> v2); // expected-error {{three-way comparison between vectors is not supported}}420  }421}422 423namespace PR44992 {424  extern "C++" struct s {425    friend auto operator<=>(s const &, s const &) = default;426  };427}428 429namespace PR52537 {430  template<typename T> struct X {};431  template<typename T> bool operator==(const X<T> &, int) { return T::error; } // expected-error 2{{no members}}432  template<typename T> int operator<=>(const X<T> &, int) { return T::error; } // expected-error 2{{no members}}433 434  const X<int[1]> x1;435  template<typename T> bool f1() { return x1 != 0; } // expected-note {{instantiation of}}436  void g1() { f1<int>(); } // expected-note {{instantiation of}}437 438  const X<int[2]> x2;439  template<typename T> bool f2() { return 0 == x2; } // expected-note {{instantiation of}}440  void g2() { f2<int>(); } // expected-note {{instantiation of}}441 442  const X<int[3]> x3;443  template<typename T> bool f3() { return x3 < 0; } // expected-note {{instantiation of}}444  void g3() { f3<int>(); } // expected-note {{instantiation of}}445 446  const X<int[4]> x4;447  template<typename T> bool f4() { return 0 >= x4; } // expected-note {{instantiation of}}448  void g4() { f4<int>(); } // expected-note {{instantiation of}}449 450  template<typename T> struct Y {};451  template<typename T> struct Z { Z(int) { T::error; } using nondeduced = Z; }; // expected-error 2{{no members}}452  template<typename T> Z<T> operator<=>(const Y<T>&, int);453  template<typename T> bool operator<(const Z<T>&, const typename Z<T>::nondeduced&);454  template<typename T> bool operator<(const typename Z<T>::nondeduced&, const Z<T>&);455 456  const Y<int[5]> y5;457  template<typename T> bool f5() { return y5 < 0; } // expected-note {{instantiation of}}458  void g5() { f5<int>(); } // expected-note {{instantiation of}}459 460  const Y<int[6]> y6;461  template<typename T> bool f6() { return 0 < y6; } // expected-note {{instantiation of}}462  void g6() { f6<int>(); } // expected-note {{instantiation of}}463}464 465 466namespace GH64923 {467using nullptr_t = decltype(nullptr);468struct MyTask{};469constexpr MyTask DoAnotherThing() {470    return {};471}472 473constexpr nullptr_t operator++(MyTask &&T); // expected-note 2{{declared here}}474void DoSomething() {475  if constexpr (++DoAnotherThing() != nullptr) {} // expected-error {{constexpr if condition is not a constant expression}} \476                                                  // expected-note  {{undefined function 'operator++' cannot be used in a constant expression}}477 478  if constexpr (nullptr == ++DoAnotherThing()) {} // expected-error {{constexpr if condition is not a constant expression}} \479                                                  // expected-note  {{undefined function 'operator++' cannot be used in a constant expression}}480}481}482 483namespace GH64162 {484struct S {485    const std::strong_ordering& operator<=>(const S&) const = default;486};487bool test(S s) {488    return s < s; // We expect this not to crash anymore489}490 491// Following example never crashed but worth adding in because it is related492struct A {};493bool operator<(A, int);494 495struct B {496    operator A();497};498 499struct C {500    B operator<=>(C);501};502 503bool f(C c) { return c < c; }504}505