ahatanak updated this revision to Diff 54088. ahatanak added a comment. Address Richard's review comments and add a test case.
The test case currently asserts when fn0 is instantiated. This doesn't happen if I give the template parameter a name. template <class N> auto fn0 = [] {}; instead of template <class> auto fn0 = [] {}; I can prevent the assert if I make changes in Sema::ActOnTypeParameter to call S->AddDecl(Param) regardless of whether Param has a name. I'm not sure this is the right fix at the moment. http://reviews.llvm.org/D19175 Files: lib/Sema/SemaTemplateInstantiateDecl.cpp test/SemaCXX/vartemplate-lambda.cpp Index: test/SemaCXX/vartemplate-lambda.cpp =================================================================== --- /dev/null +++ test/SemaCXX/vartemplate-lambda.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s +// expected-no-diagnostics + +template <class> auto fn0 = [] {}; +template <typename> void foo0() { fn0<char>(); } + +template<typename T> auto fn1 = [](auto a) { return a + T(1); }; + +template <typename X> +int foo2() { + X a = 0x61; + fn1<char>(a); + return 0; +} + +int main() { + foo2<int>(); +} Index: lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- lib/Sema/SemaTemplateInstantiateDecl.cpp +++ lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3896,9 +3896,14 @@ PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar); // Instantiate the initializer. - ExprResult Init = - SubstInitializer(OldVar->getInit(), TemplateArgs, - OldVar->getInitStyle() == VarDecl::CallInit); + ExprResult Init; + + { + ContextRAII SwitchContext(*this, Var->getDeclContext()); + Init = SubstInitializer(OldVar->getInit(), TemplateArgs, + OldVar->getInitStyle() == VarDecl::CallInit); + } + if (!Init.isInvalid()) { bool TypeMayContainAuto = true; Expr *InitExpr = Init.get();
Index: test/SemaCXX/vartemplate-lambda.cpp =================================================================== --- /dev/null +++ test/SemaCXX/vartemplate-lambda.cpp @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s +// expected-no-diagnostics + +template <class> auto fn0 = [] {}; +template <typename> void foo0() { fn0<char>(); } + +template<typename T> auto fn1 = [](auto a) { return a + T(1); }; + +template <typename X> +int foo2() { + X a = 0x61; + fn1<char>(a); + return 0; +} + +int main() { + foo2<int>(); +} Index: lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- lib/Sema/SemaTemplateInstantiateDecl.cpp +++ lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3896,9 +3896,14 @@ PushExpressionEvaluationContext(Sema::PotentiallyEvaluated, OldVar); // Instantiate the initializer. - ExprResult Init = - SubstInitializer(OldVar->getInit(), TemplateArgs, - OldVar->getInitStyle() == VarDecl::CallInit); + ExprResult Init; + + { + ContextRAII SwitchContext(*this, Var->getDeclContext()); + Init = SubstInitializer(OldVar->getInit(), TemplateArgs, + OldVar->getInitStyle() == VarDecl::CallInit); + } + if (!Init.isInvalid()) { bool TypeMayContainAuto = true; Expr *InitExpr = Init.get();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits