<div dir="ltr">I'll revert this as it makes the following snippet makes clang crash:<div><br></div><div><div>class SomeClass {</div><div> public:</div><div>  void foo() {</div><div>    auto l = [this] { auto l = [] EXCLUSIVE_LOCKS_REQUIRED(mu_) {}; };</div><div>  }</div><div>  Mutex mu_;</div><div>};</div></div><div><br></div><div>(e.g. this can be added to test/SemaCXX/warn-thread-safety-parsing.cpp)</div><div>From a brief look at the backtrace, it seems like CurLSI->Lambda->getDeclContext() fails.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 28, 2017 at 8:49 PM, Faisal Vali via cfe-commits <span dir="ltr"><<a href="mailto:cfe-commits@lists.llvm.org" target="_blank">cfe-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: faisalv<br>
Date: Fri Apr 28 22:49:17 2017<br>
New Revision: 301735<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=301735&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=301735&view=rev</a><br>
Log:<br>
Fix PR32831: 'this capture while instantiating generic lambda call operator specialization<br>
<br>
When computing the appropriate cv-qualifiers for the 'this' capture, we have to examine each enclosing lambda - but when using the FunctionScopeInfo stack we have to ensure that the lambda below (outer) is the decl-context of the closure-class of the current lambda.<br>
<br>
<a href="https://bugs.llvm.org/show_bug.cgi?id=32831" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_<wbr>bug.cgi?id=32831</a><br>
<br>
Modified:<br>
    cfe/trunk/lib/Sema/<wbr>SemaExprCXX.cpp<br>
    cfe/trunk/test/SemaCXX/cxx1z-<wbr>lambda-star-this.cpp<br>
<br>
Modified: cfe/trunk/lib/Sema/<wbr>SemaExprCXX.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=301735&r1=301734&r2=301735&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/lib/Sema/<wbr>SemaExprCXX.cpp?rev=301735&r1=<wbr>301734&r2=301735&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/lib/Sema/<wbr>SemaExprCXX.cpp (original)<br>
+++ cfe/trunk/lib/Sema/<wbr>SemaExprCXX.cpp Fri Apr 28 22:49:17 2017<br>
@@ -901,17 +901,35 @@ static QualType adjustCVQualifiersForCXX<br>
   // capturing lamdbda's call operator.<br>
   //<br>
<br>
-  // The issue is that we cannot rely entirely on the FunctionScopeInfo stack<br>
-  // since ScopeInfos are pushed on during parsing and treetransforming. But<br>
-  // since a generic lambda's call operator can be instantiated anywhere (even<br>
-  // end of the TU) we need to be able to examine its enclosing lambdas and so<br>
-  // we use the DeclContext to get a hold of the closure-class and query it for<br>
-  // capture information.  The reason we don't just resort to always using the<br>
-  // DeclContext chain is that it is only mature for lambda expressions<br>
-  // enclosing generic lambda's call operators that are being instantiated.<br>
+  // Since the FunctionScopeInfo stack is representative of the lexical<br>
+  // nesting of the lambda expressions during initial parsing (and is the best<br>
+  // place for querying information about captures about lambdas that are<br>
+  // partially processed) and perhaps during instantiation of function templates<br>
+  // that contain lambda expressions that need to be transformed BUT not<br>
+  // necessarily during instantiation of a nested generic lambda's function call<br>
+  // operator (which might even be instantiated at the end of the TU) - at which<br>
+  // time the DeclContext tree is mature enough to query capture information<br>
+  // reliably - we use a two pronged approach to walk through all the lexically<br>
+  // enclosing lambda expressions:<br>
+  //<br>
+  //  1) Climb down the FunctionScopeInfo stack as long as each item represents<br>
+  //  a Lambda (i.e. LambdaScopeInfo) AND each LSI's 'closure-type' is lexically<br>
+  //  enclosed by the call-operator of the LSI below it on the stack (while<br>
+  //  tracking the enclosing DC for step 2 if needed).  Note the topmost LSI on<br>
+  //  the stack represents the innermost lambda.<br>
+  //<br>
+  //  2) Iterate out through the DeclContext chain (if it represents a lambda's<br>
+  //  call operator, and therefore must be a generic lambda's call operator,<br>
+  //  which is the only time an inconsistency between the LSI and the<br>
+  //  DeclContext should occur) querying closure types regarding capture<br>
+  //  information.<br>
<br>
+<br>
+  // 1) Climb down the function scope info stack.<br>
   for (int I = FunctionScopes.size();<br>
-       I-- && isa<LambdaScopeInfo>(<wbr>FunctionScopes[I]);<br>
+       I-- && isa<LambdaScopeInfo>(<wbr>FunctionScopes[I]) &&<br>
+       (!CurLSI || CurLSI->Lambda-><wbr>getDeclContext() ==<br>
+                       cast<LambdaScopeInfo>(<wbr>FunctionScopes[I])-><wbr>CallOperator);<br>
        CurDC = getLambdaAwareParentOfDeclCont<wbr>ext(CurDC)) {<br>
     CurLSI = cast<LambdaScopeInfo>(<wbr>FunctionScopes[I]);<br>
<br>
@@ -927,11 +945,17 @@ static QualType adjustCVQualifiersForCXX<br>
       return ASTCtx.getPointerType(<wbr>ClassType);<br>
     }<br>
   }<br>
