| 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 |
|
| 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 { |
| 43 |
constexpr Convert(T v) : v(v) {} |
| 44 |
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 |
|
| 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 |
|
| 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 |
| 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 |
Agg<float> f10 = {2147483646}; // expected-error {{constant expression evaluates to 2147483646 which cannot be narrowed to type 'float'}} expected-note {{silence}} |
| 141 |
Agg<float> f11 = {2147483647}; // expected-error {{constant expression evaluates to 2147483647 which cannot be narrowed to type 'float'}} expected-note {{silence}} |
| 142 |
|
| 143 |
Agg<float> ce1 = { Convert<int>(123456789) }; // expected-error {{constant expression evaluates to 123456789 which cannot be narrowed to type 'float'}} expected-note {{silence}} |
| 144 |
Agg<double> ce2 = { ConvertVar<long long>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'long long' to 'double'}} expected-note {{silence}} |
| 145 |
} |
| 146 |
|
| 147 |
// * from an integer type or unscoped enumeration type to an integer type that |
| 148 |
// cannot represent all the values of the original type, except where the |
| 149 |
// source is a constant expression and the actual value after conversion will |
| 150 |
// fit into the target type and will produce the original value when converted |
| 151 |
// back to the original type. |
| 152 |
void shrink_int() { |
| 153 |
// Not a constant expression. |
| 154 |
short s = 1; |
| 155 |
UnscopedEnum e = EnumVal; |
| 156 |
unsigned short us = 1; |
| 157 |
Agg<char> c1 = {s}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 158 |
Agg<char> c2 = {e}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 159 |
Agg<unsigned short> s1 = {s}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 160 |
Agg<short> s2 = {us}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 161 |
|
| 162 |
// "that cannot represent all the values of the original type" means that the |
| 163 |
// validity of the program depends on the relative sizes of integral types. |
| 164 |
// This test compiles with -m64, so sizeof(int)<sizeof(long)==sizeof(long |
| 165 |
// long). |
| 166 |
long l1 = 1; |
| 167 |
Agg<int> i1 = {l1}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 168 |
Agg<int> i2 = {e}; // OK |
| 169 |
long long ll = 1; |
| 170 |
Agg<long> l2 = {ll}; // OK |
| 171 |
|
| 172 |
// Constants. |
| 173 |
Agg<char> c3 = {127}; // OK |
| 174 |
Agg<char> c4 = {300}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}} |
| 175 |
Agg<char> c5 = {EnumVal}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}} |
| 176 |
|
| 177 |
Agg<int> i3 = {0x7FFFFFFFU}; // OK |
| 178 |
Agg<int> i4 = {EnumVal}; // OK |
| 179 |
Agg<int> i5 = {0x80000000U}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 180 |
Agg<unsigned int> i6 = {-0x80000000L}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 181 |
|
| 182 |
// Bool is also an integer type, but conversions to it are a different AST |
| 183 |
// node. |
| 184 |
Agg<bool> b1 = {0}; // OK |
| 185 |
Agg<bool> b2 = {1}; // OK |
| 186 |
Agg<bool> b3 = {-1}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 187 |
|
| 188 |
// Conversions from pointers to booleans are narrowing conversions. |
| 189 |
Agg<bool>* ptr = &b1; |
| 190 |
Agg<bool> b = {ptr}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 191 |
|
| 192 |
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}} |
| 193 |
Agg<char> ce2 = { ConvertVar<short>() }; // expected-error {{non-constant-expression cannot be narrowed from type 'short' to 'char'}} expected-note {{silence}} |
| 194 |
|
| 195 |
// Negative -> larger unsigned type. |
| 196 |
unsigned long long ll1 = { -1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}} |
| 197 |
unsigned long long ll2 = { 1 }; // OK |
| 198 |
unsigned long long ll3 = { s }; // expected-error {{cannot be narrowed from type 'short'}} expected-note {{silence}} |
| 199 |
unsigned long long ll4 = { us }; // OK |
| 200 |
unsigned long long ll5 = { ll }; // expected-error {{cannot be narrowed from type 'long long'}} expected-note {{silence}} |
| 201 |
Agg<unsigned long long> ll6 = { -1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}} |
| 202 |
Agg<unsigned long long> ll7 = { 18446744073709551615ULL }; // OK |
| 203 |
Agg<unsigned long long> ll8 = { __int128(18446744073709551615ULL) + 1 }; // expected-error {{ 18446744073709551616 which cannot be narrowed}} expected-note {{silence}} expected-warning {{changes value}} |
| 204 |
signed char c = 'x'; |
| 205 |
unsigned short usc1 = { c }; // expected-error {{non-constant-expression cannot be narrowed from type 'signed char'}} expected-note {{silence}} |
| 206 |
unsigned short usc2 = { (signed char)'x' }; // OK |
| 207 |
unsigned short usc3 = { (signed char)-1 }; // expected-error {{ -1 which cannot be narrowed}} expected-note {{silence}} |
| 208 |
} |
| 209 |
|
| 210 |
// Be sure that type- and value-dependent expressions in templates get the error |
| 211 |
// too. |
| 212 |
|
| 213 |
template<int I, typename T> |
| 214 |
void maybe_shrink_int(T t) { |
| 215 |
Agg<short> s1 = {t}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} |
| 216 |
Agg<short> s2 = {I}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}} |
| 217 |
Agg<T> t2 = {700}; // expected-error {{ cannot be narrowed }} expected-note {{silence}} expected-warning {{changes value}} |
| 218 |
} |
| 219 |
|
| 220 |
void test_template() { |
| 221 |
maybe_shrink_int<15>((int)3); // expected-note {{in instantiation}} |
| 222 |
maybe_shrink_int<70000>((char)3); // expected-note {{in instantiation}} |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
// We don't want qualifiers on the types in the diagnostic. |
| 227 |
|
| 228 |
void test_qualifiers(int i) { |
| 229 |
const int j = i; |
| 230 |
struct {const unsigned char c;} c1 = {j}; // expected-error {{from type 'int' to 'unsigned char' in}} expected-note {{silence}} |
| 231 |
// Template arguments make it harder to avoid printing qualifiers: |
| 232 |
Agg<const unsigned char> c2 = {j}; // expected-error {{from type 'int' to 'const unsigned char' in}} expected-note {{silence}} |
| 233 |
} |
| 234 |
|
| 235 |
// Test SFINAE checks. |
| 236 |
template<unsigned> struct Value { }; |
| 237 |
|
| 238 |
template<typename T> |
| 239 |
int &check_narrowed(Value<sizeof((T){1.1})>); |
| 240 |
|
| 241 |
template<typename T> |
| 242 |
float &check_narrowed(...); |
| 243 |
|
| 244 |
void test_narrowed(Value<sizeof(int)> vi, Value<sizeof(double)> vd) { |
| 245 |
int &ir = check_narrowed<double>(vd); |
| 246 |
float &fr = check_narrowed<int>(vi); |
| 247 |
} |
| 248 |
|
| 249 |
// * from a pointer type or a pointer-to-member type to bool. |
| 250 |
void P1957R2(void *a, int *b, Agg<int> *c, int Agg<int>::*d) { |
| 251 |
Agg<bool> ta = {a}; // expected-error {{cannot be narrowed}} expected-note {{}} |
| 252 |
Agg<bool> tb = {b}; // expected-error {{cannot be narrowed}} expected-note {{}} |
| 253 |
Agg<bool> tc = {c}; // expected-error {{cannot be narrowed}} expected-note {{}} |
| 254 |
Agg<bool> td = {d}; // expected-error {{cannot be narrowed}} expected-note {{}} |
| 255 |
} |
| 256 |
template<bool> struct BoolParam {}; |
| 257 |
BoolParam<&P1957R2> bp; // expected-error {{not allowed in a converted constant expression}} |