blob: 3fede303b64f6b90f7ef84873a20fd9a2673997f [file] [log] [blame]
Sebastian Redl3201f6b2009-04-16 17:51:27 +00001// RUN: clang-cc -fsyntax-only -verify -std=c++0x %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 { A(); A(const B&); };
11struct B { operator A() const; };
12struct I { operator int(); };
13struct J { operator I(); };
14struct K { operator double(); };
15typedef void (*vfn)();
16struct F { operator vfn(); };
17struct G { operator vfn(); };
18
19struct Base {
20 int trick();
21 A trick() const;
Sebastian Redl76458502009-04-17 16:30:52 +000022 void fn1();
Sebastian Redl3201f6b2009-04-16 17:51:27 +000023};
Sebastian Redl76458502009-04-17 16:30:52 +000024struct Derived : Base {
25 void fn2();
26};
Sebastian Redl3201f6b2009-04-16 17:51:27 +000027struct Convertible { operator Base&(); };
Sebastian Redla5cd2cd2009-04-26 11:21:02 +000028struct Priv : private Base {}; // expected-note 2 {{'private' inheritance specifier here}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +000029struct Mid : Base {};
30struct Fin : Mid, Derived {};
Sebastian Redl76458502009-04-17 16:30:52 +000031typedef void (Derived::*DFnPtr)();
32struct ToMemPtr { operator DFnPtr(); };
Sebastian Redl3201f6b2009-04-16 17:51:27 +000033
34struct BadDerived;
35struct BadBase { operator BadDerived&(); };
36struct BadDerived : BadBase {};
37
38struct Fields {
39 int i1, i2, b1 : 3, b2 : 3;
40};
Sebastian Redl9bebfad2009-04-19 21:15:26 +000041struct MixedFields {
42 int i;
43 volatile int vi;
44 const int ci;
45 const volatile int cvi;
46};
47struct MixedFieldsDerived : MixedFields {
48};
Sebastian Redl3201f6b2009-04-16 17:51:27 +000049
50enum Enum { EVal };
51
52struct Ambig {
53 operator short();
54 operator signed char();
55};
56
57void 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 Redl76458502009-04-17 16:30:52 +000081 Base &bar1 = i1 ? base : derived;
82 Base &bar2 = i1 ? derived : base;
83 Base &bar3 = i1 ? base : conv;
84 Base &bar4 = i1 ? conv : base;
Sebastian Redl3201f6b2009-04-16 17:51:27 +000085 // these are ambiguous
86 BadBase bb;
87 BadDerived bd;
88 (void)(i1 ? bb : bd); // expected-error {{conditional expression is ambiguous; 'struct BadBase' can be converted to 'struct BadDerived' and vice versa}}
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
109 (void)(i1 ? Base() : constder()); // expected-error {{incompatible operand types ('struct Base' and 'struct Derived const')}}
110 (void)(i1 ? constder() : Base()); // expected-error {{incompatible operand types ('struct Derived const' and 'struct Base')}}
Sebastian Redl78eb8742009-04-19 21:53:20 +0000111
112 // FIXME: these are invalid hierarchy conversions
113 Priv priv;
114 Fin fin;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000115 (void)(i1 ? Base() : Priv()); // xpected-error private base
116 (void)(i1 ? Priv() : Base()); // xpected-error private base
117 (void)(i1 ? Base() : Fin()); // xpected-error ambiguous base
118 (void)(i1 ? Fin() : Base()); // xpected-error ambiguous base
Sebastian Redla5cd2cd2009-04-26 11:21:02 +0000119 (void)(i1 ? base : priv); // expected-error {{conversion from 'struct Priv' to inaccessible base class 'struct Base'}}
120 (void)(i1 ? priv : base); // expected-error {{conversion from 'struct Priv' to inaccessible base class 'struct Base'}}
121 (void)(i1 ? base : fin); // expected-error {{ambiguous conversion from derived class 'struct Fin' to base class 'struct Base'}}
122 (void)(i1 ? fin : base); // expected-error {{ambiguous conversion from derived class 'struct Fin' to base class 'struct 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();
132 // these are ambiguous - better messages would be nice
133 (void)(i1 ? A() : B()); // expected-error {{incompatible operand types}}
134 (void)(i1 ? B() : A()); // expected-error {{incompatible operand types}}
135 (void)(i1 ? 1 : Ambig()); // expected-error {{incompatible operand types}}
136 (void)(i1 ? Ambig() : 1); // expected-error {{incompatible operand types}}
Sebastian Redl76458502009-04-17 16:30:52 +0000137 // By the way, this isn't an lvalue:
138 &(i1 ? i1 : i2); // expected-error {{address expression must be an lvalue or a function designator}}
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000139
140 // p4 (lvalue, same type)
Sebastian Redl76458502009-04-17 16:30:52 +0000141 Fields flds;
142 int &ir1 = i1 ? flds.i1 : flds.i2;
143 (i1 ? flds.b1 : flds.i2) = 0;
144 (i1 ? flds.i1 : flds.b2) = 0;
145 (i1 ? flds.b1 : flds.b2) = 0;
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000146
147 // p5 (conversion to built-in types)
148 // GCC 4.3 fails these
149 double d1 = i1 ? I() : K();
150 pfn = i1 ? F() : G();
Sebastian Redl76458502009-04-17 16:30:52 +0000151 DFnPtr pfm;
Sebastian Redl78eb8742009-04-19 21:53:20 +0000152 pfm = i1 ? DFnPtr() : &Base::fn1;
153 pfm = i1 ? &Base::fn1 : DFnPtr();
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000154
155 // p6 (final conversions)
156 i1 = i1 ? i1 : ir1;
157 int *pi1 = i1 ? &i1 : 0;
158 pi1 = i1 ? 0 : &i1;
159 i1 = i1 ? i1 : EVal;
160 i1 = i1 ? EVal : i1;
161 d1 = i1 ? 'c' : 4.0;
162 d1 = i1 ? 4.0 : 'c';
Sebastian Redld1bd7fc2009-04-19 19:26:31 +0000163 Base *pb = i1 ? (Base*)0 : (Derived*)0;
164 pb = i1 ? (Derived*)0 : (Base*)0;
Sebastian Redl9bebfad2009-04-19 21:15:26 +0000165 pfm = i1 ? &Base::fn1 : &Derived::fn2;
166 pfm = i1 ? &Derived::fn2 : &Base::fn1;
167 pfm = i1 ? &Derived::fn2 : 0;
168 pfm = i1 ? 0 : &Derived::fn2;
169 const int (MixedFieldsDerived::*mp1) =
170 i1 ? &MixedFields::ci : &MixedFieldsDerived::i;
171 const volatile int (MixedFields::*mp2) =
172 i1 ? &MixedFields::ci : &MixedFields::cvi;
173 i1 ? &MixedFields::ci : &MixedFields::vi; // expected-error {{incompatible operand types}}
Sebastian Redl76458502009-04-17 16:30:52 +0000174 // Conversion of primitives does not result in an lvalue.
175 &(i1 ? i1 : d1); // expected-error {{address expression must be an lvalue or a function designator}}
176
Sebastian Redl3201f6b2009-04-16 17:51:27 +0000177
178 // Note the thing that this does not test: since DR446, various situations
179 // *must* create a separate temporary copy of class objects. This can only
180 // be properly tested at runtime, though.
181}