blob: 692aaefc9d54b72ef5d6b9400e2552425eda062a [file] [log] [blame]
Richard Smith762bb9d2011-10-13 22:29:44 +00001// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wsign-conversion %s
Sebastian Redl3201f6b2009-04-16 17:51:27 +00002
3// C++ rules for ?: are a lot stricter than C rules, and have to take into
4// account more conversion options.
Richard Smith50d61c82012-08-08 06:13:49 +00005// This test runs in C++11 mode for the contextual conversion of the condition.
Sebastian Redl3201f6b2009-04-16 17:51:27 +00006
7struct ToBool { explicit operator bool(); };
8
9struct B;
Douglas Gregor8dde14e2011-01-24 16:14:37 +000010struct A {
11 A();
12 A(const B&); // expected-note 2 {{candidate constructor}}
13};
John McCall1d318332010-01-12 00:44:57 +000014struct B { operator A() const; }; // expected-note 2 {{candidate function}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000015struct 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;
Sebastian Redl76458502009-04-17 16:30:52 +000025 void fn1();
Sebastian Redl3201f6b2009-04-16 17:51:27 +000026};
Sebastian Redl76458502009-04-17 16:30:52 +000027struct Derived : Base {
28 void fn2();
29};
Sebastian Redl3201f6b2009-04-16 17:51:27 +000030struct Convertible { operator Base&(); };
John McCall6b2accb2010-02-10 09:31:12 +000031struct Priv : private Base {}; // expected-note 4 {{declared private here}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000032struct Mid : Base {};
33struct Fin : Mid, Derived {};
Sebastian Redl76458502009-04-17 16:30:52 +000034typedef void (Derived::*DFnPtr)();
35struct ToMemPtr { operator DFnPtr(); };
Sebastian Redl3201f6b2009-04-16 17:51:27 +000036
37struct BadDerived;
38struct BadBase { operator BadDerived&(); };
39struct BadDerived : BadBase {};
40
41struct Fields {
42 int i1, i2, b1 : 3, b2 : 3;
43};
Sebastian Redl9bebfad2009-04-19 21:15:26 +000044struct MixedFields {
45 int i;
46 volatile int vi;
47 const int ci;
48 const volatile int cvi;
49};
50struct MixedFieldsDerived : MixedFields {
51};
Sebastian Redl3201f6b2009-04-16 17:51:27 +000052
53enum Enum { EVal };
54
55struct Ambig {
John McCall1d318332010-01-12 00:44:57 +000056 operator short(); // expected-note 2 {{candidate function}}
57 operator signed char(); // expected-note 2 {{candidate function}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000058};
59
David Blaikie654f1d52012-09-10 22:05:41 +000060struct 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
Sebastian Redl3201f6b2009-04-16 17:51:27 +000070void 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;
Sebastian Redl76458502009-04-17 16:30:52 +000094 Base &bar1 = i1 ? base : derived;
95 Base &bar2 = i1 ? derived : base;
96 Base &bar3 = i1 ? base : conv;
97 Base &bar4 = i1 ? conv : base;
Sebastian Redl3201f6b2009-04-16 17:51:27 +000098 // these are ambiguous
99 BadBase bb;
100 BadDerived bd;
John McCall7c2342d2010-03-10 11:27:22 +0000101 (void)(i1 ? bb : bd); // expected-error {{conditional expression is ambiguous; 'BadBase' can be converted to 'BadDerived' and vice versa}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000102 (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)
Richard Smith7984de32012-01-12 23:53:29 +0000109 extern const Base constret();
110 extern const Derived constder();
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000111 // 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
Chris Lattner0c42bb62010-09-05 00:17:29 +0000122 (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')}}
Sebastian Redl78eb8742009-04-19 21:53:20 +0000124
Sebastian Redl78eb8742009-04-19 21:53:20 +0000125 Priv priv;
126 Fin fin;
John McCall6b2accb2010-02-10 09:31:12 +0000127 (void)(i1 ? Base() : Priv()); // expected-error{{private base class}}
128 (void)(i1 ? Priv() : Base()); // expected-error{{private base class}}
John McCall7c2342d2010-03-10 11:27:22 +0000129 (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':}}
John McCall6b2accb2010-02-10 09:31:12 +0000131 (void)(i1 ? base : priv); // expected-error {{private base class}}
132 (void)(i1 ? priv : base); // expected-error {{private base class}}
John McCall7c2342d2010-03-10 11:27:22 +0000133 (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':}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000135
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();
John McCall7c2342d2010-03-10 11:27:22 +0000144 (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}}
Sebastian Redl76458502009-04-17 16:30:52 +0000148 // By the way, this isn't an lvalue:
Richard Smith3fa3fea2013-02-02 02:14:45 +0000149 &(i1 ? i1 : i2); // expected-error {{cannot take the address of an rvalue}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000150
151 // p4 (lvalue, same type)
Sebastian Redl76458502009-04-17 16:30:52 +0000152 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;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000157
158 // p5 (conversion to built-in types)
159 // GCC 4.3 fails these
160 double d1 = i1 ? I() : K();
161 pfn = i1 ? F() : G();
Sebastian Redl76458502009-04-17 16:30:52 +0000162 DFnPtr pfm;
Sebastian Redl78eb8742009-04-19 21:53:20 +0000163 pfm = i1 ? DFnPtr() : &Base::fn1;
164 pfm = i1 ? &Base::fn1 : DFnPtr();
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000165
166 // p6 (final conversions)
167 i1 = i1 ? i1 : ir1;
168 int *pi1 = i1 ? &i1 : 0;
169 pi1 = i1 ? 0 : &i1;
John McCalla2936be2010-03-19 18:53:26 +0000170 i1 = i1 ? i1 : EVal;
171 i1 = i1 ? EVal : i1;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000172 d1 = i1 ? 'c' : 4.0;
173 d1 = i1 ? 4.0 : 'c';
Sebastian Redld1bd7fc2009-04-19 19:26:31 +0000174 Base *pb = i1 ? (Base*)0 : (Derived*)0;
175 pb = i1 ? (Derived*)0 : (Base*)0;
Sebastian Redl9bebfad2009-04-19 21:15:26 +0000176 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;
Douglas Gregor20b3e992009-08-24 17:42:35 +0000184 (void)(i1 ? &MixedFields::ci : &MixedFields::vi);
Sebastian Redl76458502009-04-17 16:30:52 +0000185 // Conversion of primitives does not result in an lvalue.
Richard Smith3fa3fea2013-02-02 02:14:45 +0000186 &(i1 ? i1 : d1); // expected-error {{cannot take the address of an rvalue}}
Sebastian Redl76458502009-04-17 16:30:52 +0000187
Anders Carlsson1d524c32009-09-14 23:15:26 +0000188 (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
John McCallb13c87f2009-11-05 09:23:39 +0000191
192 unsigned long test0 = 5;
Richard Trieu52541612011-07-21 02:46:28 +0000193 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'}}
John McCallb13c87f2009-11-05 09:23:39 +0000199 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
Richard Trieu52541612011-07-21 02:46:28 +0000206 int test1;
John McCallb13c87f2009-11-05 09:23:39 +0000207 test0 = test0 ? EVal : test0;
Richard Trieu52541612011-07-21 02:46:28 +0000208 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;
John McCallb13c87f2009-11-05 09:23:39 +0000215
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000216 // 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.
David Blaikie654f1d52012-09-10 22:05:41 +0000219
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'}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000222}
Douglas Gregor0fd8ff72010-03-26 20:59:55 +0000223
224namespace PR6595 {
Douglas Gregor4712c022010-07-01 03:43:00 +0000225 struct OtherString {
226 OtherString();
227 OtherString(const char*);
228 };
229
Douglas Gregor0fd8ff72010-03-26 20:59:55 +0000230 struct String {
231 String(const char *);
Douglas Gregor4712c022010-07-01 03:43:00 +0000232 String(const OtherString&);
Douglas Gregor0fd8ff72010-03-26 20:59:55 +0000233 operator const char*() const;
234 };
235
Douglas Gregor4712c022010-07-01 03:43:00 +0000236 void f(bool Cond, String S, OtherString OS) {
Douglas Gregor0fd8ff72010-03-26 20:59:55 +0000237 (void)(Cond? S : "");
238 (void)(Cond? "" : S);
239 const char a[1] = {'a'};
240 (void)(Cond? S : a);
241 (void)(Cond? a : S);
Douglas Gregor4712c022010-07-01 03:43:00 +0000242 (void)(Cond? OS : S);
Douglas Gregor0fd8ff72010-03-26 20:59:55 +0000243 }
244}
Douglas Gregor2f599792010-04-02 18:24:57 +0000245
246namespace PR6757 {
247 struct Foo1 {
248 Foo1();
249 Foo1(const Foo1&);
250 };
251
252 struct Foo2 { };
253
254 struct Foo3 {
255 Foo3();
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +0000256 Foo3(Foo3&); // expected-note{{would lose const qualifier}}
Douglas Gregor2f599792010-04-02 18:24:57 +0000257 };
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
Douglas Gregor3fbaf3e2010-04-17 22:01:05 +0000268 (void)(true ? Bar() : Foo3()); // expected-error{{no viable constructor copying temporary}}
Douglas Gregor2f599792010-04-02 18:24:57 +0000269 }
270}
John McCall323ed742010-05-06 08:58:33 +0000271
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}
Douglas Gregorb65a4582010-05-19 23:40:50 +0000288
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}
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000308
309namespace PR7598 {
310 enum Enum {
311 v = 1,
312 };
313
Chandler Carruth5495f372010-07-14 06:36:18 +0000314 const Enum g() {
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000315 return v;
316 }
317
Chandler Carruth5495f372010-07-14 06:36:18 +0000318 const volatile Enum g2() {
Douglas Gregorde80ec12010-07-13 08:50:30 +0000319 return v;
320 }
321
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000322 void f() {
323 const Enum v2 = v;
324 Enum e = false ? g() : v;
325 Enum e2 = false ? v2 : v;
Douglas Gregorde80ec12010-07-13 08:50:30 +0000326 Enum e3 = false ? g2() : v;
Douglas Gregor5291c3c2010-07-13 08:18:22 +0000327 }
328
329}
Chandler Carruth82214a82011-02-18 23:54:50 +0000330
331namespace PR9236 {
332#define NULL 0L
333 void f() {
Chandler Carruth7ef93242011-02-19 00:13:59 +0000334 int i;
Chandler Carruth82214a82011-02-18 23:54:50 +0000335 (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}}
Chandler Carruth7ef93242011-02-19 00:13:59 +0000339 (void)(true ? nullptr : i); // expected-error{{non-pointer operand type 'int' incompatible with nullptr}}
Chandler Carruth82214a82011-02-18 23:54:50 +0000340 (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}
Richard Smith50d61c82012-08-08 06:13:49 +0000344
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}