• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
2 // RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
3 // FIXME: Remove the triple when PR27098 is fixed.
4 // RUN: %clang_cc1 -std=c++1z -fsyntax-only -verify %s -triple %itanium_abi_triple
5 
6 namespace std {
7   typedef decltype(sizeof(int)) size_t;
8 
9   template <typename E>
10   struct initializer_list
11   {
12     const E *p;
13     size_t n;
initializer_liststd::initializer_list14     initializer_list(const E *p, size_t n) : p(p), n(n) {}
15   };
16 
17   struct string {
18     string(const char *);
19   };
20 
21   template<typename A, typename B>
22   struct pair {
23     pair(const A&, const B&);
24   };
25 }
26 
27 namespace bullet1 {
28   double ad[] = { 1, 2.0 };
29   int ai[] = { 1, 2.0 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
30 
31   struct S2 {
32     int m1;
33     double m2, m3;
34   };
35 
36   S2 s21 = { 1, 2, 3.0 };
37   S2 s22 { 1.0, 2, 3 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
38   S2 s23 { };
39 }
40 
41 namespace bullet4_example1 {
42   struct S {
Sbullet4_example1::S43     S(std::initializer_list<double> d) {}
Sbullet4_example1::S44     S(std::initializer_list<int> i) {}
Sbullet4_example1::S45     S() {}
46   };
47 
48   S s1 = { 1.0, 2.0, 3.0 };
49   S s2 = { 1, 2, 3 };
50   S s3 = { };
51 }
52 
53 namespace bullet4_example2 {
54   struct Map {
Mapbullet4_example2::Map55     Map(std::initializer_list<std::pair<std::string,int>>) {}
56   };
57 
58   Map ship = {{"Sophie",14}, {"Surprise",28}};
59 }
60 
61 namespace bullet4_example3 {
62   struct S {
Sbullet4_example3::S63     S(int, double, double) {}
Sbullet4_example3::S64     S() {}
65   };
66 
67   S s1 = { 1, 2, 3.0 };
68   S s2 { 1.0, 2, 3 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
69   S s3 {};
70 }
71 
72 namespace bullet5 {
73   int x1 {2};
74   int x2 {2.0};  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
75 }
76 
77 namespace bullet6 {
78   struct S {
Sbullet6::S79     S(std::initializer_list<double>) {}
Sbullet6::S80     S(const std::string &) {}
81   };
82 
83   const S& r1 = { 1, 2, 3.0 };
84   const S& r2 = { "Spinach" };
85   S& r3 = { 1, 2, 3 };  // expected-error {{non-const lvalue reference to type 'bullet6::S' cannot bind to an initializer list temporary}}
86   const int& i1 = { 1 };
87   const int& i2 = { 1.1 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
88   const int (&iar)[2] = { 1, 2 };
89 }
90 
91 namespace bullet7 {
92   int** pp {};
93 }
94 
95 namespace bullet8 {
96   struct A { int i; int j; };
97   A a1 { 1, 2 };
98   A a2 { 1.2 };  // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
99 
100   struct B {
Bbullet8::B101     B(std::initializer_list<int> i) {}
102   };
103   B b1 { 1, 2 };
104   B b2 { 1, 2.0 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}}
105 
106   struct C {
Cbullet8::C107     C(int i, double j) {}
108   };
109   C c1 = { 1, 2.2 };
110   // FIXME: Suppress the narrowing warning in the cases where we issue a narrowing error.
111   C c2 = { 1.1, 2 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}}
112 
113   int j { 1 };
114   int k { };
115 }
116 
117 namespace rdar13395022 {
118   struct MoveOnly { // expected-note {{candidate}}
119     MoveOnly(MoveOnly&&); // expected-note 2{{copy constructor is implicitly deleted because}} expected-note {{candidate}}
120   };
121 
test(MoveOnly mo)122   void test(MoveOnly mo) {
123     auto &&list1 = {mo}; // expected-error {{call to implicitly-deleted copy constructor}} expected-note {{in initialization of temporary of type 'std::initializer_list}}
124     MoveOnly (&&list2)[1] = {mo}; // expected-error {{call to implicitly-deleted copy constructor}} expected-note {{in initialization of temporary of type 'rdar13395022::MoveOnly [1]'}}
125     std::initializer_list<MoveOnly> &&list3 = {};
126     MoveOnly (&&list4)[1] = {}; // expected-error {{no matching constructor}}
127     // expected-note@-1 {{in implicit initialization of array element 0 with omitted initializer}}
128     // expected-note@-2 {{in initialization of temporary of type 'rdar13395022::MoveOnly [1]' created to list-initialize this reference}}
129   }
130 }
131 
132 namespace cxx1z_direct_enum_init {
133   enum A {};
134   enum B : char {};
135   enum class C {};
136   enum class D : char {};
137   enum class E : char { k = 5 };
138 
good()139   template<typename T> void good() {
140     (void)T{0};
141     T t1{0};
142     T t2 = T{0};
143 
144     struct S { T t; };
145     S s{T{0}};
146 
147     struct U { T t{0}; } u; // expected-note 0+{{instantiation of}}
148 
149     struct V { T t; V() : t{0} {} }; // expected-note 0+{{instantiation of}}
150 
151     void f(T);
152     f(T{0});
153   }
154 #if __cplusplus <= 201402L
155   // expected-error@-15 5{{cannot initialize}}
156   // expected-error@-15 5{{cannot initialize}}
157   // expected-error@-15 5{{cannot initialize}}
158   //
159   //
160   // expected-error@-15 5{{cannot initialize}}
161   //
162   // expected-error@-15 5{{cannot initialize}}
163   //
164   // expected-error@-15 5{{cannot initialize}}
165   //
166   //
167   // expected-error@-15 5{{cannot initialize}}
168 #else
169   // expected-error@-29 {{cannot initialize}}
170   // expected-error@-29 {{cannot initialize}}
171   // expected-error@-29 {{cannot initialize}}
172   //
173   //
174   // expected-error@-29 {{cannot initialize}}
175   //
176   // expected-error@-29 {{cannot initialize}}
177   //
178   // expected-error@-29 {{cannot initialize}}
179   //
180   //
181   // expected-error@-29 {{cannot initialize}}
182 #endif
183 
bad()184   template<typename T> void bad() {
185     T t = {0};
186 
187     struct S { T t; };
188     S s1{0};
189     S s2{{0}};
190 
191     struct U { T t = {0}; } u; // expected-note 0+{{instantiation of}}
192 
193     struct V { T t; V() : t({0}) {} }; // expected-note 0+{{instantiation of}}
194 
195     void f(T); // expected-note 0+{{passing argument}}
196     f({0});
197   }
198   // expected-error@-13 5{{cannot initialize}}
199   //
200   //
201   // expected-error@-13 5{{cannot initialize}}
202   // expected-error@-13 5{{cannot initialize}}
203   //
204   // expected-error@-13 5{{cannot initialize}}
205   //
206   // expected-error@-13 5{{cannot initialize}}
207   //
208   //
209   // expected-error@-13 5{{cannot initialize}}
210 
ugly()211   template<typename T> void ugly() {
212     extern char c;
213     T t1{char('0' + c)};
214     T t2{'0' + c};
215     T t3{1234};
216   }
217 #if __cplusplus <= 201402L
218   // expected-error@-5 4{{cannot initialize}}
219   // expected-error@-5 4{{cannot initialize}}
220   // expected-error@-5 4{{cannot initialize}}
221 #else
222   // expected-error@-8 3{{non-constant-expression cannot be narrowed}}
223   // expected-error@-8 3{{constant expression evaluates to 1234 which cannot be narrowed}} expected-warning@-8 {{changes value}}
224 #endif
225 
test()226   void test() {
227     good<A>(); // expected-note 4{{instantiation of}}
228     good<B>();
229     good<C>();
230     good<D>();
231     good<E>();
232 #if __cplusplus <= 201402L
233     // expected-note@-5 4{{instantiation of}}
234     // expected-note@-5 4{{instantiation of}}
235     // expected-note@-5 4{{instantiation of}}
236     // expected-note@-5 4{{instantiation of}}
237 #endif
238 
239     bad<A>(); // expected-note 4{{instantiation of}}
240     bad<B>(); // expected-note 4{{instantiation of}}
241     bad<C>(); // expected-note 4{{instantiation of}}
242     bad<D>(); // expected-note 4{{instantiation of}}
243     bad<E>(); // expected-note 4{{instantiation of}}
244 
245     ugly<B>(); // expected-note {{instantiation of}}
246     ugly<C>(); // ok
247     ugly<D>(); // expected-note {{instantiation of}}
248     ugly<E>(); // expected-note {{instantiation of}}
249 #if __cplusplus <= 201402L
250     // expected-note@-4 {{instantiation of}}
251 #else
252     (void)B{0.0}; // expected-error {{type 'double' cannot be narrowed}}
253 #endif
254   }
255 }
256