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