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