Richard Smith | 762bb9d | 2011-10-13 22:29:44 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify -std=c++11 -Wsign-conversion %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. |
Richard Smith | 50d61c8 | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 5 | // This test runs in C++11 mode for the contextual conversion of the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 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 | |
David Blaikie | 654f1d5 | 2012-09-10 22:05:41 +0000 | [diff] [blame] | 60 | struct Abstract { |
| 61 | virtual ~Abstract() = 0; // expected-note {{unimplemented pure virtual method '~Abstract' in 'Abstract'}} |
| 62 | }; |
| 63 | |
| 64 | struct Derived1: Abstract { |
| 65 | }; |
| 66 | |
| 67 | struct Derived2: Abstract { |
| 68 | }; |
| 69 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 70 | void 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) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 78 | Fields flds; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 79 | i1 ? throw 0 : throw 1; |
| 80 | i1 ? test() : throw 1; |
| 81 | i1 ? throw 0 : test(); |
| 82 | i1 ? test() : test(); |
| 83 | i1 = i1 ? throw 0 : 0; |
| 84 | i1 = i1 ? 0 : throw 0; |
David Majnemer | c063cb1 | 2013-06-02 08:40:42 +0000 | [diff] [blame] | 85 | i1 = i1 ? (throw 0) : 0; |
| 86 | i1 = i1 ? 0 : (throw 0); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 87 | i1 ? 0 : test(); // expected-error {{right operand to ? is void, but left operand is of type 'int'}} |
| 88 | i1 ? test() : 0; // expected-error {{left operand to ? is void, but right operand is of type 'int'}} |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 89 | (i1 ? throw 0 : i1) = 0; |
| 90 | (i1 ? i1 : throw 0) = 0; |
| 91 | (i1 ? (throw 0) : i1) = 0; |
| 92 | (i1 ? i1 : (throw 0)) = 0; |
| 93 | (i1 ? (void)(throw 0) : i1) = 0; // expected-error {{left operand to ? is void, but right operand is of type 'int'}} |
| 94 | (i1 ? i1 : (void)(throw 0)) = 0; // expected-error {{right operand to ? is void, but left operand is of type 'int'}} |
| 95 | int &throwRef1 = (i1 ? flds.i1 : throw 0); |
| 96 | int &throwRef2 = (i1 ? throw 0 : flds.i1); |
| 97 | int &throwRef3 = (i1 ? flds.b1 : throw 0); // expected-error {{non-const reference cannot bind to bit-field}} |
| 98 | int &throwRef4 = (i1 ? throw 0 : flds.b1); // expected-error {{non-const reference cannot bind to bit-field}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 99 | |
| 100 | // p3 (one or both class type, convert to each other) |
| 101 | // b1 (lvalues) |
| 102 | Base base; |
| 103 | Derived derived; |
| 104 | Convertible conv; |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 105 | Base &bar1 = i1 ? base : derived; |
| 106 | Base &bar2 = i1 ? derived : base; |
| 107 | Base &bar3 = i1 ? base : conv; |
| 108 | Base &bar4 = i1 ? conv : base; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 109 | // these are ambiguous |
| 110 | BadBase bb; |
| 111 | BadDerived bd; |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 112 | (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] | 113 | (void)(i1 ? bd : bb); // expected-error {{conditional expression is ambiguous}} |
| 114 | // curiously enough (and a defect?), these are not |
| 115 | // for rvalues, hierarchy takes precedence over other conversions |
| 116 | (void)(i1 ? BadBase() : BadDerived()); |
| 117 | (void)(i1 ? BadDerived() : BadBase()); |
| 118 | |
| 119 | // b2.1 (hierarchy stuff) |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 120 | extern const Base constret(); |
| 121 | extern const Derived constder(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 122 | // should use const overload |
| 123 | A a1((i1 ? constret() : Base()).trick()); |
| 124 | A a2((i1 ? Base() : constret()).trick()); |
| 125 | A a3((i1 ? constret() : Derived()).trick()); |
| 126 | A a4((i1 ? Derived() : constret()).trick()); |
| 127 | // should use non-const overload |
| 128 | i1 = (i1 ? Base() : Base()).trick(); |
| 129 | i1 = (i1 ? Base() : Base()).trick(); |
| 130 | i1 = (i1 ? Base() : Derived()).trick(); |
| 131 | i1 = (i1 ? Derived() : Base()).trick(); |
| 132 | // should fail: const lost |
Chris Lattner | 0c42bb6 | 2010-09-05 00:17:29 +0000 | [diff] [blame] | 133 | (void)(i1 ? Base() : constder()); // expected-error {{incompatible operand types ('Base' and 'const Derived')}} |
| 134 | (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] | 135 | |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 136 | Priv priv; |
| 137 | Fin fin; |
John McCall | 6b2accb | 2010-02-10 09:31:12 +0000 | [diff] [blame] | 138 | (void)(i1 ? Base() : Priv()); // expected-error{{private base class}} |
| 139 | (void)(i1 ? Priv() : Base()); // expected-error{{private base class}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 140 | (void)(i1 ? Base() : Fin()); // expected-error{{ambiguous conversion from derived class 'Fin' to base class 'Base':}} |
| 141 | (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] | 142 | (void)(i1 ? base : priv); // expected-error {{private base class}} |
| 143 | (void)(i1 ? priv : base); // expected-error {{private base class}} |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 144 | (void)(i1 ? base : fin); // expected-error {{ambiguous conversion from derived class 'Fin' to base class 'Base':}} |
| 145 | (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] | 146 | |
| 147 | // b2.2 (non-hierarchy) |
| 148 | i1 = i1 ? I() : i1; |
| 149 | i1 = i1 ? i1 : I(); |
| 150 | I i2(i1 ? I() : J()); |
| 151 | I i3(i1 ? J() : I()); |
| 152 | // "the type [it] woud have if E2 were converted to an rvalue" |
| 153 | vfn pfn = i1 ? F() : test; |
| 154 | pfn = i1 ? test : F(); |
John McCall | 7c2342d | 2010-03-10 11:27:22 +0000 | [diff] [blame] | 155 | (void)(i1 ? A() : B()); // expected-error {{conversion from 'B' to 'A' is ambiguous}} |
| 156 | (void)(i1 ? B() : A()); // expected-error {{conversion from 'B' to 'A' is ambiguous}} |
| 157 | (void)(i1 ? 1 : Ambig()); // expected-error {{conversion from 'Ambig' to 'int' is ambiguous}} |
| 158 | (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] | 159 | // By the way, this isn't an lvalue: |
Richard Smith | 3fa3fea | 2013-02-02 02:14:45 +0000 | [diff] [blame] | 160 | &(i1 ? i1 : i2); // expected-error {{cannot take the address of an rvalue}} |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 161 | |
| 162 | // p4 (lvalue, same type) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 163 | int &ir1 = i1 ? flds.i1 : flds.i2; |
| 164 | (i1 ? flds.b1 : flds.i2) = 0; |
| 165 | (i1 ? flds.i1 : flds.b2) = 0; |
| 166 | (i1 ? flds.b1 : flds.b2) = 0; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 167 | |
| 168 | // p5 (conversion to built-in types) |
| 169 | // GCC 4.3 fails these |
| 170 | double d1 = i1 ? I() : K(); |
| 171 | pfn = i1 ? F() : G(); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 172 | DFnPtr pfm; |
Sebastian Redl | 78eb874 | 2009-04-19 21:53:20 +0000 | [diff] [blame] | 173 | pfm = i1 ? DFnPtr() : &Base::fn1; |
| 174 | pfm = i1 ? &Base::fn1 : DFnPtr(); |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 175 | |
| 176 | // p6 (final conversions) |
| 177 | i1 = i1 ? i1 : ir1; |
| 178 | int *pi1 = i1 ? &i1 : 0; |
| 179 | pi1 = i1 ? 0 : &i1; |
John McCall | a2936be | 2010-03-19 18:53:26 +0000 | [diff] [blame] | 180 | i1 = i1 ? i1 : EVal; |
| 181 | i1 = i1 ? EVal : i1; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 182 | d1 = i1 ? 'c' : 4.0; |
| 183 | d1 = i1 ? 4.0 : 'c'; |
Sebastian Redl | d1bd7fc | 2009-04-19 19:26:31 +0000 | [diff] [blame] | 184 | Base *pb = i1 ? (Base*)0 : (Derived*)0; |
| 185 | pb = i1 ? (Derived*)0 : (Base*)0; |
Sebastian Redl | 9bebfad | 2009-04-19 21:15:26 +0000 | [diff] [blame] | 186 | pfm = i1 ? &Base::fn1 : &Derived::fn2; |
| 187 | pfm = i1 ? &Derived::fn2 : &Base::fn1; |
| 188 | pfm = i1 ? &Derived::fn2 : 0; |
| 189 | pfm = i1 ? 0 : &Derived::fn2; |
| 190 | const int (MixedFieldsDerived::*mp1) = |
| 191 | i1 ? &MixedFields::ci : &MixedFieldsDerived::i; |
| 192 | const volatile int (MixedFields::*mp2) = |
| 193 | i1 ? &MixedFields::ci : &MixedFields::cvi; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 194 | (void)(i1 ? &MixedFields::ci : &MixedFields::vi); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 195 | // Conversion of primitives does not result in an lvalue. |
Richard Smith | 3fa3fea | 2013-02-02 02:14:45 +0000 | [diff] [blame] | 196 | &(i1 ? i1 : d1); // expected-error {{cannot take the address of an rvalue}} |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 197 | |
Anders Carlsson | 1d524c3 | 2009-09-14 23:15:26 +0000 | [diff] [blame] | 198 | (void)&(i1 ? flds.b1 : flds.i1); // expected-error {{address of bit-field requested}} |
| 199 | (void)&(i1 ? flds.i1 : flds.b1); // expected-error {{address of bit-field requested}} |
| 200 | |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 201 | |
| 202 | unsigned long test0 = 5; |
Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 203 | test0 = test0 ? (long) test0 : test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}} |
| 204 | test0 = test0 ? (int) test0 : test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}} |
| 205 | test0 = test0 ? (short) test0 : test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}} |
| 206 | test0 = test0 ? test0 : (long) test0; // expected-warning {{operand of ? changes signedness: 'long' to 'unsigned long'}} |
| 207 | test0 = test0 ? test0 : (int) test0; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}} |
| 208 | test0 = test0 ? test0 : (short) test0; // expected-warning {{operand of ? changes signedness: 'short' to 'unsigned long'}} |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 209 | test0 = test0 ? test0 : (long) 10; |
| 210 | test0 = test0 ? test0 : (int) 10; |
| 211 | test0 = test0 ? test0 : (short) 10; |
| 212 | test0 = test0 ? (long) 10 : test0; |
| 213 | test0 = test0 ? (int) 10 : test0; |
| 214 | test0 = test0 ? (short) 10 : test0; |
| 215 | |
Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 216 | int test1; |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 217 | test0 = test0 ? EVal : test0; |
Richard Trieu | 5254161 | 2011-07-21 02:46:28 +0000 | [diff] [blame] | 218 | test1 = test0 ? EVal : (int) test0; |
| 219 | |
| 220 | test0 = test0 ? EVal : test1; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}} |
| 221 | test0 = test0 ? test1 : EVal; // expected-warning {{operand of ? changes signedness: 'int' to 'unsigned long'}} |
| 222 | |
| 223 | test1 = test0 ? EVal : (int) test0; |
| 224 | test1 = test0 ? (int) test0 : EVal; |
John McCall | b13c87f | 2009-11-05 09:23:39 +0000 | [diff] [blame] | 225 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 226 | // Note the thing that this does not test: since DR446, various situations |
| 227 | // *must* create a separate temporary copy of class objects. This can only |
| 228 | // be properly tested at runtime, though. |
David Blaikie | 654f1d5 | 2012-09-10 22:05:41 +0000 | [diff] [blame] | 229 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 230 | const Abstract &abstract1 = true ? static_cast<const Abstract&>(Derived1()) : Derived2(); // expected-error {{allocating an object of abstract class type 'const Abstract'}} |
| 231 | const Abstract &abstract2 = true ? static_cast<const Abstract&>(Derived1()) : throw 3; // ok |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 232 | } |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 233 | |
| 234 | namespace PR6595 { |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 235 | struct OtherString { |
| 236 | OtherString(); |
| 237 | OtherString(const char*); |
| 238 | }; |
| 239 | |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 240 | struct String { |
| 241 | String(const char *); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 242 | String(const OtherString&); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 243 | operator const char*() const; |
| 244 | }; |
| 245 | |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 246 | void f(bool Cond, String S, OtherString OS) { |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 247 | (void)(Cond? S : ""); |
| 248 | (void)(Cond? "" : S); |
| 249 | const char a[1] = {'a'}; |
| 250 | (void)(Cond? S : a); |
| 251 | (void)(Cond? a : S); |
Douglas Gregor | 4712c02 | 2010-07-01 03:43:00 +0000 | [diff] [blame] | 252 | (void)(Cond? OS : S); |
Douglas Gregor | 0fd8ff7 | 2010-03-26 20:59:55 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 255 | |
| 256 | namespace PR6757 { |
| 257 | struct Foo1 { |
| 258 | Foo1(); |
| 259 | Foo1(const Foo1&); |
| 260 | }; |
| 261 | |
| 262 | struct Foo2 { }; |
| 263 | |
| 264 | struct Foo3 { |
| 265 | Foo3(); |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 266 | Foo3(Foo3&); // expected-note{{would lose const qualifier}} |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 267 | }; |
| 268 | |
| 269 | struct Bar { |
| 270 | operator const Foo1&() const; |
| 271 | operator const Foo2&() const; |
| 272 | operator const Foo3&() const; |
| 273 | }; |
| 274 | |
| 275 | void f() { |
| 276 | (void)(true ? Bar() : Foo1()); // okay |
| 277 | (void)(true ? Bar() : Foo2()); // okay |
Douglas Gregor | 3fbaf3e | 2010-04-17 22:01:05 +0000 | [diff] [blame] | 278 | (void)(true ? Bar() : Foo3()); // expected-error{{no viable constructor copying temporary}} |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
John McCall | 323ed74 | 2010-05-06 08:58:33 +0000 | [diff] [blame] | 281 | |
| 282 | // Reduced from selfhost. |
| 283 | namespace test1 { |
| 284 | struct A { |
| 285 | enum Foo { |
| 286 | fa, fb, fc, fd, fe, ff |
| 287 | }; |
| 288 | |
| 289 | Foo x(); |
| 290 | }; |
| 291 | |
| 292 | void foo(int); |
| 293 | |
| 294 | void test(A *a) { |
| 295 | foo(a ? a->x() : 0); |
| 296 | } |
| 297 | } |
Douglas Gregor | b65a458 | 2010-05-19 23:40:50 +0000 | [diff] [blame] | 298 | |
| 299 | namespace rdar7998817 { |
| 300 | class X { |
| 301 | X(X&); // expected-note{{declared private here}} |
| 302 | |
| 303 | struct ref { }; |
| 304 | |
| 305 | public: |
| 306 | X(); |
| 307 | X(ref); |
| 308 | |
| 309 | operator ref(); |
| 310 | }; |
| 311 | |
| 312 | void f(bool B) { |
| 313 | X x; |
| 314 | (void)(B? x // expected-error{{calling a private constructor of class 'rdar7998817::X'}} |
| 315 | : X()); |
| 316 | } |
| 317 | } |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 318 | |
| 319 | namespace PR7598 { |
| 320 | enum Enum { |
| 321 | v = 1, |
| 322 | }; |
| 323 | |
Chandler Carruth | 5495f37 | 2010-07-14 06:36:18 +0000 | [diff] [blame] | 324 | const Enum g() { |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 325 | return v; |
| 326 | } |
| 327 | |
Chandler Carruth | 5495f37 | 2010-07-14 06:36:18 +0000 | [diff] [blame] | 328 | const volatile Enum g2() { |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 329 | return v; |
| 330 | } |
| 331 | |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 332 | void f() { |
| 333 | const Enum v2 = v; |
| 334 | Enum e = false ? g() : v; |
| 335 | Enum e2 = false ? v2 : v; |
Douglas Gregor | de80ec1 | 2010-07-13 08:50:30 +0000 | [diff] [blame] | 336 | Enum e3 = false ? g2() : v; |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | } |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 340 | |
| 341 | namespace PR9236 { |
| 342 | #define NULL 0L |
| 343 | void f() { |
Chandler Carruth | 7ef9324 | 2011-02-19 00:13:59 +0000 | [diff] [blame] | 344 | int i; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 345 | (void)(true ? A() : NULL); // expected-error{{non-pointer operand type 'A' incompatible with NULL}} |
| 346 | (void)(true ? NULL : A()); // expected-error{{non-pointer operand type 'A' incompatible with NULL}} |
| 347 | (void)(true ? 0 : A()); // expected-error{{incompatible operand types}} |
| 348 | (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] | 349 | (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] | 350 | (void)(true ? __null : A()); // expected-error{{non-pointer operand type 'A' incompatible with NULL}} |
| 351 | (void)(true ? (void*)0 : A()); // expected-error{{incompatible operand types}} |
| 352 | } |
| 353 | } |
Richard Smith | 50d61c8 | 2012-08-08 06:13:49 +0000 | [diff] [blame] | 354 | |
| 355 | namespace DR587 { |
| 356 | template<typename T> |
| 357 | const T *f(bool b) { |
| 358 | static T t1 = T(); |
| 359 | static const T t2 = T(); |
| 360 | return &(b ? t1 : t2); |
| 361 | } |
| 362 | struct S {}; |
| 363 | template const int *f(bool); |
| 364 | template const S *f(bool); |
| 365 | |
| 366 | extern bool b; |
| 367 | int i = 0; |
| 368 | const int ci = 0; |
| 369 | volatile int vi = 0; |
| 370 | const volatile int cvi = 0; |
| 371 | |
| 372 | const int &cir = b ? i : ci; |
| 373 | volatile int &vir = b ? vi : i; |
| 374 | const volatile int &cvir1 = b ? ci : cvi; |
| 375 | const volatile int &cvir2 = b ? cvi : vi; |
| 376 | 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'}} |
| 377 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 378 | |
| 379 | namespace PR17052 { |
| 380 | struct X { |
| 381 | int i_; |
| 382 | bool b_; |
| 383 | |
| 384 | int &test() { return b_ ? i_ : throw 1; } |
| 385 | }; |
| 386 | } |