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 | 8dde14e | 2011-01-24 16:14:37 +0000 | [diff] [blame] | 10 | struct A { |
| 11 | A(); |
| 12 | A(const B&); // expected-note 2 {{candidate constructor}} |
| 13 | }; |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 14 | struct B { operator A() const; }; // expected-note 2 {{candidate function}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 15 | struct I { operator int(); }; |
| 16 | struct J { operator I(); }; |
| 17 | struct K { operator double(); }; |
| 18 | typedef void (*vfn)(); |
| 19 | struct F { operator vfn(); }; |
| 20 | struct G { operator vfn(); }; |
| 21 | |
| 22 | struct Base { |
| 23 | int trick(); |
| 24 | A trick() const; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 25 | void fn1(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 26 | }; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 27 | struct Derived : Base { |
| 28 | void fn2(); |
| 29 | }; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 30 | struct Convertible { operator Base&(); }; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 31 | struct Priv : private Base {}; // expected-note 4 {{declared private here}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 32 | struct Mid : Base {}; |
| 33 | struct Fin : Mid, Derived {}; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 34 | typedef void (Derived::*DFnPtr)(); |
| 35 | struct ToMemPtr { operator DFnPtr(); }; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 36 | |
| 37 | struct BadDerived; |
| 38 | struct BadBase { operator BadDerived&(); }; |
| 39 | struct BadDerived : BadBase {}; |
| 40 | |
| 41 | struct Fields { |
| 42 | int i1, i2, b1 : 3, b2 : 3; |
| 43 | }; |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 44 | struct MixedFields { |
| 45 | int i; |
| 46 | volatile int vi; |
| 47 | const int ci; |
| 48 | const volatile int cvi; |
| 49 | }; |
| 50 | struct MixedFieldsDerived : MixedFields { |
| 51 | }; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 52 | |
| 53 | enum Enum { EVal }; |
| 54 | |
| 55 | struct Ambig { |
John McCall | 1d31833 | 2010-01-12 00:44:57 +0000 | [diff] [blame] | 56 | operator short(); // expected-note 2 {{candidate function}} |
| 57 | operator signed char(); // expected-note 2 {{candidate function}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 58 | }; |
| 59 | |
| 60 | void 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; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 84 | Base &bar1 = i1 ? base : derived; |
| 85 | Base &bar2 = i1 ? derived : base; |
| 86 | Base &bar3 = i1 ? base : conv; |
| 87 | Base &bar4 = i1 ? conv : base; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 88 | // these are ambiguous |
| 89 | BadBase bb; |
| 90 | BadDerived bd; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 91 | (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] | 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 | const Base constret(); |
| 100 | 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 |
Chris Lattner | 0c42bb6 | 2010-09-05 00:17:29 +0000 | [diff] [blame] | 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')}} |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 114 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 115 | Priv priv; |
| 116 | Fin fin; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 117 | (void)(i1 ? Base() : Priv()); // expected-error{{private base class}} |
| 118 | (void)(i1 ? Priv() : Base()); // expected-error{{private base class}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 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':}} |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 121 | (void)(i1 ? base : priv); // expected-error {{private base class}} |
| 122 | (void)(i1 ? priv : base); // expected-error {{private base class}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 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':}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 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(); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 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}} |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 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}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 140 | |
| 141 | // p4 (lvalue, same type) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 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; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 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(); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 152 | DFnPtr pfm; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 153 | pfm = i1 ? DFnPtr() : &Base::fn1; |
| 154 | pfm = i1 ? &Base::fn1 : DFnPtr(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 155 | |
| 156 | // p6 (final conversions) |
| 157 | i1 = i1 ? i1 : ir1; |
| 158 | int *pi1 = i1 ? &i1 : 0; |
| 159 | pi1 = i1 ? 0 : &i1; |
John McCall | a2936be | 2010-03-19 18:53:26 +0000 | [diff] [blame] | 160 | i1 = i1 ? i1 : EVal; |
| 161 | i1 = i1 ? EVal : i1; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 162 | d1 = i1 ? 'c' : 4.0; |
| 163 | d1 = i1 ? 4.0 : 'c'; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 164 | Base *pb = i1 ? (Base*)0 : (Derived*)0; |
| 165 | pb = i1 ? (Derived*)0 : (Base*)0; |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 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; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 174 | (void)(i1 ? &MixedFields::ci : &MixedFields::vi); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 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 | |
Anders Carlsson | 1d524c3 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 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 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 181 | |
| 182 | unsigned long test0 = 5; |
| 183 | test0 = test0 ? (long) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} |
| 184 | test0 = test0 ? (int) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} |
| 185 | test0 = test0 ? (short) test0 : test0; // expected-warning {{operands of ? are integers of different signs}} |
| 186 | test0 = test0 ? test0 : (long) test0; // expected-warning {{operands of ? are integers of different signs}} |
| 187 | test0 = test0 ? test0 : (int) test0; // expected-warning {{operands of ? are integers of different signs}} |
| 188 | test0 = test0 ? test0 : (short) test0; // expected-warning {{operands of ? are integers of different signs}} |
| 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 | test0 = test0 ? EVal : test0; |
John McCall | a2936be | 2010-03-19 18:53:26 +0000 | [diff] [blame] | 197 | test0 = test0 ? EVal : (int) test0; |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 198 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 199 | // Note the thing that this does not test: since DR446, various situations |
| 200 | // *must* create a separate temporary copy of class objects. This can only |
| 201 | // be properly tested at runtime, though. |
| 202 | } |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 203 | |
| 204 | namespace PR6595 { |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 205 | struct OtherString { |
| 206 | OtherString(); |
| 207 | OtherString(const char*); |
| 208 | }; |
| 209 | |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 210 | struct String { |
| 211 | String(const char *); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 212 | String(const OtherString&); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 213 | operator const char*() const; |
| 214 | }; |
| 215 | |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 216 | void f(bool Cond, String S, OtherString OS) { |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 217 | (void)(Cond? S : ""); |
| 218 | (void)(Cond? "" : S); |
| 219 | const char a[1] = {'a'}; |
| 220 | (void)(Cond? S : a); |
| 221 | (void)(Cond? a : S); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 222 | (void)(Cond? OS : S); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 223 | } |
| 224 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 225 | |
| 226 | namespace PR6757 { |
| 227 | struct Foo1 { |
| 228 | Foo1(); |
| 229 | Foo1(const Foo1&); |
| 230 | }; |
| 231 | |
| 232 | struct Foo2 { }; |
| 233 | |
| 234 | struct Foo3 { |
| 235 | Foo3(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 236 | Foo3(Foo3&); // expected-note{{would lose const qualifier}} |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 237 | }; |
| 238 | |
| 239 | struct Bar { |
| 240 | operator const Foo1&() const; |
| 241 | operator const Foo2&() const; |
| 242 | operator const Foo3&() const; |
| 243 | }; |
| 244 | |
| 245 | void f() { |
| 246 | (void)(true ? Bar() : Foo1()); // okay |
| 247 | (void)(true ? Bar() : Foo2()); // okay |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 248 | (void)(true ? Bar() : Foo3()); // expected-error{{no viable constructor copying temporary}} |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 251 | |
| 252 | // Reduced from selfhost. |
| 253 | namespace test1 { |
| 254 | struct A { |
| 255 | enum Foo { |
| 256 | fa, fb, fc, fd, fe, ff |
| 257 | }; |
| 258 | |
| 259 | Foo x(); |
| 260 | }; |
| 261 | |
| 262 | void foo(int); |
| 263 | |
| 264 | void test(A *a) { |
| 265 | foo(a ? a->x() : 0); |
| 266 | } |
| 267 | } |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 268 | |
| 269 | namespace rdar7998817 { |
| 270 | class X { |
| 271 | X(X&); // expected-note{{declared private here}} |
| 272 | |
| 273 | struct ref { }; |
| 274 | |
| 275 | public: |
| 276 | X(); |
| 277 | X(ref); |
| 278 | |
| 279 | operator ref(); |
| 280 | }; |
| 281 | |
| 282 | void f(bool B) { |
| 283 | X x; |
| 284 | (void)(B? x // expected-error{{calling a private constructor of class 'rdar7998817::X'}} |
| 285 | : X()); |
| 286 | } |
| 287 | } |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 288 | |
| 289 | namespace PR7598 { |
| 290 | enum Enum { |
| 291 | v = 1, |
| 292 | }; |
| 293 | |
Chandler Carruth | 5495f37 | 2010-07-14 06:36:18 +0000 | [diff] [blame] | 294 | const Enum g() { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 295 | return v; |
| 296 | } |
| 297 | |
Chandler Carruth | 5495f37 | 2010-07-14 06:36:18 +0000 | [diff] [blame] | 298 | const volatile Enum g2() { |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 299 | return v; |
| 300 | } |
| 301 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 302 | void f() { |
| 303 | const Enum v2 = v; |
| 304 | Enum e = false ? g() : v; |
| 305 | Enum e2 = false ? v2 : v; |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 306 | Enum e3 = false ? g2() : v; |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | } |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 310 | |
| 311 | namespace PR9236 { |
| 312 | #define NULL 0L |
| 313 | void f() { |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame^] | 314 | int i; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 315 | (void)(true ? A() : NULL); // expected-error{{non-pointer operand type 'A' incompatible with NULL}} |
| 316 | (void)(true ? NULL : A()); // expected-error{{non-pointer operand type 'A' incompatible with NULL}} |
| 317 | (void)(true ? 0 : A()); // expected-error{{incompatible operand types}} |
| 318 | (void)(true ? nullptr : A()); // expected-error{{non-pointer operand type 'A' incompatible with nullptr}} |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame^] | 319 | (void)(true ? nullptr : i); // expected-error{{non-pointer operand type 'int' incompatible with nullptr}} |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 320 | (void)(true ? __null : A()); // expected-error{{non-pointer operand type 'A' incompatible with NULL}} |
| 321 | (void)(true ? (void*)0 : A()); // expected-error{{incompatible operand types}} |
| 322 | } |
| 323 | } |