Home | History | Annotate | Download | only in SemaCXX
      1 // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wsign-conversion %s
      2 
      3 // C++ rules for ?: are a lot stricter than C rules, and have to take into
      4 // account more conversion options.
      5 // This test runs in C++11 mode for the contextual conversion of the condition.
      6 
      7 struct ToBool { explicit operator bool(); };
      8 
      9 struct B;
     10 struct A {
     11   A();
     12   A(const B&); // expected-note 2 {{candidate constructor}}
     13 };
     14 struct B { operator A() const; }; // expected-note 2 {{candidate function}}
     15 struct I { operator int(); };
     16 struct J { operator I(); };
     17 struct K { operator double(); };
     18 typedef void (*vfn)();
     19 struct F { operator vfn(); };
     20 struct G { operator vfn(); };
     21 
     22 struct Base {
     23   int trick();
     24   A trick() const;
     25   void fn1();
     26 };
     27 struct Derived : Base {
     28   void fn2();
     29 };
     30 struct Convertible { operator Base&(); };
     31 struct Priv : private Base {}; // expected-note 4 {{declared private here}}
     32 struct Mid : Base {};
     33 struct Fin : Mid, Derived {};
     34 typedef void (Derived::*DFnPtr)();
     35 struct ToMemPtr { operator DFnPtr(); };
     36 
     37 struct BadDerived;
     38 struct BadBase { operator BadDerived&(); };
     39 struct BadDerived : BadBase {};
     40 
     41 struct Fields {
     42   int i1, i2, b1 : 3, b2 : 3;
     43 };
     44 struct MixedFields {
     45   int i;
     46   volatile int vi;
     47   const int ci;
     48   const volatile int cvi;
     49 };
     50 struct MixedFieldsDerived : MixedFields {
     51 };
     52 
     53 enum Enum { EVal };
     54 
     55 struct Ambig {
     56   operator short(); // expected-note 2 {{candidate function}}
     57   operator signed char(); // expected-note 2 {{candidate function}}
     58 };
     59 
     60 struct Abstract {
     61   virtual ~Abstract() = 0; // expected-note {{unimplemented pure virtual method '~Abstract' in 'Abstract'}}
     62 };
     63 
     64 struct Derived1: Abstract {
     65 };
     66 
     67 struct Derived2: Abstract {
     68 };
     69 
     70 void test()
     71 {
     72   // This function tests C++0x 5.16
     73 
     74   // p1 (contextually convert to bool)
     75   int i1 = ToBool() ? 0 : 1;
     76 
     77   // p2 (one or both void, and throwing)
     78   i1 ? throw 0 : throw 1;
     79   i1 ? test() : throw 1;
     80   i1 ? throw 0 : test();
     81   i1 ? test() : test();
     82   i1 = i1 ? throw 0 : 0;
     83   i1 = i1 ? 0 : throw 0;
     84   i1 = i1 ? (throw 0) : 0;
     85   i1 = i1 ? 0 : (throw 0);
     86   i1 ? 0 : test(); // expected-error {{right operand to ? is void, but left operand is of type 'int'}}
     87   i1 ? test() : 0; // expected-error {{left operand to ? is void, but right operand is of type 'int'}}
     88   (i1 ? throw 0 : i1) = 0; // expected-error {{expression is not assignable}}
     89   (i1 ? i1 : throw 0) = 0; // expected-error {{expression is not assignable}}
     90 
     91   // p3 (one or both class type, convert to each other)
     92   // b1 (lvalues)
     93   Base base;
     94   Derived derived;
     95   Convertible conv;
     96   Base &bar1 = i1 ? base : derived;
     97   Base &bar2 = i1 ? derived : base;
     98   Base &bar3 = i1 ? base : conv;
     99   Base &bar4 = i1 ? conv : base;
    100   // these are ambiguous
    101   BadBase bb;
    102   BadDerived bd;
    103   (void)(i1 ? bb : bd); // expected-error {{conditional expression is ambiguous; 'BadBase' can be converted to 'BadDerived' and vice versa}}
    104   (void)(i1 ? bd : bb); // expected-error {{conditional expression is ambiguous}}
    105   // curiously enough (and a defect?), these are not
    106   // for rvalues, hierarchy takes precedence over other conversions
    107   (void)(i1 ? BadBase() : BadDerived());
    108   (void)(i1 ? BadDerived() : BadBase());
    109 
    110   // b2.1 (hierarchy stuff)
    111   extern const Base constret();
    112   extern const Derived constder();
    113   // should use const overload
    114   A a1((i1 ? constret() : Base()).trick());
    115   A a2((i1 ? Base() : constret()).trick());
    116   A a3((i1 ? constret() : Derived()).trick());
    117   A a4((i1 ? Derived() : constret()).trick());
    118   // should use non-const overload
    119   i1 = (i1 ? Base() : Base()).trick();
    120   i1 = (i1 ? Base() : Base()).trick();
    121   i1 = (i1 ? Base() : Derived()).trick();
    122   i1 = (i1 ? Derived() : Base()).trick();
    123   // should fail: const lost
    124   (void)(i1 ? Base() : constder()); // expected-error {{incompatible operand types ('Base' and 'const Derived')}}
    125   (void)(i1 ? constder() : Base()); // expected-error {{incompatible operand types ('const Derived' and 'Base')}}
    126 
    127   Priv priv;
    128   Fin fin;
    129   (void)(i1 ? Base() : Priv()); // expected-error{{private base class}}
    130   (void)(i1 ? Priv() : Base()); // expected-error{{private base class}}
    131   (void)(i1 ? Base() : Fin()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
    132   (void)(i1 ? Fin() : Base()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
    133   (void)(i1 ? base : priv); // expected-error {{private base class}}
    134   (void)(i1 ? priv : base); // expected-error {{private base class}}
    135   (void)(i1 ? base : fin); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
    136   (void)(i1 ? fin : base); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
    137 
    138   // b2.2 (non-hierarchy)
    139   i1 = i1 ? I() : i1;
    140   i1 = i1 ? i1 : I();
    141   I i2(i1 ? I() : J());
    142   I i3(i1 ? J() : I());
    143   // "the type [it] woud have if E2 were converted to an rvalue"
    144   vfn pfn = i1 ? F() : test;
    145   pfn = i1 ? test : F();
    146   (void)(i1 ? A() : B()); // expected-error {{conversion from 'B' to 'A' is ambiguous}}
    147   (void)(i1 ? B() : A()); // expected-error {{conversion from 'B' to 'A' is ambiguous}}
    148   (void)(i1 ? 1 : Ambig()); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}}
    149   (void)(i1 ? Ambig() : 1); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}}
    150   // By the way, this isn't an lvalue:
    151   &(i1 ? i1 : i2); // expected-error {{cannot take the address of an rvalue}}
    152 
    153   // p4 (lvalue, same type)
    154   Fields flds;
    155   int &ir1 = i1 ? flds.i1 : flds.i2;
    156   (i1 ? flds.b1 : flds.i2) = 0;
    157   (i1 ? flds.i1 : flds.b2) = 0;
    158   (i1 ? flds.b1 : flds.b2) = 0;
    159 
    160   // p5 (conversion to built-in types)
    161   // GCC 4.3 fails these
    162   double d1 = i1 ? I() : K();
    163   pfn = i1 ? F() : G();
    164   DFnPtr pfm;
    165   pfm = i1 ? DFnPtr() : &Base::fn1;
    166   pfm = i1 ? &Base::fn1 : DFnPtr();
    167 
    168   // p6 (final conversions)
    169   i1 = i1 ? i1 : ir1;
    170   int *pi1 = i1 ? &i1 : 0;
    171   pi1 = i1 ? 0 : &i1;
    172   i1 = i1 ? i1 : EVal;
    173   i1 = i1 ? EVal : i1;
    174   d1 = i1 ? 'c' : 4.0;
    175   d1 = i1 ? 4.0 : 'c';
    176   Base *pb = i1 ? (Base*)0 : (Derived*)0;
    177   pb = i1 ? (Derived*)0 : (Base*)0;
    178   pfm = i1 ? &Base::fn1 : &Derived::fn2;
    179   pfm = i1 ? &Derived::fn2 : &Base::fn1;
    180   pfm = i1 ? &Derived::fn2 : 0;
    181   pfm = i1 ? 0 : &Derived::fn2;
    182   const int (MixedFieldsDerived::*mp1) =
    183     i1 ? &MixedFields::ci : &MixedFieldsDerived::i;
    184   const volatile int (MixedFields::*mp2) =
    185     i1 ? &MixedFields::ci : &MixedFields::cvi;
    186   (void)(i1 ? &MixedFields::ci : &MixedFields::vi);
    187   // Conversion of primitives does not result in an lvalue.
    188   &(i1 ? i1 : d1); // expected-error {{cannot take the address of an rvalue}}
    189 
    190   (void)&(i1 ? flds.b1 : flds.i1); // expected-error {{address of bit-field requested}}
    191   (void)&(i1 ? flds.i1 : flds.b1); // expected-error {{address of bit-field requested}}
    192 
    193 
    194   unsigned long test0 = 5;
    195   test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
    196   test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
    197   test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
    198   test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
    199   test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
    200   test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
    201   test0 = test0 ? test0 : (long) 10;
    202   test0 = test0 ? test0 : (int) 10;
    203   test0 = test0 ? test0 : (short) 10;
    204   test0 = test0 ? (long) 10 : test0;
    205   test0 = test0 ? (int) 10 : test0;
    206   test0 = test0 ? (short) 10 : test0;
    207 
    208   int test1;
    209   test0 = test0 ? EVal : test0;
    210   test1 = test0 ? EVal : (int) test0;
    211 
    212   test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
    213   test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
    214 
    215   test1 = test0 ? EVal : (int) test0;
    216   test1 = test0 ? (int) test0 : EVal;
    217 
    218   // Note the thing that this does not test: since DR446, various situations
    219   // *must* create a separate temporary copy of class objects. This can only
    220   // be properly tested at runtime, though.
    221 
    222   const Abstract &a = true ? static_cast<const Abstract&>(Derived1()) : Derived2(); // expected-error {{allocating an object of abstract class type 'const Abstract'}}
    223   true ? static_cast<const Abstract&>(Derived1()) : throw 3; // expected-error {{allocating an object of abstract class type 'const Abstract'}}
    224 }
    225 
    226 namespace PR6595 {
    227   struct OtherString {
    228     OtherString();
    229     OtherString(const char*);
    230   };
    231 
    232   struct String {
    233     String(const char *);
    234     String(const OtherString&);
    235     operator const char*() const;
    236   };
    237 
    238   void f(bool Cond, String S, OtherString OS) {
    239     (void)(Cond? S : "");
    240     (void)(Cond? "" : S);
    241     const char a[1] = {'a'};
    242     (void)(Cond? S : a);
    243     (void)(Cond? a : S);
    244     (void)(Cond? OS : S);
    245   }
    246 }
    247 
    248 namespace PR6757 {
    249   struct Foo1 {
    250     Foo1();
    251     Foo1(const Foo1&);
    252   };
    253 
    254   struct Foo2 { };
    255 
    256   struct Foo3 {
    257     Foo3();
    258     Foo3(Foo3&); // expected-note{{would lose const qualifier}}
    259   };
    260 
    261   struct Bar {
    262     operator const Foo1&() const;
    263     operator const Foo2&() const;
    264     operator const Foo3&() const;
    265   };
    266 
    267   void f() {
    268     (void)(true ? Bar() : Foo1()); // okay
    269     (void)(true ? Bar() : Foo2()); // okay
    270     (void)(true ? Bar() : Foo3()); // expected-error{{no viable constructor copying temporary}}
    271   }
    272 }
    273 
    274 // Reduced from selfhost.
    275 namespace test1 {
    276   struct A {
    277     enum Foo {
    278       fa, fb, fc, fd, fe, ff
    279     };
    280 
    281     Foo x();
    282   };
    283 
    284   void foo(int);
    285 
    286   void test(A *a) {
    287     foo(a ? a->x() : 0);
    288   }
    289 }
    290 
    291 namespace rdar7998817 {
    292   class X {
    293     X(X&); // expected-note{{declared private here}}
    294 
    295     struct ref { };
    296 
    297   public:
    298     X();
    299     X(ref);
    300 
    301     operator ref();
    302   };
    303 
    304   void f(bool B) {
    305     X x;
    306     (void)(B? x // expected-error{{calling a private constructor of class 'rdar7998817::X'}}
    307            : X());
    308   }
    309 }
    310 
    311 namespace PR7598 {
    312   enum Enum {
    313     v = 1,
    314   };
    315 
    316   const Enum g() {
    317     return v;
    318   }
    319 
    320   const volatile Enum g2() {
    321     return v;
    322   }
    323 
    324   void f() {
    325     const Enum v2 = v;
    326     Enum e = false ? g() : v;
    327     Enum e2 = false ? v2 : v;
    328     Enum e3 = false ? g2() : v;
    329   }
    330 
    331 }
    332 
    333 namespace PR9236 {
    334 #define NULL 0L
    335   void f() {
    336     int i;
    337     (void)(true ? A() : NULL); // expected-error{{non-pointer operand type 'A' incompatible with NULL}}
    338     (void)(true ? NULL : A()); // expected-error{{non-pointer operand type 'A' incompatible with NULL}}
    339     (void)(true ? 0 : A()); // expected-error{{incompatible operand types}}
    340     (void)(true ? nullptr : A()); // expected-error{{non-pointer operand type 'A' incompatible with nullptr}}
    341     (void)(true ? nullptr : i); // expected-error{{non-pointer operand type 'int' incompatible with nullptr}}
    342     (void)(true ? __null : A()); // expected-error{{non-pointer operand type 'A' incompatible with NULL}}
    343     (void)(true ? (void*)0 : A()); // expected-error{{incompatible operand types}}
    344   }
    345 }
    346 
    347 namespace DR587 {
    348   template<typename T>
    349   const T *f(bool b) {
    350     static T t1 = T();
    351     static const T t2 = T();
    352     return &(b ? t1 : t2);
    353   }
    354   struct S {};
    355   template const int *f(bool);
    356   template const S *f(bool);
    357 
    358   extern bool b;
    359   int i = 0;
    360   const int ci = 0;
    361   volatile int vi = 0;
    362   const volatile int cvi = 0;
    363 
    364   const int &cir = b ? i : ci;
    365   volatile int &vir = b ? vi : i;
    366   const volatile int &cvir1 = b ? ci : cvi;
    367   const volatile int &cvir2 = b ? cvi : vi;
    368   const volatile int &cvir3 = b ? ci : vi; // expected-error{{volatile lvalue reference to type 'const volatile int' cannot bind to a temporary of type 'int'}}
    369 }
    370