]> granicus.if.org Git - clang/commitdiff
Fix PR32638 : Make sure we switch Sema's CurContext to the substituted FunctionDecl...
authorFaisal Vali <faisalv@yahoo.com>
Tue, 9 May 2017 04:17:15 +0000 (04:17 +0000)
committerFaisal Vali <faisalv@yahoo.com>
Tue, 9 May 2017 04:17:15 +0000 (04:17 +0000)
This fixes the bug: https://bugs.llvm.org/show_bug.cgi?id=32638

  int main()
  {
    [](auto x) noexcept(noexcept(x)) { } (0);
  }

In the above code, prior to this patch, when substituting into the noexcept expression, i.e. transforming the DeclRefExpr that represents 'x' - clang attempts to capture 'x' because Sema's CurContext is still pointing to the pattern FunctionDecl (i.e. the templated-decl set in FinishTemplateArgumentDeduction) which does not match the substituted 'x's DeclContext, which leads to an attempt to capture and an assertion failure.

We fix this by adjusting Sema's CurContext to point to the substituted FunctionDecl under which the noexcept specifier's argument should be transformed, and so the ParmVarDecl that 'x' refers to has the same declcontext and no capture is attempted.

I briefly investigated whether the SwitchContext should occur right after VisitMethodDecl creates the new substituted FunctionDecl, instead of only during instantiating the exception specification - but seeing no other code that seemed to rely on that, I decided to leave it just for the duration of the exception specification instantiation.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@302507 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaTemplateInstantiateDecl.cpp
test/SemaCXX/cxx1y-generic-lambdas.cpp

index 9a71a17561c7c08ddf56844da97f76ca6ac04175..226bd6055c628ab53db3cfd752655f0cadcbc884 100644 (file)
@@ -3660,6 +3660,7 @@ TemplateDeclInstantiator::InitFunctionInstantiation(FunctionDecl *New,
       New->setType(SemaRef.Context.getFunctionType(
           NewProto->getReturnType(), NewProto->getParamTypes(), EPI));
     } else {
+      Sema::ContextRAII SwitchContext(SemaRef, New);
       SemaRef.SubstExceptionSpec(New, Proto, TemplateArgs);
     }
   }
index 1993c6e1853dd06679f18dc40db31802d8412da9..9f3c77591a8611eaeff9d09cf02cfde745b33f41 100644 (file)
@@ -986,3 +986,10 @@ class Enclosing3 {
   );
 };
 }
+
+namespace PR32638 {
+ //https://bugs.llvm.org/show_bug.cgi?id=32638
+ void test() {
+    [](auto x) noexcept(noexcept(x)) { } (0);
+ }
+}
\ No newline at end of file