xref: /aosp_15_r20/external/clang/test/SemaCXX/member-init.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -fcxx-exceptions -verify -std=c++11 -Wall %s
2*67e74705SXin Li 
3*67e74705SXin Li struct Bitfield {
4*67e74705SXin Li   int n : 3 = 7; // expected-error {{bitfield member cannot have an in-class initializer}}
5*67e74705SXin Li };
6*67e74705SXin Li 
7*67e74705SXin Li int a;
8*67e74705SXin Li class NoWarning {
9*67e74705SXin Li   int &n = a;
10*67e74705SXin Li public:
GetN()11*67e74705SXin Li   int &GetN() { return n; }
12*67e74705SXin Li };
13*67e74705SXin Li 
14*67e74705SXin Li bool b();
15*67e74705SXin Li int k;
16*67e74705SXin Li struct Recurse {
17*67e74705SXin Li   int &n = // expected-error {{cannot use defaulted default constructor of 'Recurse' within the class outside of member functions because 'n' has an initializer}}
18*67e74705SXin Li       b() ?
19*67e74705SXin Li       Recurse().n : // expected-note {{implicit default constructor for 'Recurse' first required here}}
20*67e74705SXin Li       k;
21*67e74705SXin Li };
22*67e74705SXin Li 
23*67e74705SXin Li struct UnknownBound {
24*67e74705SXin Li   int as[] = { 1, 2, 3 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
25*67e74705SXin Li   int bs[4] = { 4, 5, 6, 7 };
26*67e74705SXin Li   int cs[] = { 8, 9, 10 }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
27*67e74705SXin Li };
28*67e74705SXin Li 
29*67e74705SXin Li template<int n> struct T { static const int B; };
30*67e74705SXin Li template<> struct T<2> { template<int C, int D> using B = int; };
31*67e74705SXin Li const int C = 0, D = 0;
32*67e74705SXin Li struct S {
33*67e74705SXin Li   int as[] = { decltype(x)::B<C, D>(0) }; // expected-error {{array bound cannot be deduced from an in-class initializer}}
34*67e74705SXin Li   T<sizeof(as) / sizeof(int)> x;
35*67e74705SXin Li   // test that we handle invalid array bound deductions without crashing when the declarator name is itself invalid
operator int[]S36*67e74705SXin Li   operator int[](){}; // expected-error {{'operator int' cannot be the name of a variable or data member}} \
37*67e74705SXin Li                       // expected-error {{array bound cannot be deduced from an in-class initializer}}
38*67e74705SXin Li };
39*67e74705SXin Li 
40*67e74705SXin Li struct ThrowCtor { ThrowCtor(int) noexcept(false); };
41*67e74705SXin Li struct NoThrowCtor { NoThrowCtor(int) noexcept(true); };
42*67e74705SXin Li 
43*67e74705SXin Li struct Throw { ThrowCtor tc = 42; };
44*67e74705SXin Li struct NoThrow { NoThrowCtor tc = 42; };
45*67e74705SXin Li 
46*67e74705SXin Li static_assert(!noexcept(Throw()), "incorrect exception specification");
47*67e74705SXin Li static_assert(noexcept(NoThrow()), "incorrect exception specification");
48*67e74705SXin Li 
49*67e74705SXin Li struct CheckExcSpec {
50*67e74705SXin Li   CheckExcSpec() noexcept(true) = default;
51*67e74705SXin Li   int n = 0;
52*67e74705SXin Li };
53*67e74705SXin Li struct CheckExcSpecFail {
54*67e74705SXin Li   CheckExcSpecFail() noexcept(true) = default; // expected-error {{exception specification of explicitly defaulted default constructor does not match the calculated one}}
55*67e74705SXin Li   ThrowCtor tc = 123;
56*67e74705SXin Li };
57*67e74705SXin Li 
58*67e74705SXin Li struct TypedefInit {
59*67e74705SXin Li   typedef int A = 0; // expected-error {{illegal initializer}}
60*67e74705SXin Li };
61*67e74705SXin Li 
62*67e74705SXin Li // PR10578 / <rdar://problem/9877267>
63*67e74705SXin Li namespace PR10578 {
64*67e74705SXin Li   template<typename T>
65*67e74705SXin Li   struct X {
XPR10578::X66*67e74705SXin Li     X() {
67*67e74705SXin Li       T* x = 1; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}}
68*67e74705SXin Li     }
69*67e74705SXin Li   };
70*67e74705SXin Li 
71*67e74705SXin Li   struct Y : X<int> {
72*67e74705SXin Li     Y();
73*67e74705SXin Li   };
74*67e74705SXin Li 
Y()75*67e74705SXin Li   Y::Y() try { // expected-note{{in instantiation of member function 'PR10578::X<int>::X' requested here}}
76*67e74705SXin Li   } catch(...) {
77*67e74705SXin Li   }
78*67e74705SXin Li }
79*67e74705SXin Li 
80*67e74705SXin Li namespace PR14838 {
~basePR14838::base81*67e74705SXin Li   struct base { ~base() {} };
82*67e74705SXin Li   class function : base {
~function()83*67e74705SXin Li     ~function() {} // expected-note {{implicitly declared private here}}
84*67e74705SXin Li   public:
function(...)85*67e74705SXin Li     function(...) {}
86*67e74705SXin Li   };
87*67e74705SXin Li   struct thing {};
88*67e74705SXin Li   struct another {
anotherPR14838::another89*67e74705SXin Li     another() : r(thing()) {}
90*67e74705SXin Li     // expected-error@-1 {{temporary of type 'const PR14838::function' has private destructor}}
91*67e74705SXin Li     // expected-warning@-2 {{binding reference member 'r' to a temporary value}}
92*67e74705SXin Li     const function &r; // expected-note {{reference member declared here}}
93*67e74705SXin Li   } af;
94*67e74705SXin Li }
95*67e74705SXin Li 
96*67e74705SXin Li namespace rdar14084171 {
97*67e74705SXin Li   struct Point { // expected-note 3 {{candidate constructor}}
98*67e74705SXin Li     double x;
99*67e74705SXin Li     double y;
100*67e74705SXin Li   };
101*67e74705SXin Li   struct Sprite {
102*67e74705SXin Li     Point location = Point(0,0); // expected-error {{no matching constructor for initialization of 'rdar14084171::Point'}}
103*67e74705SXin Li   };
f(Sprite & x)104*67e74705SXin Li   void f(Sprite& x) { x = x; }
105*67e74705SXin Li }
106*67e74705SXin Li 
107*67e74705SXin Li namespace PR18560 {
108*67e74705SXin Li   struct X { int m; };
109*67e74705SXin Li 
110*67e74705SXin Li   template<typename T = X,
111*67e74705SXin Li            typename U = decltype(T::m)>
112*67e74705SXin Li   int f();
113*67e74705SXin Li 
114*67e74705SXin Li   struct Y { int b = f(); };
115*67e74705SXin Li }
116*67e74705SXin Li 
117*67e74705SXin Li namespace template_valid {
118*67e74705SXin Li // Valid, we shouldn't build a CXXDefaultInitExpr until A's ctor definition.
119*67e74705SXin Li struct A {
120*67e74705SXin Li   A();
121*67e74705SXin Li   template <typename T>
122*67e74705SXin Li   struct B { int m1 = sizeof(A) + sizeof(T); };
123*67e74705SXin Li   B<int> m2;
124*67e74705SXin Li };
A()125*67e74705SXin Li A::A() {}
126*67e74705SXin Li }
127*67e74705SXin Li 
128*67e74705SXin Li namespace template_default_ctor {
129*67e74705SXin Li struct A {
130*67e74705SXin Li   template <typename T>
131*67e74705SXin Li   struct B {
132*67e74705SXin Li     int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'B' within 'A' outside of member functions because 'm1' has an initializer}}
133*67e74705SXin Li   };
134*67e74705SXin Li   // expected-note@+1 {{implicit default constructor for 'template_default_ctor::A::B<int>' first required here}}
135*67e74705SXin Li   enum { NOE = noexcept(B<int>()) };
136*67e74705SXin Li };
137*67e74705SXin Li }
138*67e74705SXin Li 
139*67e74705SXin Li namespace default_ctor {
140*67e74705SXin Li struct A {
141*67e74705SXin Li   struct B {
142*67e74705SXin Li     int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'B' within 'A' outside of member functions because 'm1' has an initializer}}
143*67e74705SXin Li   };
144*67e74705SXin Li   // expected-note@+1 {{implicit default constructor for 'default_ctor::A::B' first required here}}
145*67e74705SXin Li   enum { NOE = noexcept(B()) };
146*67e74705SXin Li };
147*67e74705SXin Li }
148*67e74705SXin Li 
149*67e74705SXin Li namespace member_template {
150*67e74705SXin Li struct A {
151*67e74705SXin Li   template <typename T>
152*67e74705SXin Li   struct B {
153*67e74705SXin Li     struct C {
154*67e74705SXin Li       int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'C' within 'A' outside of member functions because 'm1' has an initializer}}
155*67e74705SXin Li     };
156*67e74705SXin Li     template <typename U>
157*67e74705SXin Li     struct D {
158*67e74705SXin Li       int m1 = 0; // expected-error {{cannot use defaulted default constructor of 'D' within 'A' outside of member functions because 'm1' has an initializer}}
159*67e74705SXin Li     };
160*67e74705SXin Li   };
161*67e74705SXin Li   enum {
162*67e74705SXin Li     // expected-note@+1 {{implicit default constructor for 'member_template::A::B<int>::C' first required here}}
163*67e74705SXin Li     NOE1 = noexcept(B<int>::C()),
164*67e74705SXin Li     // expected-note@+1 {{implicit default constructor for 'member_template::A::B<int>::D<int>' first required here}}
165*67e74705SXin Li     NOE2 = noexcept(B<int>::D<int>())
166*67e74705SXin Li   };
167*67e74705SXin Li };
168*67e74705SXin Li }
169*67e74705SXin Li 
170*67e74705SXin Li namespace explicit_instantiation {
171*67e74705SXin Li template<typename T> struct X {
172*67e74705SXin Li   X(); // expected-note {{in instantiation of default member initializer 'explicit_instantiation::X<float>::n' requested here}}
173*67e74705SXin Li   int n = T::error; // expected-error {{type 'float' cannot be used prior to '::' because it has no members}}
174*67e74705SXin Li };
175*67e74705SXin Li template struct X<int>; // ok
X()176*67e74705SXin Li template<typename T> X<T>::X() {}
177*67e74705SXin Li template struct X<float>; // expected-note {{in instantiation of member function 'explicit_instantiation::X<float>::X' requested here}}
178*67e74705SXin Li }
179*67e74705SXin Li 
180*67e74705SXin Li namespace local_class {
f()181*67e74705SXin Li template<typename T> void f() {
182*67e74705SXin Li   struct X { // expected-note {{in instantiation of default member initializer 'local_class::f()::X::n' requested here}}
183*67e74705SXin Li     int n = T::error; // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
184*67e74705SXin Li   };
185*67e74705SXin Li }
g()186*67e74705SXin Li void g() { f<int>(); } // expected-note {{in instantiation of function template specialization 'local_class::f<int>' requested here}}
187*67e74705SXin Li }
188*67e74705SXin Li 
189*67e74705SXin Li namespace PR22056 {
190*67e74705SXin Li template <int N>
191*67e74705SXin Li struct S {
192*67e74705SXin Li   int x[3] = {[N] = 3};
193*67e74705SXin Li };
194*67e74705SXin Li }
195*67e74705SXin Li 
196*67e74705SXin Li namespace PR28060 {
197*67e74705SXin Li template <class T>
foo(T v)198*67e74705SXin Li void foo(T v) {
199*67e74705SXin Li   struct s {
200*67e74705SXin Li     T *s = 0;
201*67e74705SXin Li   };
202*67e74705SXin Li }
203*67e74705SXin Li template void foo(int);
204*67e74705SXin Li }
205