From bf99a507c66e756ab713b1eaffbfcd7224e375da Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Wed, 29 Apr 2015 00:07:09 +0000 Subject: [PATCH] PR20625: Instantiate static constexpr member function of a local struct in a function template earlier. This is necessary in order to allow the use of a constexpr member function, or a member function with deduced return type, of a local class within a surrounding instantiated function template specialization. Patch by Michael Park! git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@236063 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaTemplateInstantiateDecl.cpp | 8 ++++++++ test/SemaTemplate/instantiate-local-class.cpp | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/Sema/SemaTemplateInstantiateDecl.cpp b/lib/Sema/SemaTemplateInstantiateDecl.cpp index 8f2e95a615..57faa1cbad 100644 --- a/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -1302,11 +1302,19 @@ Decl *TemplateDeclInstantiator::VisitCXXRecordDecl(CXXRecordDecl *D) { // DR1484 clarifies that the members of a local class are instantiated as part // of the instantiation of their enclosing entity. if (D->isCompleteDefinition() && D->isLocalClass()) { + Sema::SavePendingLocalImplicitInstantiationsRAII + SavedPendingLocalImplicitInstantiations(SemaRef); + SemaRef.InstantiateClass(D->getLocation(), Record, D, TemplateArgs, TSK_ImplicitInstantiation, /*Complain=*/true); + SemaRef.InstantiateClassMembers(D->getLocation(), Record, TemplateArgs, TSK_ImplicitInstantiation); + + // This class may have local implicit instantiations that need to be + // performed within this scope. + SemaRef.PerformPendingInstantiations(/*LocalOnly=*/true); } SemaRef.DiagnoseUnusedNestedTypedefs(Record); diff --git a/test/SemaTemplate/instantiate-local-class.cpp b/test/SemaTemplate/instantiate-local-class.cpp index 367134a2a5..668fb6fa65 100644 --- a/test/SemaTemplate/instantiate-local-class.cpp +++ b/test/SemaTemplate/instantiate-local-class.cpp @@ -213,3 +213,16 @@ namespace PR23194 { return make_seed_pair(); } } + +namespace PR20625 { +template +void f() { + struct N { + static constexpr int get() { return 42; } + }; + constexpr int n = N::get(); + static_assert(n == 42, "n == 42"); +} + +void g() { f(); } +} -- 2.40.0