p7-0x.cpp revision 6bcf27bb9a4b5c3f79cb44c0e4654a6d7619ad89
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
6void 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// Test each rule individually.
28
29template<typename T>
30struct Agg {
31  T t;
32};
33
34template<typename T>
35struct Convert {
36  constexpr Convert(T v) : v(v) {}
37  constexpr operator T() const { return v; }
38  T v;
39};
40template<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
46void float_to_int() {
47  Agg<char> a1 = {1.0F};  // expected-error {{type 'float' cannot be narrowed to 'char'}} expected-note {{silence}}
48  Agg<char> a2 = {1.0};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
49  Agg<char> a3 = {1.0L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
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 {{silence}}
55  Agg<char> a5 = {d};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
56  Agg<char> a6 = {ld};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
57
58  Agg<char> ce1 = { Convert<float>(1.0) }; // expected-error {{type 'float' cannot be narrowed to 'char'}} expected-note {{silence}}
59  Agg<char> ce2 = { ConvertVar<double>() }; // expected-error {{type 'double' cannot be narrowed to 'char'}} expected-note {{silence}}
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
67void 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 {{silence}}
76  Agg<float> f3 = {ld};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
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 {{silence}}
85  Agg<float> f9 = {1E50L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
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 {{silence}}
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 {{silence}}
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 {{silence}}
105  Agg<double> ce2 = { ConvertVar<long double>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long double' to 'double'}} expected-note {{silence}}
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
112void 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 {{silence}}
118  Agg<double> f2 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
119  Agg<long double> f3 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
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 {{silence}}
124
125  Agg<float> ce1 = { Convert<int>(123456789) }; // expected-error {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{silence}}
126  Agg<double> ce2 = { ConvertVar<long long>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{silence}}
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.
134void 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 {{silence}}
139  Agg<unsigned short> s1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
140  Agg<short> s2 = {us};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
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 {{silence}}
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 {{silence}} expected-warning {{changes value}}
154
155  Agg<int> i2 = {0x7FFFFFFFU};  // OK
156  Agg<int> i3 = {0x80000000U};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
157  Agg<unsigned int> i4 = {-0x80000000L};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
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 {{silence}}
164
165  // Conversions from pointers to booleans aren't narrowing conversions.
166  Agg<bool>* ptr = &b1;
167  Agg<bool> b = {ptr};  // OK
168
169  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}}
170  Agg<char> ce2 = { ConvertVar<short>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{silence}}
171
172  // Negative -> larger unsigned type.
173  unsigned long long ll1 = { -1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
174  unsigned long long ll2 = { 1 }; // OK
175  unsigned long long ll3 = { s }; // expected-error {{cannot be narrowed from type 'short'}} expected-note {{silence}}
176  unsigned long long ll4 = { us }; // OK
177  unsigned long long ll5 = { ll }; // expected-error {{cannot be narrowed from type 'long long'}} expected-note {{silence}}
178  Agg<unsigned long long> ll6 = { -1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
179  Agg<unsigned long long> ll7 = { 18446744073709551615ULL }; // OK
180  Agg<unsigned long long> ll8 = { __int128(18446744073709551615ULL) + 1 }; // expected-error {{ 18446744073709551616 which cannot be narrowed}} expected-note {{silence}} expected-warning {{changes value}}
181  signed char c = 'x';
182  unsigned short usc1 = { c }; // expected-error {{non-constant-expression cannot be narrowed from type 'signed char'}} expected-note {{silence}}
183  unsigned short usc2 = { (signed char)'x' }; // OK
184  unsigned short usc3 = { (signed char)-1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}}
185}
186
187// Be sure that type- and value-dependent expressions in templates get the error
188// too.
189
190template<int I, typename T>
191void maybe_shrink_int(T t) {
192  Agg<short> s1 = {t};  // expected-error {{ cannot be narrowed }} expected-note {{silence}}
193  Agg<short> s2 = {I};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
194  Agg<T> t2 = {700};  // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}}
195}
196
197void test_template() {
198  maybe_shrink_int<15>((int)3);  // expected-note {{in instantiation}}
199  maybe_shrink_int<70000>((char)3);  // expected-note {{in instantiation}}
200}
201
202
203// We don't want qualifiers on the types in the diagnostic.
204
205void test_qualifiers(int i) {
206  const int j = i;
207  struct {const unsigned char c;} c1 = {j};  // expected-error {{from type 'int' to 'unsigned char' in}} expected-note {{silence}}
208  // Template arguments make it harder to avoid printing qualifiers:
209  Agg<const unsigned char> c2 = {j};  // expected-error {{from type 'int' to 'const unsigned char' in}} expected-note {{silence}}
210}
211
212// Test SFINAE checks.
213template<unsigned> struct Value { };
214
215template<typename T>
216int &check_narrowed(Value<sizeof((T){1.1})>);
217
218template<typename T>
219float &check_narrowed(...);
220
221void test_narrowed(Value<sizeof(int)> vi, Value<sizeof(double)> vd) {
222  int &ir = check_narrowed<double>(vd);
223  float &fr = check_narrowed<int>(vi);
224}
225