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