[PATCH] Tune capturing of init-captures in non-generic lambdas

Richard Smith richard at metafoo.co.uk
Sat Nov 2 11:51:29 PDT 2013


On 2 Nov 2013 10:33, "Faisal Vali" <faisalv at yahoo.com> wrote:
>
> Hi rsmith, doug.gregor,
>
>
>
> Currently clang does not seem to get capturing of init-captures correctly.
>
> Consider the following code:
>     void test() {
>        const int x = 10;
>        const int y = 12;
>        auto L = [&k = x, j = x](int a) {
>            return [&l = x, m = y] (int b) { };

This is ill formed, right? You can't odr-use x in the init of l.

>        };
>     }
>
> The outer lambda needs to capture 'x' but not 'y'.  And the inner lambda
should not capture either x or y.

Neither should capture either, because there is no capture default. Should
the outer lambda have a default of & in your example?

> This patch fixes this by:
>   - moving up the checking of the init capture before its lambda call
operator's declcontext, scope, scopeinfo are pushed on (so that
lvalue-to-rvalue conversions can be checked for const variables in the
init-expr)
>   - using the declcontext of the enclosing scope temporarily when the
init-variable is created, and then fixing it once its call-operator has
been created.  (this does require fixing the linkage computation of
init-vars to return none, so that it isn't perceived as ever being declared
at namespace scope).
>
> Tests added - and regressions tests all seem to pass.
>
> Certain aspects of this patch might emanate a bad smell - lets see if
anyone else feels the same ;)
>
> Thanks!
>
> http://llvm-reviews.chandlerc.com/D2092
>
> Files:
>   include/clang/AST/Decl.h
>   include/clang/Sema/DeclSpec.h
>   include/clang/Sema/Sema.h
>   lib/AST/Decl.cpp
>   lib/Parse/ParseExprCXX.cpp
>   lib/Sema/Sema.cpp
>   lib/Sema/SemaDecl.cpp
>   lib/Sema/SemaLambda.cpp
>   lib/Sema/SemaTemplateInstantiate.cpp
>   lib/Sema/SemaTemplateVariadic.cpp
>   lib/Sema/TreeTransform.h
>   test/SemaCXX/cxx1y-init-captures.cpp
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20131102/feafba44/attachment.html>


More information about the cfe-commits mailing list