1 // RUN: %clang_cc1 -fsyntax-only -std=c++11 -triple x86_64-apple-macosx10.6.7 -verify %s
2 
3 // Verify that narrowing conversions in initializer lists cause errors in C++0x
4 // mode.
5 
std_example()6 void std_example() {
7   int x = 999;  // x is not a constant expression
8   const int y = 999;
9   const int z = 99;
10   char c1 = x;  // OK, though it might narrow (in this case, it does narrow)
11   char c2{x};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
12   char c3{y};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
13   char c4{z};  // OK: no narrowing needed
14   unsigned char uc1 = {5};  // OK: no narrowing needed
15   unsigned char uc2 = {-1};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
16   unsigned int ui1 = {-1};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
17   signed int si1 =
18     { (unsigned int)-1 };  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
19   int ii = {2.0};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
20   float f1 { x };  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
21   float f2 { 7 };  // OK: 7 can be exactly represented as a float
22   int f(int);
23   int a[] =
24     { 2, f(2), f(2.0) };  // OK: the double-to-int conversion is not at the top level
25 }
26 
27 enum UnscopedEnum {
28   EnumVal = 300
29 };
30 
31 // Test each rule individually.
32 
33 template<typename T>
34 struct Agg {
35   T t;
36 };
37 
38 template<typename T>
39 struct Convert {
ConvertConvert40   constexpr Convert(T v) : v(v) {}
operator TConvert41   constexpr operator T() const { return v; }
42   T v;
43 };
44 template<typename T> Convert<T> ConvertVar();
45 
46 // C++0x [dcl.init.list]p7: A narrowing conversion is an implicit conversion
47 //
48 // * from a floating-point type to an integer type, or
49 
float_to_int()50 void float_to_int() {
51   Agg<char> a1 = {1.0F};  // expected-error {{type 'float' cannot be narrowed to 'char'}} expected-note {{silence}}
52   Agg<char> a2 = {1.0};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
53   Agg<char> a3 = {1.0L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
54 
55   float f = 1.0;
56   double d = 1.0;
57   long double ld = 1.0;
58   Agg<char> a4 = {f};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
59   Agg<char> a5 = {d};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
60   Agg<char> a6 = {ld};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
61 
62   Agg<char> ce1 = { Convert<float>(1.0) }; // expected-error {{type 'float' cannot be narrowed to 'char'}} expected-note {{silence}}
63   Agg<char> ce2 = { ConvertVar<double>() }; // expected-error {{type 'double' cannot be narrowed to 'char'}} expected-note {{silence}}
64 
65   bool b{1.0}; // expected-error {{type 'double' cannot be narrowed to 'bool'}} expected-note {{silence}}
66   Agg<bool> ab = {0.0}; // expected-error {{type 'double' cannot be narrowed to 'bool'}} expected-note {{silence}}
67 }
68 
69 // * from long double to double or float, or from double to float, except where
70 //   the source is a constant expression and the actual value after conversion
71 //   is within the range of values that can be represented (even if it cannot be
72 //   represented exactly), or
73 
shrink_float()74 void shrink_float() {
75   // These aren't constant expressions.
76   float f = 1.0;
77   double d = 1.0;
78   long double ld = 1.0;
79 
80   // Variables.
81   Agg<float> f1 = {f};  // OK  (no-op)
82   Agg<float> f2 = {d};  // expected-error {{non-constant-expression cannot be narrowed from type 'double' to 'float'}} expected-note {{silence}}
83   Agg<float> f3 = {ld};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
84   // Exact constants.
85   Agg<float> f4 = {1.0};  // OK  (double constant represented exactly)
86   Agg<float> f5 = {1.0L};  // OK  (long double constant represented exactly)
87   // Inexact but in-range constants.
88   Agg<float> f6 = {0.1};  // OK (double constant in range but rounded)
89   Agg<float> f7 = {0.1L};  // OK (long double constant in range but rounded)
90   // Out of range constants.
91   Agg<float> f8 = {1E50};  // expected-error {{constant expression evaluates to 1.000000e+50 which cannot be narrowed to type 'float'}} expected-note {{silence}}
92   Agg<float> f9 = {1E50L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
93   // More complex constant expression.
94   constexpr long double e40 = 1E40L, e30 = 1E30L, e39 = 1E39L;
95   Agg<float> f10 = {e40 - 5 * e39 + e30 - 5 * e39};  // OK
96 
97   // Variables.
98   Agg<double> d1 = {f};  // OK  (widening)
99   Agg<double> d2 = {d};  // OK  (no-op)
100   Agg<double> d3 = {ld};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
101   // Exact constant.
102   Agg<double> d4 = {1.0L};  // OK  (long double constant represented exactly)
103   // Inexact but in-range constant.
104   Agg<double> d5 = {0.1L};  // OK (long double constant in range but rounded)
105   // Out of range constant.
106   Agg<double> d6 = {1E315L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
107   // More complex constant expression.
108   constexpr long double e315 = 1E315L, e305 = 1E305L, e314 = 1E314L;
109   Agg<double> d7 = {e315 - 5 * e314 + e305 - 5 * e314};  // OK
110 
111   Agg<float> ce1 = { Convert<double>(1e300) }; // expected-error {{constant expression evaluates to 1.000000e+300 which cannot be narrowed to type 'float'}} expected-note {{silence}}
112   Agg<double> ce2 = { ConvertVar<long double>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long double' to 'double'}} expected-note {{silence}}
113 }
114 
115 // * from an integer type or unscoped enumeration type to a floating-point type,
116 //   except where the source is a constant expression and the actual value after
117 //   conversion will fit into the target type and will produce the original
118 //   value when converted back to the original type, or
int_to_float()119 void int_to_float() {
120   // Not a constant expression.
121   char c = 1;
122   UnscopedEnum e = EnumVal;
123 
124   // Variables.  Yes, even though all char's will fit into any floating type.
125   Agg<float> f1 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
126   Agg<double> f2 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
127   Agg<long double> f3 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
128 
129   Agg<float> f4 = {e};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
130   Agg<double> f5 = {e};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
131   Agg<long double> f6 = {e};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
132 
133   // Constants.
134   Agg<float> f7 = {12345678};  // OK (exactly fits in a float)
135   Agg<float> f8 = {EnumVal};  // OK
136   Agg<float> f9 = {123456789};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
137 
138   Agg<float> ce1 = { Convert<int>(123456789) }; // expected-error {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{silence}}
139   Agg<double> ce2 = { ConvertVar<long long>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{silence}}
140 }
141 
142 // * from an integer type or unscoped enumeration type to an integer type that
143 //   cannot represent all the values of the original type, except where the
144 //   source is a constant expression and the actual value after conversion will
145 //   fit into the target type and will produce the original value when converted
146 //   back to the original type.
shrink_int()147 void shrink_int() {
148   // Not a constant expression.
149   short s = 1;
150   UnscopedEnum e = EnumVal;
151   unsigned short us = 1;
152   Agg<char> c1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
153   Agg<char> c2 = {e};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
154   Agg<unsigned short> s1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
155   Agg<short> s2 = {us};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
156 
157   // "that cannot represent all the values of the original type" means that the
158   // validity of the program depends on the relative sizes of integral types.
159   // This test compiles with -m64, so sizeof(int)<sizeof(long)==sizeof(long
160   // long).
161   long l1 = 1;
162   Agg<int> i1 = {l1};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
163   Agg<int> i2 = {e};  // OK
164   long long ll = 1;
165   Agg<long> l2 = {ll};  // OK
166 
167   // Constants.
168   Agg<char> c3 = {127};  // OK
169   Agg<char> c4 = {300};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
170   Agg<char> c5 = {EnumVal};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
171 
172   Agg<int> i3 = {0x7FFFFFFFU};  // OK
173   Agg<int> i4 = {EnumVal};  // OK
174   Agg<int> i5 = {0x80000000U};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
175   Agg<unsigned int> i6 = {-0x80000000L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
176 
177   // Bool is also an integer type, but conversions to it are a different AST
178   // node.
179   Agg<bool> b1 = {0};  // OK
180   Agg<bool> b2 = {1};  // OK
181   Agg<bool> b3 = {-1};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
182 
183   // Conversions from pointers to booleans are narrowing conversions.
184   Agg<bool>* ptr = &b1;
185   Agg<bool> b = {ptr};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
186 
187   Agg<short> ce1 = { Convert<int>(100000) }; // expected-error {{constant expression evaluates to 100000 which cannot be narrowed to type 'short'}} expected-note {{silence}} expected-warning {{changes value from 100000 to -31072}}
188   Agg<char> ce2 = { ConvertVar<short>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{silence}}
189 
190   // Negative -> larger unsigned type.
191   unsigned long long ll1 = { -1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
192   unsigned long long ll2 = { 1 }; // OK
193   unsigned long long ll3 = { s }; // expected-error {{cannot be narrowed from type 'short'}} expected-note {{silence}}
194   unsigned long long ll4 = { us }; // OK
195   unsigned long long ll5 = { ll }; // expected-error {{cannot be narrowed from type 'long long'}} expected-note {{silence}}
196   Agg<unsigned long long> ll6 = { -1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
197   Agg<unsigned long long> ll7 = { 18446744073709551615ULL }; // OK
198   Agg<unsigned long long> ll8 = { __int128(18446744073709551615ULL) + 1 }; // expected-error {{ 18446744073709551616 which cannot be narrowed}} expected-note {{silence}} expected-warning {{changes value}}
199   signed char c = 'x';
200   unsigned short usc1 = { c }; // expected-error {{non-constant-expression cannot be narrowed from type 'signed char'}} expected-note {{silence}}
201   unsigned short usc2 = { (signed char)'x' }; // OK
202   unsigned short usc3 = { (signed char)-1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
203 }
204 
205 // Be sure that type- and value-dependent expressions in templates get the error
206 // too.
207 
208 template<int I, typename T>
maybe_shrink_int(T t)209 void maybe_shrink_int(T t) {
210   Agg<short> s1 = {t};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
211   Agg<short> s2 = {I};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
212   Agg<T> t2 = {700};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
213 }
214 
test_template()215 void test_template() {
216   maybe_shrink_int<15>((int)3);  // expected-note {{in instantiation}}
217   maybe_shrink_int<70000>((char)3);  // expected-note {{in instantiation}}
218 }
219 
220 
221 // We don't want qualifiers on the types in the diagnostic.
222 
test_qualifiers(int i)223 void test_qualifiers(int i) {
224   const int j = i;
225   struct {const unsigned char c;} c1 = {j};  // expected-error {{from type 'int' to 'unsigned char' in}} expected-note {{silence}}
226   // Template arguments make it harder to avoid printing qualifiers:
227   Agg<const unsigned char> c2 = {j};  // expected-error {{from type 'int' to 'const unsigned char' in}} expected-note {{silence}}
228 }
229 
230 // Test SFINAE checks.
231 template<unsigned> struct Value { };
232 
233 template<typename T>
234 int &check_narrowed(Value<sizeof((T){1.1})>);
235 
236 template<typename T>
237 float &check_narrowed(...);
238 
test_narrowed(Value<sizeof (int)> vi,Value<sizeof (double)> vd)239 void test_narrowed(Value<sizeof(int)> vi, Value<sizeof(double)> vd) {
240   int &ir = check_narrowed<double>(vd);
241   float &fr = check_narrowed<int>(vi);
242 }
243 
244 // * from a pointer type or a pointer-to-member type to bool.
P1957R2(void * a,int * b,Agg<int> * c,int Agg<int>::* d)245 void P1957R2(void *a, int *b, Agg<int> *c, int Agg<int>::*d) {
246   Agg<bool> ta = {a}; // expected-error {{cannot be narrowed}} expected-note {{}}
247   Agg<bool> tb = {b}; // expected-error {{cannot be narrowed}} expected-note {{}}
248   Agg<bool> tc = {c}; // expected-error {{cannot be narrowed}} expected-note {{}}
249   Agg<bool> td = {d}; // expected-error {{cannot be narrowed}} expected-note {{}}
250 }
251 template<bool> struct BoolParam {};
252 BoolParam<&P1957R2> bp; // expected-error {{not allowed in a converted constant expression}}
253