Home | History | Annotate | Download | only in dcl.init.list
      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 {{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 // C++0x [dcl.init.list]p7: A narrowing conversion is an implicit conversion
     35 //
     36 // * from a floating-point type to an integer type, or
     37 
     38 void float_to_int() {
     39   Agg<char> a1 = {1.0F};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     40   Agg<char> a2 = {1.0};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     41   Agg<char> a3 = {1.0L};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     42 
     43   float f = 1.0;
     44   double d = 1.0;
     45   long double ld = 1.0;
     46   Agg<char> a4 = {f};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     47   Agg<char> a5 = {d};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     48   Agg<char> a6 = {ld};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     49 }
     50 
     51 // * from long double to double or float, or from double to float, except where
     52 //   the source is a constant expression and the actual value after conversion
     53 //   is within the range of values that can be represented (even if it cannot be
     54 //   represented exactly), or
     55 
     56 void shrink_float() {
     57   // These aren't constant expressions.
     58   float f = 1.0;
     59   double d = 1.0;
     60   long double ld = 1.0;
     61 
     62   // Variables.
     63   Agg<float> f1 = {f};  // OK  (no-op)
     64   Agg<float> f2 = {d};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     65   Agg<float> f3 = {ld};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     66   // Exact constants.
     67   Agg<float> f4 = {1.0};  // OK  (double constant represented exactly)
     68   Agg<float> f5 = {1.0L};  // OK  (long double constant represented exactly)
     69   // Inexact but in-range constants.
     70   Agg<float> f6 = {0.1};  // OK (double constant in range but rounded)
     71   Agg<float> f7 = {0.1L};  // OK (long double constant in range but rounded)
     72   // Out of range constants.
     73   Agg<float> f8 = {1E50};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     74   Agg<float> f9 = {1E50L};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     75   // More complex constant expression.
     76   constexpr long double e40 = 1E40L, e30 = 1E30L, e39 = 1E39L;
     77   Agg<float> f10 = {e40 - 5 * e39 + e30 - 5 * e39};  // OK
     78 
     79   // Variables.
     80   Agg<double> d1 = {f};  // OK  (widening)
     81   Agg<double> d2 = {d};  // OK  (no-op)
     82   Agg<double> d3 = {ld};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     83   // Exact constant.
     84   Agg<double> d4 = {1.0L};  // OK  (long double constant represented exactly)
     85   // Inexact but in-range constant.
     86   Agg<double> d5 = {0.1L};  // OK (long double constant in range but rounded)
     87   // Out of range constant.
     88   Agg<double> d6 = {1E315L};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
     89   // More complex constant expression.
     90   constexpr long double e315 = 1E315L, e305 = 1E305L, e314 = 1E314L;
     91   Agg<double> d7 = {e315 - 5 * e314 + e305 - 5 * e314};  // OK
     92 }
     93 
     94 // * from an integer type or unscoped enumeration type to a floating-point type,
     95 //   except where the source is a constant expression and the actual value after
     96 //   conversion will fit into the target type and will produce the original
     97 //   value when converted back to the original type, or
     98 void int_to_float() {
     99   // Not a constant expression.
    100   char c = 1;
    101 
    102   // Variables.  Yes, even though all char's will fit into any floating type.
    103   Agg<float> f1 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    104   Agg<double> f2 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    105   Agg<long double> f3 = {c};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    106 
    107   // Constants.
    108   Agg<float> f4 = {12345678};  // OK (exactly fits in a float)
    109   Agg<float> f5 = {123456789};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    110 }
    111 
    112 // * from an integer type or unscoped enumeration type to an integer type that
    113 //   cannot represent all the values of the original type, except where the
    114 //   source is a constant expression and the actual value after conversion will
    115 //   fit into the target type and will produce the original value when converted
    116 //   back to the original type.
    117 void shrink_int() {
    118   // Not a constant expression.
    119   short s = 1;
    120   unsigned short us = 1;
    121   Agg<char> c1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    122   Agg<unsigned short> s1 = {s};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    123   Agg<short> s2 = {us};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    124 
    125   // "that cannot represent all the values of the original type" means that the
    126   // validity of the program depends on the relative sizes of integral types.
    127   // This test compiles with -m64, so sizeof(int)<sizeof(long)==sizeof(long
    128   // long).
    129   long l1 = 1;
    130   Agg<int> i1 = {l1};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    131   long long ll = 1;
    132   Agg<long> l2 = {ll};  // OK
    133 
    134   // Constants.
    135   Agg<char> c2 = {127};  // OK
    136   Agg<char> c3 = {300};  // expected-error {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}}
    137 
    138   Agg<int> i2 = {0x7FFFFFFFU};  // OK
    139   Agg<int> i3 = {0x80000000U};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    140   Agg<unsigned int> i4 = {-0x80000000L};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    141 
    142   // Bool is also an integer type, but conversions to it are a different AST
    143   // node.
    144   Agg<bool> b1 = {0};  // OK
    145   Agg<bool> b2 = {1};  // OK
    146   Agg<bool> b3 = {-1};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    147 
    148   // Conversions from pointers to booleans aren't narrowing conversions.
    149   Agg<bool> b = {&b1};  // OK
    150 }
    151 
    152 // Be sure that type- and value-dependent expressions in templates get the error
    153 // too.
    154 
    155 template<int I, typename T>
    156 void maybe_shrink_int(T t) {
    157   Agg<short> s1 = {t};  // expected-error {{ cannot be narrowed }} expected-note {{override}}
    158   Agg<short> s2 = {I};  // expected-error {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}}
    159   Agg<T> t2 = {700};  // expected-error {{ cannot be narrowed }} expected-note {{override}} expected-warning {{changes value}}
    160 }
    161 
    162 void test_template() {
    163   maybe_shrink_int<15>((int)3);  // expected-note {{in instantiation}}
    164   maybe_shrink_int<70000>((char)3);  // expected-note {{in instantiation}}
    165 }
    166 
    167 
    168 // We don't want qualifiers on the types in the diagnostic.
    169 
    170 void test_qualifiers(int i) {
    171   const int j = i;
    172   struct {const unsigned char c;} c1 = {j};  // expected-error {{from type 'int' to 'unsigned char' in}} expected-note {{override}}
    173   // Template arguments make it harder to avoid printing qualifiers:
    174   Agg<const unsigned char> c2 = {j};  // expected-error {{from type 'int' to 'const unsigned char' in}} expected-note {{override}}
    175 }
    176