PR11851 (and duplicates): Whenever a constexpr function is referenced,
instantiate it if it can be instantiated and implicitly define it if it can be
implicitly defined. This matches g++'s approach. Remove some cases from
SemaOverload which were marking functions as referenced when just planning how
overload resolution would proceed; such cases are not actually references.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@167514 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/test/SemaTemplate/constexpr-instantiate.cpp b/test/SemaTemplate/constexpr-instantiate.cpp
index 2f9fe0e..80c4aaf 100644
--- a/test/SemaTemplate/constexpr-instantiate.cpp
+++ b/test/SemaTemplate/constexpr-instantiate.cpp
@@ -75,3 +75,136 @@
   constexpr int n = const_cast<int&>(S<int>::r);
   static_assert(n == 5, "");
 }
+
+namespace Unevaluated {
+  // We follow g++ in treating any reference to a constexpr function template
+  // specialization as requiring an instantiation, even if it occurs in an
+  // unevaluated context.
+  //
+  // We go slightly further than g++, and also trigger the implicit definition
+  // of a defaulted special member in the same circumstances. This seems scary,
+  // since a lot of classes have constexpr special members in C++11, but the
+  // only observable impact should be the implicit instantiation of constexpr
+  // special member templates (defaulted special members should only be
+  // generated if they are well-formed, and non-constexpr special members in a
+  // base or member cause the class's special member to not be constexpr).
+  //
+  // FIXME: None of this is required by the C++ standard. The rules in this
+  //        area are poorly specified, so this is subject to change.
+  namespace NotConstexpr {
+    template<typename T> struct S {
+      S() : n(0) {}
+      S(const S&) : n(T::error) {}
+      int n;
+    };
+    struct U : S<int> {};
+    decltype(U(U())) u; // ok, don't instantiate S<int>::S() because it wasn't declared constexpr
+  }
+  namespace Constexpr {
+    template<typename T> struct S {
+      constexpr S() : n(0) {}
+      constexpr S(const S&) : n(T::error) {} // expected-error {{has no members}}
+      int n;
+    };
+    struct U : S<int> {}; // expected-note {{instantiation}}
+    decltype(U(U())) u; // expected-note {{here}}
+  }
+
+  namespace PR11851_Comment0 {
+    template<int x> constexpr int f() { return x; }
+    template<int i> void ovf(int (&x)[f<i>()]);
+    void f() { int x[10]; ovf<10>(x); }
+  }
+
+  namespace PR11851_Comment1 {
+    template<typename T>
+    constexpr bool Integral() {
+      return true;
+    }
+    template<typename T, bool Int = Integral<T>()>
+    struct safe_make_unsigned {
+      typedef T type;
+    };
+    template<typename T>
+    using Make_unsigned = typename safe_make_unsigned<T>::type;
+    template <typename T>
+    struct get_distance_type {
+      using type = int;
+    };
+    template<typename R>
+    auto size(R) -> Make_unsigned<typename get_distance_type<R>::type>;
+    auto check() -> decltype(size(0));
+  }
+
+  namespace PR11851_Comment6 {
+    template<int> struct foo {};
+    template<class> constexpr int bar() { return 0; }
+    template<class T> foo<bar<T>()> foobar();
+    auto foobar_ = foobar<int>();
+  }
+
+  namespace PR11851_Comment9 {
+    struct S1 {
+      constexpr S1() {}
+      constexpr operator int() const { return 0; }
+    };
+    int k1 = sizeof(short{S1(S1())});
+
+    struct S2 {
+      constexpr S2() {}
+      constexpr operator int() const { return 123456; }
+    };
+    int k2 = sizeof(short{S2(S2())}); // expected-error {{cannot be narrowed}} expected-note {{override}}
+  }
+
+  namespace PR12288 {
+    template <typename> constexpr bool foo() { return true; }
+    template <bool> struct bar {};
+    template <typename T> bar<foo<T>()> baz() { return bar<foo<T>()>(); }
+    int main() { baz<int>(); }
+  }
+
+  namespace PR13423 {
+    template<bool, typename> struct enable_if {};
+    template<typename T> struct enable_if<true, T> { using type = T; };
+
+    template<typename T> struct F {
+      template<typename U>
+      static constexpr bool f() { return sizeof(T) < U::size; }
+
+      template<typename U>
+      static typename enable_if<f<U>(), void>::type g() {} // expected-note {{disabled by 'enable_if'}}
+    };
+
+    struct U { static constexpr int size = 2; };
+
+    void h() { F<char>::g<U>(); }
+    void i() { F<int>::g<U>(); } // expected-error {{no matching function}}
+  }
+
+  namespace PR14203 {
+    struct duration { constexpr duration() {} };
+
+    template <typename>
+    void sleep_for() {
+      constexpr duration max = duration();
+    }
+  }
+}
+
+namespace NoInstantiationWhenSelectingOverload {
+  // Check that we don't instantiate conversion functions when we're checking
+  // for the existence of an implicit conversion sequence, only when a function
+  // is actually chosen by overload resolution.
+  struct S {
+    template<typename T> constexpr S(T) : n(T::error) {} // expected-error {{no members}}
+    int n;
+  };
+
+  void f(S);
+  void f(int);
+
+  void g() { f(0); }
+  void h() { (void)sizeof(f(0)); }
+  void i() { (void)sizeof(f("oops")); } // expected-note {{instantiation of}}
+}