-  // We've run out of ScopeInfos but check if CurDC is a lambda (which can<br>
-  // happen during instantiation of generic lambdas)<br>
+<br>
+  // 2) We've run out of ScopeInfos but check if CurDC is a lambda (which can<br>
+  // happen during instantiation of its nested generic lambda call operator)<br>
   if (isLambdaCallOperator(CurDC)) {<br>
-    assert(CurLSI);<br>
-    assert(<wbr>isGenericLambdaCallOperatorSpe<wbr>cialization(CurLSI-><wbr>CallOperator));<br>
+    assert(CurLSI && "While computing 'this' capture-type for a generic "<br>
+                     "lambda, we must have a corresponding LambdaScopeInfo");<br>
+    assert(<wbr>isGenericLambdaCallOperatorSpe<wbr>cialization(CurLSI-><wbr>CallOperator) &&<br>
+           "While computing 'this' capture-type for a generic lambda, when we "<br>
+           "run out of enclosing LSI's, yet the enclosing DC is a "<br>
+           "lambda-call-operator we must be (i.e. Current LSI) in a generic "<br>
+           "lambda call oeprator");<br>
     assert(CurDC == getLambdaAwareParentOfDeclCont<wbr>ext(CurLSI->CallOperator));<br>
<br>
     auto IsThisCaptured =<br>
<br>
Modified: cfe/trunk/test/SemaCXX/cxx1z-<wbr>lambda-star-this.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/cxx1z-lambda-star-this.cpp?rev=301735&r1=301734&r2=301735&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/cfe/trunk/test/<wbr>SemaCXX/cxx1z-lambda-star-<wbr>this.cpp?rev=301735&r1=301734&<wbr>r2=301735&view=diff</a><br>
==============================<wbr>==============================<wbr>==================<br>
--- cfe/trunk/test/SemaCXX/cxx1z-<wbr>lambda-star-this.cpp (original)<br>
+++ cfe/trunk/test/SemaCXX/cxx1z-<wbr>lambda-star-this.cpp Fri Apr 28 22:49:17 2017<br>
@@ -229,3 +229,69 @@ int main() {<br>
<br>
 } //end ns test_star_this<br>
<br>
+namespace PR32831 {<br>
+// <a href="https://bugs.llvm.org/show_bug.cgi?id=32831" rel="noreferrer" target="_blank">https://bugs.llvm.org/show_<wbr>bug.cgi?id=32831</a><br>
+namespace ns1 {<br>
+template <typename Func> void fun_template(Func func) {<br>
+  (void) [&]() {<br>
+    func(0);<br>
+  };<br>
+}<br>
+<br>
+class A {<br>
+  void member_foo() {<br>
+    (void) [this] {<br>
+      (void) [this] {<br>
+        fun_template(<br>
+            [this](auto X) {<br>
+              auto L = [this](auto Y)<br>
+              { member_foo(); };<br>
+              L(5);<br>
+            }<br>
+        );<br>
+        fun_template(<br>
+            [this](auto) { member_foo(); });<br>
+<br>
+      };<br>
+    };<br>
+  }<br>
+};<br>
+} // end ns1<br>
+<br>
+namespace ns2 {<br>
+<br>
+struct B {<br>
+  int data = 0;<br>
+  template<class F><br>
+  void mem2(F f) {<br>
+    (void) [&] (auto f) {<br>
+      (void) [&] { f(this->data); };<br>
+    }(f);<br>
+  }<br>
+<br>
+};<br>
+<br>
+class A {<br>
+  void member_foo() {<br>
+    (void) [this] {<br>
+      (void) [this] {<br>
+        B{}.mem2(<br>
+            [this](auto X) {<br>
+              auto L = [this](auto Y)<br>
+              { member_foo(); };<br>
+              L(5);<br>
+            }<br>
+        );<br>
+        B{}.mem2(<br>
+            [this](auto) { member_foo(); });<br>
+<br>
+      };<br>
+    };<br>
+  }<br>
+};<br>
+<br>
+<br>
+} // end ns2<br>
+<br>
+}<br>
+<br>
<br>
<br>
______________________________<wbr>_________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/cfe-commits</a><br>
</blockquote></div><br></div>