Author: ahatanak
Date: Thu Apr 28 18:50:12 2016
New Revision: 267956

URL: http://llvm.org/viewvc/llvm-project?rev=267956&view=rev
Log:
[Sema] Fix a crash that occurs when a variable template is initialized
with a generic lambda.

This patch fixes Sema::InstantiateVariableInitializer to switch to the
context of the variable before instantiating its initializer, which is
necessary to set the correct type for VarTemplateSpecializationDecl.

This is the first part of the patch that was reviewed here:
http://reviews.llvm.org/D19175

rdar://problem/23440346

Added:
    cfe/trunk/test/SemaCXX/vartemplate-lambda.cpp
Modified:
    cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp?rev=267956&r1=267955&r2=267956&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplateInstantiateDecl.cpp Thu Apr 28 18:50:12 2016
@@ -3907,9 +3907,14 @@ void Sema::InstantiateVariableInitialize
       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();

Added: cfe/trunk/test/SemaCXX/vartemplate-lambda.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/vartemplate-lambda.cpp?rev=267956&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/vartemplate-lambda.cpp (added)
+++ cfe/trunk/test/SemaCXX/vartemplate-lambda.cpp Thu Apr 28 18:50:12 2016
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -std=c++14 -fsyntax-only -verify %s
+// expected-no-diagnostics
+
+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>();
+}


_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to