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