From 394558e6a1329b791de69d0fc7c618eac0dbc982 Mon Sep 17 00:00:00 2001 From: Faisal Vali Date: Tue, 12 Nov 2013 03:48:27 +0000 Subject: [PATCH] A quick fix to PR17877 that was introduced by r194188 (generic-lambda-capturing) that broke libc++. See http://lists.cs.uiuc.edu/pipermail/cfe-dev/2013-November/033369.html for discussion on cfe-dev. This fix explicitly checks whether we are within the declcontext of a lambda's call operator - which is what I had intended to be true (and assumed would be true if getCurLambda returns a valid pointer) before checking whether a lambda can capture the potential-captures of the innermost lambda. A deeper fix (that addresses why getCurLambda() returns a valid pointer when perhaps it shouldn't?) - as proposed by Richard Smith in http://llvm.org/bugs/show_bug.cgi?id=17877 - has been suggested as a FIXME. Patch was LGTM'd by Richard (just barely :) http://llvm-reviews.chandlerc.com/D2144 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@194448 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaExprCXX.cpp | 21 ++++++++++++++++++--- test/SemaCXX/cxx1y-generic-lambdas.cpp | 23 ++++++++++++++++++++++- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/lib/Sema/SemaExprCXX.cpp b/lib/Sema/SemaExprCXX.cpp index 84220f7c9c..e27bfd8050 100644 --- a/lib/Sema/SemaExprCXX.cpp +++ b/lib/Sema/SemaExprCXX.cpp @@ -5988,9 +5988,24 @@ ExprResult Sema::ActOnFinishFullExpr(Expr *FE, SourceLocation CC, // full-expression +n + ({ 0; }); ends, but it's too late for us to see that // we need to capture n after all. - LambdaScopeInfo *const CurrentLSI = getCurLambda(); - if (CurrentLSI && CurrentLSI->hasPotentialCaptures() && - !FullExpr.isInvalid()) + LambdaScopeInfo *const CurrentLSI = getCurLambda(); + // FIXME: PR 17877 showed that getCurLambda() can return a valid pointer + // even if CurContext is not a lambda call operator. Refer to that Bug Report + // for an example of the code that might cause this asynchrony. + // By ensuring we are in the context of a lambda's call operator + // we can fix the bug (we only need to check whether we need to capture + // if we are within a lambda's body); but per the comments in that + // PR, a proper fix would entail : + // "Alternative suggestion: + // - Add to Sema an integer holding the smallest (outermost) scope + // index that we are *lexically* within, and save/restore/set to + // FunctionScopes.size() in InstantiatingTemplate's + // constructor/destructor. + // - Teach the handful of places that iterate over FunctionScopes to + // stop at the outermost enclosing lexical scope." + const bool IsInLambdaDeclContext = isLambdaCallOperator(CurContext); + if (IsInLambdaDeclContext && CurrentLSI && + CurrentLSI->hasPotentialCaptures() && !FullExpr.isInvalid()) CheckLambdaCaptures(FE, CurrentLSI, *this); return MaybeCreateExprWithCleanups(FullExpr); } diff --git a/test/SemaCXX/cxx1y-generic-lambdas.cpp b/test/SemaCXX/cxx1y-generic-lambdas.cpp index 4d0e27ec1c..a8518a3a40 100644 --- a/test/SemaCXX/cxx1y-generic-lambdas.cpp +++ b/test/SemaCXX/cxx1y-generic-lambdas.cpp @@ -824,4 +824,25 @@ void finalizeDefaultAtomValues() { void f() { finalizeDefaultAtomValues(); } -} \ No newline at end of file +} + +namespace PR17877_lambda_declcontext_and_get_cur_lambda_disconnect { + + +template struct U { + int t = 0; +}; + +template +struct V { + U size() const { return U{}; } +}; + +template +void Do() { + V v{}; + [=] { v.size(); }; +} + + +} \ No newline at end of file -- 2.40.0