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