John McCall | 7002f4c | 2010-04-09 19:03:51 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -verify -std=c++0x -Wsign-compare %s |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 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 | |
| 7 | struct ToBool { explicit operator bool(); }; |
| 8 | |
| 9 | struct B; |
Douglas Gregor | d1a2722 | 2010-04-24 20:54:38 +0000 | [diff] [blame] | 10 | struct A { A(); A(const B&); }; // expected-note 2 {{candidate constructor}} |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 11 | struct B { operator A() const; }; // expected-note 2 {{candidate function}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 12 | struct I { operator int(); }; |
| 13 | struct J { operator I(); }; |
| 14 | struct K { operator double(); }; |
| 15 | typedef void (*vfn)(); |
| 16 | struct F { operator vfn(); }; |
| 17 | struct G { operator vfn(); }; |
| 18 | |
| 19 | struct Base { |
| 20 | int trick(); |
| 21 | A trick() const; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 22 | void fn1(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 23 | }; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 24 | struct Derived : Base { |
| 25 | void fn2(); |
| 26 | }; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 27 | struct Convertible { operator Base&(); }; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 28 | struct Priv : private Base {}; // expected-note 4 {{declared private here}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 29 | struct Mid : Base {}; |
| 30 | struct Fin : Mid, Derived {}; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 31 | typedef void (Derived::*DFnPtr)(); |
| 32 | struct ToMemPtr { operator DFnPtr(); }; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 33 | |
| 34 | struct BadDerived; |
| 35 | struct BadBase { operator BadDerived&(); }; |
| 36 | struct BadDerived : BadBase {}; |
| 37 | |
| 38 | struct Fields { |
| 39 | int i1, i2, b1 : 3, b2 : 3; |
| 40 | }; |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 41 | struct MixedFields { |
| 42 | int i; |
| 43 | volatile int vi; |
| 44 | const int ci; |
| 45 | const volatile int cvi; |
| 46 | }; |
| 47 | struct MixedFieldsDerived : MixedFields { |
| 48 | }; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 49 | |
| 50 | enum Enum { EVal }; |
| 51 | |
| 52 | struct Ambig { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 53 | operator short(); // expected-note 2 {{candidate function}} |
| 54 | operator signed char(); // expected-note 2 {{candidate function}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | void test() |
| 58 | { |
| 59 | // This function tests C++0x 5.16 |
| 60 | |
| 61 | // p1 (contextually convert to bool) |
| 62 | int i1 = ToBool() ? 0 : 1; |
| 63 | |
| 64 | // p2 (one or both void, and throwing) |
| 65 | i1 ? throw 0 : throw 1; |
| 66 | i1 ? test() : throw 1; |
| 67 | i1 ? throw 0 : test(); |
| 68 | i1 ? test() : test(); |
| 69 | i1 = i1 ? throw 0 : 0; |
| 70 | i1 = i1 ? 0 : throw 0; |
| 71 | i1 ? 0 : test(); // expected-error {{right operand to ? is void, but left operand is of type 'int'}} |
| 72 | i1 ? test() : 0; // expected-error {{left operand to ? is void, but right operand is of type 'int'}} |
| 73 | (i1 ? throw 0 : i1) = 0; // expected-error {{expression is not assignable}} |
| 74 | (i1 ? i1 : throw 0) = 0; // expected-error {{expression is not assignable}} |
| 75 | |
| 76 | // p3 (one or both class type, convert to each other) |
| 77 | // b1 (lvalues) |
| 78 | Base base; |
| 79 | Derived derived; |
| 80 | Convertible conv; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 81 | Base &bar1 = i1 ? base : derived; |
| 82 | Base &bar2 = i1 ? derived : base; |
| 83 | Base &bar3 = i1 ? base : conv; |
| 84 | Base &bar4 = i1 ? conv : base; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 85 | // these are ambiguous |
| 86 | BadBase bb; |
| 87 | BadDerived bd; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 88 | (void)(i1 ? bb : bd); // expected-error {{conditional expression is ambiguous; 'BadBase' can be converted to 'BadDerived' and vice versa}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 89 | (void)(i1 ? bd : bb); // expected-error {{conditional expression is ambiguous}} |
| 90 | // curiously enough (and a defect?), these are not |
| 91 | // for rvalues, hierarchy takes precedence over other conversions |
| 92 | (void)(i1 ? BadBase() : BadDerived()); |
| 93 | (void)(i1 ? BadDerived() : BadBase()); |
| 94 | |
| 95 | // b2.1 (hierarchy stuff) |
| 96 | const Base constret(); |
| 97 | const Derived constder(); |
| 98 | // should use const overload |
| 99 | A a1((i1 ? constret() : Base()).trick()); |
| 100 | A a2((i1 ? Base() : constret()).trick()); |
| 101 | A a3((i1 ? constret() : Derived()).trick()); |
| 102 | A a4((i1 ? Derived() : constret()).trick()); |
| 103 | // should use non-const overload |
| 104 | i1 = (i1 ? Base() : Base()).trick(); |
| 105 | i1 = (i1 ? Base() : Base()).trick(); |
| 106 | i1 = (i1 ? Base() : Derived()).trick(); |
| 107 | i1 = (i1 ? Derived() : Base()).trick(); |
| 108 | // should fail: const lost |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 109 | (void)(i1 ? Base() : constder()); // expected-error {{incompatible operand types ('Base' and 'Derived const')}} |
| 110 | (void)(i1 ? constder() : Base()); // expected-error {{incompatible operand types ('Derived const' and 'Base')}} |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 111 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 112 | Priv priv; |
| 113 | Fin fin; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 114 | (void)(i1 ? Base() : Priv()); // expected-error{{private base class}} |
| 115 | (void)(i1 ? Priv() : Base()); // expected-error{{private base class}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 116 | (void)(i1 ? Base() : Fin()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}} |
| 117 | (void)(i1 ? Fin() : Base()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}} |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 118 | (void)(i1 ? base : priv); // expected-error {{private base class}} |
| 119 | (void)(i1 ? priv : base); // expected-error {{private base class}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 120 | (void)(i1 ? base : fin); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}} |
| 121 | (void)(i1 ? fin : base); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 122 | |
| 123 | // b2.2 (non-hierarchy) |
| 124 | i1 = i1 ? I() : i1; |
| 125 | i1 = i1 ? i1 : I(); |
| 126 | I i2(i1 ? I() : J()); |
| 127 | I i3(i1 ? J() : I()); |
| 128 | // "the type [it] woud have if E2 were converted to an rvalue" |
| 129 | vfn pfn = i1 ? F() : test; |
| 130 | pfn = i1 ? test : F(); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 131 | (void)(i1 ? A() : B()); // expected-error {{conversion from 'B' to 'A' is ambiguous}} |
| 132 | (void)(i1 ? B() : A()); // expected-error {{conversion from 'B' to 'A' is ambiguous}} |
| 133 | (void)(i1 ? 1 : Ambig()); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}} |
| 134 | (void)(i1 ? Ambig() : 1); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}} |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 135 | // By the way, this isn't an lvalue: |
| 136 | &(i1 ? i1 : i2); // expected-error {{address expression must be an lvalue or a function designator}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 137 | |
| 138 | // p4 (lvalue, same type) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 139 | Fields flds; |
| 140 | int &ir1 = i1 ? flds.i1 : flds.i2; |
| 141 | (i1 ? flds.b1 : flds.i2) = 0; |
| 142 | (i1 ? flds.i1 : flds.b2) = 0; |
| 143 | (i1 ? flds.b1 : flds.b2) = 0; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 144 | |
| 145 | // p5 (conversion to built-in types) |
| 146 | // GCC 4.3 fails these |
| 147 | double d1 = i1 ? I() : K(); |
| 148 | pfn = i1 ? F() : G(); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 149 | DFnPtr pfm; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 150 | pfm = i1 ? DFnPtr() : &Base::fn1; |
| 151 | pfm = i1 ? &Base::fn1 : DFnPtr(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 152 | |
| 153 | // p6 (final conversions) |
| 154 | i1 = i1 ? i1 : ir1; |
| 155 | int *pi1 = i1 ? &i1 : 0; |
| 156 | pi1 = i1 ? 0 : &i1; |
John McCall | a2936be | 2010-03-19 18:53:26 +0000 | [diff] [blame] | 157 | i1 = i1 ? i1 : EVal; |
| 158 | i1 = i1 ? EVal : i1; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 159 | d1 = i1 ? 'c' : 4.0; |
| 160 | d1 = i1 ? 4.0 : 'c'; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 161 | Base *pb = i1 ? (Base*)0 : (Derived*)0; |
| 162 | pb = i1 ? (Derived*)0 : (Base*)0; |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 163 | pfm = i1 ? &Base::fn1 : &Derived::fn2; |
| 164 | pfm = i1 ? &Derived::fn2 : &Base::fn1; |
| 165 | pfm = i1 ? &Derived::fn2 : 0; |
| 166 | pfm = i1 ? 0 : &Derived::fn2; |
| 167 | const int (MixedFieldsDerived::*mp1) = |
| 168 | i1 ? &MixedFields::ci : &MixedFieldsDerived::i; |
| 169 | const volatile int (MixedFields::*mp2) = |
| 170 | i1 ? &MixedFields::ci : &MixedFields::cvi; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 171 | (void)(i1 ? &MixedFields::ci : &MixedFields::vi); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 172 | // Conversion of primitives does not result in an lvalue. |
| 173 | &(i1 ? i1 : d1); // expected-error {{address expression must be an lvalue or a function designator}} |
| 174 | |
Anders Carlsson | 1d524c3 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 175 | (void)&(i1 ? flds.b1 : flds.i1); // expected-error {{address of bit-field requested}} |
| 176 | (void)&(i1 ? flds.i1 : flds.b1); // expected-error {{address of bit-field requested}} |
| 177 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 178 | |
| 179 | unsigned long test0 = 5; |
| 180 | test0 = test0 ? (long) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} |
| 181 | test0 = test0 ? (int) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} |
| 182 | test0 = test0 ? (short) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} |
| 183 | test0 = test0 ? test0 : (long) test0; // expected-warning {{operands of ? are integers of different signs}} |
| 184 | test0 = test0 ? test0 : (int) test0; // expected-warning {{operands of ? are integers of different signs}} |
| 185 | test0 = test0 ? test0 : (short) test0; // expected-warning {{operands of ? are integers of different signs}} |
| 186 | test0 = test0 ? test0 : (long) 10; |
| 187 | test0 = test0 ? test0 : (int) 10; |
| 188 | test0 = test0 ? test0 : (short) 10; |
| 189 | test0 = test0 ? (long) 10 : test0; |
| 190 | test0 = test0 ? (int) 10 : test0; |
| 191 | test0 = test0 ? (short) 10 : test0; |
| 192 | |
| 193 | test0 = test0 ? EVal : test0; |
John McCall | a2936be | 2010-03-19 18:53:26 +0000 | [diff] [blame] | 194 | test0 = test0 ? EVal : (int) test0; |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 195 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 196 | // Note the thing that this does not test: since DR446, various situations |
| 197 | // *must* create a separate temporary copy of class objects. This can only |
| 198 | // be properly tested at runtime, though. |
| 199 | } |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 200 | |
| 201 | namespace PR6595 { |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 202 | struct OtherString { |
| 203 | OtherString(); |
| 204 | OtherString(const char*); |
| 205 | }; |
| 206 | |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 207 | struct String { |
| 208 | String(const char *); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 209 | String(const OtherString&); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 210 | operator const char*() const; |
| 211 | }; |
| 212 | |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 213 | void f(bool Cond, String S, OtherString OS) { |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 214 | (void)(Cond? S : ""); |
| 215 | (void)(Cond? "" : S); |
| 216 | const char a[1] = {'a'}; |
| 217 | (void)(Cond? S : a); |
| 218 | (void)(Cond? a : S); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 219 | (void)(Cond? OS : S); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 222 | |
| 223 | namespace PR6757 { |
| 224 | struct Foo1 { |
| 225 | Foo1(); |
| 226 | Foo1(const Foo1&); |
| 227 | }; |
| 228 | |
| 229 | struct Foo2 { }; |
| 230 | |
| 231 | struct Foo3 { |
| 232 | Foo3(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 233 | Foo3(Foo3&); // expected-note{{would lose const qualifier}} |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | struct Bar { |
| 237 | operator const Foo1&() const; |
| 238 | operator const Foo2&() const; |
| 239 | operator const Foo3&() const; |
| 240 | }; |
| 241 | |
| 242 | void f() { |
| 243 | (void)(true ? Bar() : Foo1()); // okay |
| 244 | (void)(true ? Bar() : Foo2()); // okay |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 245 | (void)(true ? Bar() : Foo3()); // expected-error{{no viable constructor copying temporary}} |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 246 | } |
| 247 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 248 | |
| 249 | // Reduced from selfhost. |
| 250 | namespace test1 { |
| 251 | struct A { |
| 252 | enum Foo { |
| 253 | fa, fb, fc, fd, fe, ff |
| 254 | }; |
| 255 | |
| 256 | Foo x(); |
| 257 | }; |
| 258 | |
| 259 | void foo(int); |
| 260 | |
| 261 | void test(A *a) { |
| 262 | foo(a ? a->x() : 0); |
| 263 | } |
| 264 | } |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 265 | |
| 266 | namespace rdar7998817 { |
| 267 | class X { |
| 268 | X(X&); // expected-note{{declared private here}} |
| 269 | |
| 270 | struct ref { }; |
| 271 | |
| 272 | public: |
| 273 | X(); |
| 274 | X(ref); |
| 275 | |
| 276 | operator ref(); |
| 277 | }; |
| 278 | |
| 279 | void f(bool B) { |
| 280 | X x; |
| 281 | (void)(B? x // expected-error{{calling a private constructor of class 'rdar7998817::X'}} |
| 282 | : X()); |
| 283 | } |
| 284 | } |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 285 | |
| 286 | namespace PR7598 { |
| 287 | enum Enum { |
| 288 | v = 1, |
| 289 | }; |
| 290 | |
Chandler Carruth | 5495f37 | 2010-07-14 06:36:18 +0000 | [diff] [blame^] | 291 | const Enum g() { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 292 | return v; |
| 293 | } |
| 294 | |
Chandler Carruth | 5495f37 | 2010-07-14 06:36:18 +0000 | [diff] [blame^] | 295 | const volatile Enum g2() { |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 296 | return v; |
| 297 | } |
| 298 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 299 | void f() { |
| 300 | const Enum v2 = v; |
| 301 | Enum e = false ? g() : v; |
| 302 | Enum e2 = false ? v2 : v; |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 303 | Enum e3 = false ? g2() : v; |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | } |