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