r267956 - [Sema] Fix a crash that occurs when a variable template is initialized

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 28 16:50:13 PDT 2016


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>();
+}




More information about the cfe-commits mailing list