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 // We interpret "class type with a default constructor" as including the case 91 // where a default constructor is inherited. 92 struct X { 93 X(); 94 X(std::initializer_list<int>) = delete; 95 }; 96 struct Y : X { 97 using X::X; 98 Y(int); 99 }; 100 Y y1{}; use()101 void use() { Y y; } 102 Y y2{}; 103 } 104 105 namespace bullet7 { 106 int** pp {}; 107 } 108 109 namespace bullet8 { 110 struct A { int i; int j; }; 111 A a1 { 1, 2 }; 112 A a2 { 1.2 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}} 113 114 struct B { Bbullet8::B115 B(std::initializer_list<int> i) {} 116 }; 117 B b1 { 1, 2 }; 118 B b2 { 1, 2.0 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} 119 120 struct C { Cbullet8::C121 C(int i, double j) {} 122 }; 123 C c1 = { 1, 2.2 }; 124 // FIXME: Suppress the narrowing warning in the cases where we issue a narrowing error. 125 C c2 = { 1.1, 2 }; // expected-error {{type 'double' cannot be narrowed to 'int' in initializer list}} expected-note {{silence}} expected-warning {{implicit conversion}} 126 127 int j { 1 }; 128 int k { }; 129 } 130 131 namespace rdar13395022 { 132 struct MoveOnly { // expected-note {{candidate}} 133 MoveOnly(MoveOnly&&); // expected-note 2{{copy constructor is implicitly deleted because}} expected-note {{candidate}} 134 }; 135 test(MoveOnly mo)136 void test(MoveOnly mo) { 137 auto &&list1 = {mo}; // expected-error {{call to implicitly-deleted copy constructor}} expected-note {{in initialization of temporary of type 'std::initializer_list}} 138 MoveOnly (&&list2)[1] = {mo}; // expected-error {{call to implicitly-deleted copy constructor}} expected-note {{in initialization of temporary of type 'rdar13395022::MoveOnly [1]'}} 139 std::initializer_list<MoveOnly> &&list3 = {}; 140 MoveOnly (&&list4)[1] = {}; // expected-error {{no matching constructor}} 141 // expected-note@-1 {{in implicit initialization of array element 0 with omitted initializer}} 142 // expected-note@-2 {{in initialization of temporary of type 'rdar13395022::MoveOnly [1]' created to list-initialize this reference}} 143 } 144 } 145 146 namespace cxx1z_direct_enum_init { 147 enum A {}; 148 enum B : char {}; 149 enum class C {}; 150 enum class D : char {}; 151 enum class E : char { k = 5 }; 152 good()153 template<typename T> void good() { 154 (void)T{0}; 155 T t1{0}; 156 T t2 = T{0}; 157 158 struct S { T t; }; 159 S s{T{0}}; 160 161 struct U { T t{0}; } u; // expected-note 0+{{instantiation of}} 162 163 struct V { T t; V() : t{0} {} }; // expected-note 0+{{instantiation of}} 164 165 void f(T); 166 f(T{0}); 167 168 char c; 169 auto t3 = T{c}; 170 } 171 #if __cplusplus <= 201402L 172 // expected-error@-18 5{{cannot initialize}} 173 // expected-error@-18 5{{cannot initialize}} 174 // expected-error@-18 5{{cannot initialize}} 175 // 176 // 177 // expected-error@-18 5{{cannot initialize}} 178 // 179 // expected-error@-18 5{{cannot initialize}} 180 // 181 // expected-error@-18 5{{cannot initialize}} 182 // 183 // 184 // expected-error@-18 5{{cannot initialize}} 185 // 186 // 187 // expected-error@-18 5{{cannot initialize}} 188 #else 189 // expected-error@-35 {{cannot initialize}} 190 // expected-error@-35 {{cannot initialize}} 191 // expected-error@-35 {{cannot initialize}} 192 // 193 // 194 // expected-error@-35 {{cannot initialize}} 195 // 196 // expected-error@-35 {{cannot initialize}} 197 // 198 // expected-error@-35 {{cannot initialize}} 199 // 200 // 201 // expected-error@-35 {{cannot initialize}} 202 // 203 // 204 // expected-error@-35 {{cannot initialize}} 205 #endif 206 bad()207 template<typename T> void bad() { 208 T t = {0}; 209 210 struct S { T t; }; 211 S s1{0}; 212 S s2{{0}}; 213 214 struct U { T t = {0}; } u; // expected-note 0+{{instantiation of}} 215 216 struct V { T t; V() : t({0}) {} }; // expected-note 0+{{instantiation of}} 217 218 void f(T); // expected-note 0+{{passing argument}} 219 f({0}); 220 } 221 // expected-error@-13 5{{cannot initialize}} 222 // 223 // 224 // expected-error@-13 5{{cannot initialize}} 225 // expected-error@-13 5{{cannot initialize}} 226 // 227 // expected-error@-13 5{{cannot initialize}} 228 // 229 // expected-error@-13 5{{cannot initialize}} 230 // 231 // 232 // expected-error@-13 5{{cannot initialize}} 233 ugly()234 template<typename T> void ugly() { 235 extern char c; 236 T t1{char('0' + c)}; 237 T t2{'0' + c}; 238 T t3{1234}; 239 } 240 #if __cplusplus <= 201402L 241 // expected-error@-5 4{{cannot initialize}} 242 // expected-error@-5 4{{cannot initialize}} 243 // expected-error@-5 4{{cannot initialize}} 244 #else 245 // expected-error@-8 3{{non-constant-expression cannot be narrowed}} 246 // expected-error@-8 3{{constant expression evaluates to 1234 which cannot be narrowed}} expected-warning@-8 {{changes value}} 247 #endif 248 test()249 void test() { 250 good<A>(); // expected-note 4{{instantiation of}} 251 good<B>(); 252 good<C>(); 253 good<D>(); 254 good<E>(); 255 #if __cplusplus <= 201402L 256 // expected-note@-5 4{{instantiation of}} 257 // expected-note@-5 4{{instantiation of}} 258 // expected-note@-5 4{{instantiation of}} 259 // expected-note@-5 4{{instantiation of}} 260 #endif 261 262 bad<A>(); // expected-note 4{{instantiation of}} 263 bad<B>(); // expected-note 4{{instantiation of}} 264 bad<C>(); // expected-note 4{{instantiation of}} 265 bad<D>(); // expected-note 4{{instantiation of}} 266 bad<E>(); // expected-note 4{{instantiation of}} 267 268 ugly<B>(); // expected-note {{instantiation of}} 269 ugly<C>(); // ok 270 ugly<D>(); // expected-note {{instantiation of}} 271 ugly<E>(); // expected-note {{instantiation of}} 272 #if __cplusplus <= 201402L 273 // expected-note@-4 {{instantiation of}} 274 #else 275 (void)B{0.0}; // expected-error {{type 'double' cannot be narrowed}} 276 #endif 277 } 278 279 #if __cplusplus > 201402L 280 enum class F : unsigned {}; f1(unsigned x)281 F f1(unsigned x) { return F{x}; } f2(const unsigned x)282 F f2(const unsigned x) { return F{x}; } f3(bool x)283 F f3(bool x) { return F{x}; } f4(const bool x)284 F f4(const bool x) { return F{x}; } 285 #endif 286 } 287