blob: cb78a2e65f22cbf06abc85c04b837988d6568246 [file] [log] [blame]
John McCall7002f4c2010-04-09 19:03:51 +00001// RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -Wsign-compare %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.
5// This test runs in C++0x mode for the contextual conversion of the condition.
6
7struct ToBool { explicit operator bool(); };
8
9struct B;
Douglas Gregorb70cf442010-03-26 20:14:36 +000010struct A { A(); A(const B&); }; // expected-note 2 {{candidate constructor}} \
11 // expected-note 2 {{candidate is the implicit copy constructor}}
John McCall1d318332010-01-12 00:44:57 +000012struct B { operator A() const; }; // expected-note 2 {{candidate function}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000013struct I { operator int(); };
14struct J { operator I(); };
15struct K { operator double(); };
16typedef void (*vfn)();
17struct F { operator vfn(); };
18struct G { operator vfn(); };
19
20struct Base {
21 int trick();
22 A trick() const;
Sebastian Redl76458502009-04-17 16:30:52 +000023 void fn1();
Sebastian Redl3201f6b2009-04-16 17:51:27 +000024};
Sebastian Redl76458502009-04-17 16:30:52 +000025struct Derived : Base {
26 void fn2();
27};
Sebastian Redl3201f6b2009-04-16 17:51:27 +000028struct Convertible { operator Base&(); };
John McCall6b2accb2010-02-10 09:31:12 +000029struct Priv : private Base {}; // expected-note 4 {{declared private here}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000030struct Mid : Base {};
31struct Fin : Mid, Derived {};
Sebastian Redl76458502009-04-17 16:30:52 +000032typedef void (Derived::*DFnPtr)();
33struct ToMemPtr { operator DFnPtr(); };
Sebastian Redl3201f6b2009-04-16 17:51:27 +000034
35struct BadDerived;
36struct BadBase { operator BadDerived&(); };
37struct BadDerived : BadBase {};
38
39struct Fields {
40 int i1, i2, b1 : 3, b2 : 3;
41};
Sebastian Redl9bebfad2009-04-19 21:15:26 +000042struct MixedFields {
43 int i;
44 volatile int vi;
45 const int ci;
46 const volatile int cvi;
47};
48struct MixedFieldsDerived : MixedFields {
49};
Sebastian Redl3201f6b2009-04-16 17:51:27 +000050
51enum Enum { EVal };
52
53struct Ambig {
John McCall1d318332010-01-12 00:44:57 +000054 operator short(); // expected-note 2 {{candidate function}}
55 operator signed char(); // expected-note 2 {{candidate function}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000056};
57
58void test()
59{
60 // This function tests C++0x 5.16
61
62 // p1 (contextually convert to bool)
63 int i1 = ToBool() ? 0 : 1;
64
65 // p2 (one or both void, and throwing)
66 i1 ? throw 0 : throw 1;
67 i1 ? test() : throw 1;
68 i1 ? throw 0 : test();
69 i1 ? test() : test();
70 i1 = i1 ? throw 0 : 0;
71 i1 = i1 ? 0 : throw 0;
72 i1 ? 0 : test(); // expected-error {{right operand to ? is void, but left operand is of type 'int'}}
73 i1 ? test() : 0; // expected-error {{left operand to ? is void, but right operand is of type 'int'}}
74 (i1 ? throw 0 : i1) = 0; // expected-error {{expression is not assignable}}
75 (i1 ? i1 : throw 0) = 0; // expected-error {{expression is not assignable}}
76
77 // p3 (one or both class type, convert to each other)
78 // b1 (lvalues)
79 Base base;
80 Derived derived;
81 Convertible conv;
Sebastian Redl76458502009-04-17 16:30:52 +000082 Base &bar1 = i1 ? base : derived;
83 Base &bar2 = i1 ? derived : base;
84 Base &bar3 = i1 ? base : conv;
85 Base &bar4 = i1 ? conv : base;
Sebastian Redl3201f6b2009-04-16 17:51:27 +000086 // these are ambiguous
87 BadBase bb;
88 BadDerived bd;
John McCall7c2342d2010-03-10 11:27:22 +000089 (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 +000090 (void)(i1 ? bd : bb); // expected-error {{conditional expression is ambiguous}}
91 // curiously enough (and a defect?), these are not
92 // for rvalues, hierarchy takes precedence over other conversions
93 (void)(i1 ? BadBase() : BadDerived());
94 (void)(i1 ? BadDerived() : BadBase());
95
96 // b2.1 (hierarchy stuff)
97 const Base constret();
98 const Derived constder();
99 // should use const overload
100 A a1((i1 ? constret() : Base()).trick());
101 A a2((i1 ? Base() : constret()).trick());
102 A a3((i1 ? constret() : Derived()).trick());
103 A a4((i1 ? Derived() : constret()).trick());
104 // should use non-const overload
105 i1 = (i1 ? Base() : Base()).trick();
106 i1 = (i1 ? Base() : Base()).trick();
107 i1 = (i1 ? Base() : Derived()).trick();
108 i1 = (i1 ? Derived() : Base()).trick();
109 // should fail: const lost
John McCall7c2342d2010-03-10 11:27:22 +0000110 (void)(i1 ? Base() : constder()); // expected-error {{incompatible operand types ('Base' and 'Derived const')}}
111 (void)(i1 ? constder() : Base()); // expected-error {{incompatible operand types ('Derived const' and 'Base')}}
Sebastian Redl78eb8742009-04-19 21:53:20 +0000112
Sebastian Redl78eb8742009-04-19 21:53:20 +0000113 Priv priv;
114 Fin fin;
John McCall6b2accb2010-02-10 09:31:12 +0000115 (void)(i1 ? Base() : Priv()); // expected-error{{private base class}}
116 (void)(i1 ? Priv() : Base()); // expected-error{{private base class}}
John McCall7c2342d2010-03-10 11:27:22 +0000117 (void)(i1 ? Base() : Fin()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
118 (void)(i1 ? Fin() : Base()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
John McCall6b2accb2010-02-10 09:31:12 +0000119 (void)(i1 ? base : priv); // expected-error {{private base class}}
120 (void)(i1 ? priv : base); // expected-error {{private base class}}
John McCall7c2342d2010-03-10 11:27:22 +0000121 (void)(i1 ? base : fin); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
122 (void)(i1 ? fin : base); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000123
124 // b2.2 (non-hierarchy)
125 i1 = i1 ? I() : i1;
126 i1 = i1 ? i1 : I();
127 I i2(i1 ? I() : J());
128 I i3(i1 ? J() : I());
129 // "the type [it] woud have if E2 were converted to an rvalue"
130 vfn pfn = i1 ? F() : test;
131 pfn = i1 ? test : F();
John McCall7c2342d2010-03-10 11:27:22 +0000132 (void)(i1 ? A() : B()); // expected-error {{conversion from 'B' to 'A' is ambiguous}}
133 (void)(i1 ? B() : A()); // expected-error {{conversion from 'B' to 'A' is ambiguous}}
134 (void)(i1 ? 1 : Ambig()); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}}
135 (void)(i1 ? Ambig() : 1); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}}
Sebastian Redl76458502009-04-17 16:30:52 +0000136 // By the way, this isn't an lvalue:
137 &(i1 ? i1 : i2); // expected-error {{address expression must be an lvalue or a function designator}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000138
139 // p4 (lvalue, same type)
Sebastian Redl76458502009-04-17 16:30:52 +0000140 Fields flds;
141 int &ir1 = i1 ? flds.i1 : flds.i2;
142 (i1 ? flds.b1 : flds.i2) = 0;
143 (i1 ? flds.i1 : flds.b2) = 0;
144 (i1 ? flds.b1 : flds.b2) = 0;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000145
146 // p5 (conversion to built-in types)
147 // GCC 4.3 fails these
148 double d1 = i1 ? I() : K();
149 pfn = i1 ? F() : G();
Sebastian Redl76458502009-04-17 16:30:52 +0000150 DFnPtr pfm;
Sebastian Redl78eb8742009-04-19 21:53:20 +0000151 pfm = i1 ? DFnPtr() : &Base::fn1;
152 pfm = i1 ? &Base::fn1 : DFnPtr();
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000153
154 // p6 (final conversions)
155 i1 = i1 ? i1 : ir1;
156 int *pi1 = i1 ? &i1 : 0;
157 pi1 = i1 ? 0 : &i1;
John McCalla2936be2010-03-19 18:53:26 +0000158 i1 = i1 ? i1 : EVal;
159 i1 = i1 ? EVal : i1;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000160 d1 = i1 ? 'c' : 4.0;
161 d1 = i1 ? 4.0 : 'c';
Sebastian Redld1bd7fc2009-04-19 19:26:31 +0000162 Base *pb = i1 ? (Base*)0 : (Derived*)0;
163 pb = i1 ? (Derived*)0 : (Base*)0;
Sebastian Redl9bebfad2009-04-19 21:15:26 +0000164 pfm = i1 ? &Base::fn1 : &Derived::fn2;
165 pfm = i1 ? &Derived::fn2 : &Base::fn1;
166 pfm = i1 ? &Derived::fn2 : 0;
167 pfm = i1 ? 0 : &Derived::fn2;
168 const int (MixedFieldsDerived::*mp1) =
169 i1 ? &MixedFields::ci : &MixedFieldsDerived::i;
170 const volatile int (MixedFields::*mp2) =
171 i1 ? &MixedFields::ci : &MixedFields::cvi;
Douglas Gregor20b3e992009-08-24 17:42:35 +0000172 (void)(i1 ? &MixedFields::ci : &MixedFields::vi);
Sebastian Redl76458502009-04-17 16:30:52 +0000173 // Conversion of primitives does not result in an lvalue.
174 &(i1 ? i1 : d1); // expected-error {{address expression must be an lvalue or a function designator}}
175
Anders Carlsson1d524c32009-09-14 23:15:26 +0000176 (void)&(i1 ? flds.b1 : flds.i1); // expected-error {{address of bit-field requested}}
177 (void)&(i1 ? flds.i1 : flds.b1); // expected-error {{address of bit-field requested}}
178
John McCallb13c87f2009-11-05 09:23:39 +0000179
180 unsigned long test0 = 5;
181 test0 = test0 ? (long) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
182 test0 = test0 ? (int) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
183 test0 = test0 ? (short) test0 : test0; // expected-warning {{operands of ? are integers of different signs}}
184 test0 = test0 ? test0 : (long) test0; // expected-warning {{operands of ? are integers of different signs}}
185 test0 = test0 ? test0 : (int) test0; // expected-warning {{operands of ? are integers of different signs}}
186 test0 = test0 ? test0 : (short) test0; // expected-warning {{operands of ? are integers of different signs}}
187 test0 = test0 ? test0 : (long) 10;
188 test0 = test0 ? test0 : (int) 10;
189 test0 = test0 ? test0 : (short) 10;
190 test0 = test0 ? (long) 10 : test0;
191 test0 = test0 ? (int) 10 : test0;
192 test0 = test0 ? (short) 10 : test0;
193
194 test0 = test0 ? EVal : test0;
John McCalla2936be2010-03-19 18:53:26 +0000195 test0 = test0 ? EVal : (int) test0;
John McCallb13c87f2009-11-05 09:23:39 +0000196
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000197 // Note the thing that this does not test: since DR446, various situations
198 // *must* create a separate temporary copy of class objects. This can only
199 // be properly tested at runtime, though.
200}
Douglas Gregor0fd8ff72010-03-26 20:59:55 +0000201
202namespace PR6595 {
203 struct String {
204 String(const char *);
205 operator const char*() const;
206 };
207
208 void f(bool Cond, String S) {
209 (void)(Cond? S : "");
210 (void)(Cond? "" : S);
211 const char a[1] = {'a'};
212 (void)(Cond? S : a);
213 (void)(Cond? a : S);
214 }
215}
Douglas Gregor2f599792010-04-02 18:24:57 +0000216
217namespace PR6757 {
218 struct Foo1 {
219 Foo1();
220 Foo1(const Foo1&);
221 };
222
223 struct Foo2 { };
224
225 struct Foo3 {
226 Foo3();
227 Foo3(Foo3&);
228 };
229
230 struct Bar {
231 operator const Foo1&() const;
232 operator const Foo2&() const;
233 operator const Foo3&() const;
234 };
235
236 void f() {
237 (void)(true ? Bar() : Foo1()); // okay
238 (void)(true ? Bar() : Foo2()); // okay
239 // FIXME: Diagnostic below could be improved
240 (void)(true ? Bar() : Foo3()); // expected-error{{incompatible operand types ('PR6757::Bar' and 'PR6757::Foo3')}}
241 }
242}