diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4840,7 +4840,8 @@ /*Complain*/DefinitionRequired)) { if (DefinitionRequired) Function->setInvalidDecl(); - else if (TSK == TSK_ExplicitInstantiationDefinition) { + else if (TSK == TSK_ExplicitInstantiationDefinition || + (Function->isConstexpr() && !Recursive)) { // Try again at the end of the translation unit (at which point a // definition will be required). assert(!Recursive); @@ -4855,7 +4856,7 @@ Diag(PatternDecl->getLocation(), diag::note_forward_template_decl); if (getLangOpts().CPlusPlus11) Diag(PointOfInstantiation, diag::note_inst_declaration_hint) - << Function; + << Function; } } diff --git a/clang/test/CodeGenCXX/constexpr-late-instantiation.cpp b/clang/test/CodeGenCXX/constexpr-late-instantiation.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CodeGenCXX/constexpr-late-instantiation.cpp @@ -0,0 +1,17 @@ +// Make sure foo is instantiated and we don't get a link error +// RUN: %clang_cc1 -S -emit-llvm %s -o- | FileCheck %s + +template +constexpr T foo(T a); + +// CHECK-LABEL: define {{.*}} @main +int main() { + // CHECK: call {{.*}} @_Z3fooIiET_S0_ + int k = foo(5); +} + +// CHECK-LABEL: define {{.*}} @_Z3fooIiET_S0_ +template +constexpr T foo(T a) { + return a; +} diff --git a/clang/test/SemaCXX/constexpr-late-instantiation.cpp b/clang/test/SemaCXX/constexpr-late-instantiation.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/constexpr-late-instantiation.cpp @@ -0,0 +1,15 @@ +// Make sure foo is instantiated and we don't get a link error +// RUN: not %clang_cc1 -S -emit-llvm %s -fsyntax-only 2>&1 | FileCheck %s + +template +constexpr T foo(T a); + +int main() { + int k = foo(5); // Ok + constexpr int j = foo(5); // CHECK: error: constexpr variable 'j' must be initialized by a constant expression +} + +template +constexpr T foo(T a) { + return a; +}