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++0x 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
60void test()
61{
62  // This function tests C++0x 5.16
63
64  // p1 (contextually convert to bool)
65  int i1 = ToBool() ? 0 : 1;
66
67  // p2 (one or both void, and throwing)
68  i1 ? throw 0 : throw 1;
69  i1 ? test() : throw 1;
70  i1 ? throw 0 : test();
71  i1 ? test() : test();
72  i1 = i1 ? throw 0 : 0;
73  i1 = i1 ? 0 : throw 0;
74  i1 ? 0 : test(); // expected-error {{right operand to ? is void, but left operand is of type 'int'}}
75  i1 ? test() : 0; // expected-error {{left operand to ? is void, but right operand is of type 'int'}}
76  (i1 ? throw 0 : i1) = 0; // expected-error {{expression is not assignable}}
77  (i1 ? i1 : throw 0) = 0; // expected-error {{expression is not assignable}}
78
79  // p3 (one or both class type, convert to each other)
80  // b1 (lvalues)
81  Base base;
82  Derived derived;
83  Convertible conv;
84  Base &bar1 = i1 ? base : derived;
85  Base &bar2 = i1 ? derived : base;
86  Base &bar3 = i1 ? base : conv;
87  Base &bar4 = i1 ? conv : base;
88  // these are ambiguous
89  BadBase bb;
90  BadDerived bd;
91  (void)(i1 ? bb : bd); // expected-error {{conditional expression is ambiguous; 'BadBase' can be converted to 'BadDerived' and vice versa}}
92  (void)(i1 ? bd : bb); // expected-error {{conditional expression is ambiguous}}
93  // curiously enough (and a defect?), these are not
94  // for rvalues, hierarchy takes precedence over other conversions
95  (void)(i1 ? BadBase() : BadDerived());
96  (void)(i1 ? BadDerived() : BadBase());
97
98  // b2.1 (hierarchy stuff)
99  extern const Base constret();
100  extern const Derived constder();
101  // should use const overload
102  A a1((i1 ? constret() : Base()).trick());
103  A a2((i1 ? Base() : constret()).trick());
104  A a3((i1 ? constret() : Derived()).trick());
105  A a4((i1 ? Derived() : constret()).trick());
106  // should use non-const overload
107  i1 = (i1 ? Base() : Base()).trick();
108  i1 = (i1 ? Base() : Base()).trick();
109  i1 = (i1 ? Base() : Derived()).trick();
110  i1 = (i1 ? Derived() : Base()).trick();
111  // should fail: const lost
112  (void)(i1 ? Base() : constder()); // expected-error {{incompatible operand types ('Base' and 'const Derived')}}
113  (void)(i1 ? constder() : Base()); // expected-error {{incompatible operand types ('const Derived' and 'Base')}}
114
115  Priv priv;
116  Fin fin;
117  (void)(i1 ? Base() : Priv()); // expected-error{{private base class}}
118  (void)(i1 ? Priv() : Base()); // expected-error{{private base class}}
119  (void)(i1 ? Base() : Fin()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
120  (void)(i1 ? Fin() : Base()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
121  (void)(i1 ? base : priv); // expected-error {{private base class}}
122  (void)(i1 ? priv : base); // expected-error {{private base class}}
123  (void)(i1 ? base : fin); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
124  (void)(i1 ? fin : base); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
125
126  // b2.2 (non-hierarchy)
127  i1 = i1 ? I() : i1;
128  i1 = i1 ? i1 : I();
129  I i2(i1 ? I() : J());
130  I i3(i1 ? J() : I());
131  // "the type [it] woud have if E2 were converted to an rvalue"
132  vfn pfn = i1 ? F() : test;
133  pfn = i1 ? test : F();
134  (void)(i1 ? A() : B()); // expected-error {{conversion from 'B' to 'A' is ambiguous}}
135  (void)(i1 ? B() : A()); // expected-error {{conversion from 'B' to 'A' is ambiguous}}
136  (void)(i1 ? 1 : Ambig()); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}}
137  (void)(i1 ? Ambig() : 1); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}}
138  // By the way, this isn't an lvalue:
139  &(i1 ? i1 : i2); // expected-error {{address expression must be an lvalue or a function designator}}
140
141  // p4 (lvalue, same type)
142  Fields flds;
143  int &ir1 = i1 ? flds.i1 : flds.i2;
144  (i1 ? flds.b1 : flds.i2) = 0;
145  (i1 ? flds.i1 : flds.b2) = 0;
146  (i1 ? flds.b1 : flds.b2) = 0;
147
148  // p5 (conversion to built-in types)
149  // GCC 4.3 fails these
150  double d1 = i1 ? I() : K();
151  pfn = i1 ? F() : G();
152  DFnPtr pfm;
153  pfm = i1 ? DFnPtr() : &Base::fn1;
154  pfm = i1 ? &Base::fn1 : DFnPtr();
155
156  // p6 (final conversions)
157  i1 = i1 ? i1 : ir1;
158  int *pi1 = i1 ? &i1 : 0;
159  pi1 = i1 ? 0 : &i1;
160  i1 = i1 ? i1 : EVal;
161  i1 = i1 ? EVal : i1;
162  d1 = i1 ? 'c' : 4.0;
163  d1 = i1 ? 4.0 : 'c';
164  Base *pb = i1 ? (Base*)0 : (Derived*)0;
165  pb = i1 ? (Derived*)0 : (Base*)0;
166  pfm = i1 ? &Base::fn1 : &Derived::fn2;
167  pfm = i1 ? &Derived::fn2 : &Base::fn1;
168  pfm = i1 ? &Derived::fn2 : 0;
169  pfm = i1 ? 0 : &Derived::fn2;
170  const int (MixedFieldsDerived::*mp1) =
171    i1 ? &MixedFields::ci : &MixedFieldsDerived::i;
172  const volatile int (MixedFields::*mp2) =
173    i1 ? &MixedFields::ci : &MixedFields::cvi;
174  (void)(i1 ? &MixedFields::ci : &MixedFields::vi);
175  // Conversion of primitives does not result in an lvalue.
176  &(i1 ? i1 : d1); // expected-error {{address expression must be an lvalue or a function designator}}
177
178  (void)&(i1 ? flds.b1 : flds.i1); // expected-error {{address of bit-field requested}}
179  (void)&(i1 ? flds.i1 : flds.b1); // expected-error {{address of bit-field requested}}
180
181
182  unsigned long test0 = 5;
183  test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
184  test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
185  test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
186  test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}}
187  test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
188  test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}}
189  test0 = test0 ? test0 : (long) 10;
190  test0 = test0 ? test0 : (int) 10;
191  test0 = test0 ? test0 : (short) 10;
192  test0 = test0 ? (long) 10 : test0;
193  test0 = test0 ? (int) 10 : test0;
194  test0 = test0 ? (short) 10 : test0;
195
196  int test1;
197  test0 = test0 ? EVal : test0;
198  test1 = test0 ? EVal : (int) test0;
199
200  test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
201  test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}}
202
203  test1 = test0 ? EVal : (int) test0;
204  test1 = test0 ? (int) test0 : EVal;
205
206  // Note the thing that this does not test: since DR446, various situations
207  // *must* create a separate temporary copy of class objects. This can only
208  // be properly tested at runtime, though.
209}
210
211namespace PR6595 {
212  struct OtherString {
213    OtherString();
214    OtherString(const char*);
215  };
216
217  struct String {
218    String(const char *);
219    String(const OtherString&);
220    operator const char*() const;
221  };
222
223  void f(bool Cond, String S, OtherString OS) {
224    (void)(Cond? S : "");
225    (void)(Cond? "" : S);
226    const char a[1] = {'a'};
227    (void)(Cond? S : a);
228    (void)(Cond? a : S);
229    (void)(Cond? OS : S);
230  }
231}
232
233namespace PR6757 {
234  struct Foo1 {
235    Foo1();
236    Foo1(const Foo1&);
237  };
238
239  struct Foo2 { };
240
241  struct Foo3 {
242    Foo3();
243    Foo3(Foo3&); // expected-note{{would lose const qualifier}}
244  };
245
246  struct Bar {
247    operator const Foo1&() const;
248    operator const Foo2&() const;
249    operator const Foo3&() const;
250  };
251
252  void f() {
253    (void)(true ? Bar() : Foo1()); // okay
254    (void)(true ? Bar() : Foo2()); // okay
255    (void)(true ? Bar() : Foo3()); // expected-error{{no viable constructor copying temporary}}
256  }
257}
258
259// Reduced from selfhost.
260namespace test1 {
261  struct A {
262    enum Foo {
263      fa, fb, fc, fd, fe, ff
264    };
265
266    Foo x();
267  };
268
269  void foo(int);
270
271  void test(A *a) {
272    foo(a ? a->x() : 0);
273  }
274}
275
276namespace rdar7998817 {
277  class X {
278    X(X&); // expected-note{{declared private here}}
279
280    struct ref { };
281
282  public:
283    X();
284    X(ref);
285
286    operator ref();
287  };
288
289  void f(bool B) {
290    X x;
291    (void)(B? x // expected-error{{calling a private constructor of class 'rdar7998817::X'}}
292           : X());
293  }
294}
295
296namespace PR7598 {
297  enum Enum {
298    v = 1,
299  };
300
301  const Enum g() {
302    return v;
303  }
304
305  const volatile Enum g2() {
306    return v;
307  }
308
309  void f() {
310    const Enum v2 = v;
311    Enum e = false ? g() : v;
312    Enum e2 = false ? v2 : v;
313    Enum e3 = false ? g2() : v;
314  }
315
316}
317
318namespace PR9236 {
319#define NULL 0L
320  void f() {
321    int i;
322    (void)(true ? A() : NULL); // expected-error{{non-pointer operand type 'A' incompatible with NULL}}
323    (void)(true ? NULL : A()); // expected-error{{non-pointer operand type 'A' incompatible with NULL}}
324    (void)(true ? 0 : A()); // expected-error{{incompatible operand types}}
325    (void)(true ? nullptr : A()); // expected-error{{non-pointer operand type 'A' incompatible with nullptr}}
326    (void)(true ? nullptr : i); // expected-error{{non-pointer operand type 'int' incompatible with nullptr}}
327    (void)(true ? __null : A()); // expected-error{{non-pointer operand type 'A' incompatible with NULL}}
328    (void)(true ? (void*)0 : A()); // expected-error{{incompatible operand types}}
329  }
330}
331