diff options
author | Akira Hatanaka <ahatanaka@apple.com> | 2016-04-28 23:50:12 +0000 |
---|---|---|
committer | Akira Hatanaka <ahatanaka@apple.com> | 2016-04-28 23:50:12 +0000 |
commit | b87faffdb9bc86c4c08224bdbaa37096edcfda4d (patch) | |
tree | 453120acc476efff63e5f9e3a717c38e730ab1b5 | |
parent | 1573b242aeef45124c68a22a995fbc4bff2cc868 (diff) | |
download | bcm5719-llvm-b87faffdb9bc86c4c08224bdbaa37096edcfda4d.tar.gz bcm5719-llvm-b87faffdb9bc86c4c08224bdbaa37096edcfda4d.zip |
[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
llvm-svn: 267956
-rw-r--r-- | clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | 11 | ||||
-rw-r--r-- | clang/test/SemaCXX/vartemplate-lambda.cpp | 15 |
2 files changed, 23 insertions, 3 deletions
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index 8e42be0ee2e..66b274f6260 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3907,9 +3907,14 @@ void Sema::InstantiateVariableInitializer( 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(); diff --git a/clang/test/SemaCXX/vartemplate-lambda.cpp b/clang/test/SemaCXX/vartemplate-lambda.cpp new file mode 100644 index 00000000000..d5ad155d0e3 --- /dev/null +++ b/clang/test/SemaCXX/vartemplate-lambda.cpp @@ -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>(); +} |