18ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s
28ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
38ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith// A converted constant expression of type T is a core constant expression,
48ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithint nonconst = 8; // expected-note 3 {{here}}
58ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithenum NonConstE : unsigned char { NCE = nonconst }; // expected-error {{enumerator value is not a constant expression}} expected-note {{read of non-const}}
68ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<int = nonconst> struct NonConstT {}; // expected-error {{non-type template argument is not a constant expression}} expected-note {{read of non-const}}
78ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithvoid NonConstF() {
88ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  switch (nonconst) {
98ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case nonconst: // expected-error {{case value is not a constant expression}} expected-note {{read of non-const}}
108ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith      break;
118ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  }
128ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  return;
138ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith}
148ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
158ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith// implicitly converted to a prvalue of type T, where the converted expression
168ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith// is a literal constant expression
178ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
188ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithbool a(int n) {
198ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  constexpr char vowels[] = "aeiou";
208ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  switch (n) {
218ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  case vowels[0]:
228ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  case vowels[1]:
238ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  case vowels[2]:
248ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  case vowels[3]:
258ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  case vowels[4]:
268ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    static_assert(!vowels[5], "unexpected number of vowels");
278ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    return true;
288ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  }
298ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  return false;
308ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith}
318ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
328ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith// and the implicit conversion sequence contains only
338ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith//
348ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith//  user-defined conversions,
358ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstruct S { constexpr operator int() const { return 5; } };
368ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithenum E : unsigned char { E5 = S(), E6, E10 = S() * 2, E1 = E5 / 5 };
378ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
388ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith//  lvalue-to-rvalue conversions,
398ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithconst E e10 = E10;
408ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<E> struct T {};
418ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard SmithT<e10> s10;
428ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
438ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith//  integral promotions, and
448ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithenum class EE { EE32 = ' ', EE65 = 'A', EE1 = (short)1, EE5 = E5 };
458ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
468ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith//  integral conversions other than narrowing conversions
478ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithint b(unsigned n) {
488ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  switch (n) {
498ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case E6:
508ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case EE::EE32: // expected-error {{not implicitly convertible}}
518ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case (int)EE::EE32:
528ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case 1000:
538ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case (long long)1e10: // expected-error {{case value evaluates to 10000000000, which cannot be narrowed to type 'unsigned int'}}
548ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    case -3: // expected-error {{case value evaluates to -3, which cannot be narrowed to type 'unsigned int'}}
558ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith      return n;
568ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  }
578ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  return 0;
588ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith}
598ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithenum class EEE : unsigned short {
608ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  a = E6,
618ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  b = EE::EE32, // expected-error {{not implicitly convertible}}
628ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  c = (int)EE::EE32,
638ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  d = 1000,
648ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  e = 123456, // expected-error {{enumerator value evaluates to 123456, which cannot be narrowed to type 'unsigned short'}}
658ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  f = -3 // expected-error {{enumerator value evaluates to -3, which cannot be narrowed to type 'unsigned short'}}
668ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith};
678ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<unsigned char> using A = int;
688ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithusing Int = A<E6>;
698ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithusing Int = A<EE::EE32>; // expected-error {{not implicitly convertible}}
708ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithusing Int = A<(int)EE::EE32>;
718ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithusing Int = A<200>;
728ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithusing Int = A<1000>; // expected-error {{template argument evaluates to 1000, which cannot be narrowed to type 'unsigned char'}}
738ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithusing Int = A<-3>; // expected-error {{template argument evaluates to -3, which cannot be narrowed to type 'unsigned char'}}
748ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
758ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith// Note, conversions from integral or unscoped enumeration types to bool are
768ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith// integral conversions as well as boolean conversions.
778ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<typename T, T v> struct Val { static constexpr T value = v; };
788ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(Val<bool, E1>::value == 1, ""); // ok
798ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(Val<bool, '\0'>::value == 0, ""); // ok
808ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(Val<bool, U'\1'>::value == 1, ""); // ok
818ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithstatic_assert(Val<bool, E5>::value == 1, ""); // expected-error {{5, which cannot be narrowed to type 'bool'}}
828ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
838ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith// (no other conversions are permitted)
848ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithusing Int = A<1.0>; // expected-error {{conversion from 'double' to 'unsigned char' is not allowed in a converted constant expression}}
858ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithenum B : bool {
868ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  True = &a, // expected-error {{conversion from 'bool (*)(int)' to 'bool' is not allowed in a converted constant expression}}
878ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  False = nullptr // expected-error {{conversion from 'nullptr_t' to 'bool' is not allowed in a converted constant expression}}
888ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith};
898ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithvoid c() {
908ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  // Note, promoted type of switch is 'int'.
918ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  switch (bool b = a(5)) { // expected-warning {{boolean value}}
928ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  case 0.0f: // expected-error {{conversion from 'float' to 'int' is not allowed in a converted constant expression}}
938ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    break;
948ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  }
958ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith}
968ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate<bool B> int f() { return B; }
978ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate int f<&S::operator int>(); // expected-error {{does not refer to a function template}}
988ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithtemplate int f<(bool)&S::operator int>();
998ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
1008ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithint n = Val<bool, &S::operator int>::value; // expected-error {{conversion from 'int (S::*)() const' to 'bool' is not allowed in a converted constant expression}}
1018ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith
1028ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smithnamespace NonConstLValue {
1038ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  struct S {
1048ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith    constexpr operator int() { return 10; }
1058ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  };
1068ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  S s; // not constexpr
1078ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  // Under the FDIS, this is not a converted constant expression.
1088ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  // Under the new proposed wording, it is.
1098ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith  enum E : char { e = s };
1108ef7b203332b0c8d65876a1f5e6d1db4e6f40e4bRichard Smith}
111