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