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

Faisal Vali faisalv at gmail.com
Sat Nov 2 13:30:03 PDT 2013


On Sat, Nov 2, 2013 at 1:51 PM, Richard Smith <richard at metafoo.co.uk> wrote:

>
> 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.
>
>
yes this is ill-formed only because the outer lambda does not have a
default-capture (or explicitly captured 'x').
With the default-capture in the enclosing lambda, 'x' would be odr used,
and so the outer lambda would have
to capture it.  Unless i am missing something?



> >        };
> >     }
> >
> > 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?
>
>
Yes - the outer lambda would need to capture 'x' - and the code as written
would result in an error.



> > 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/208f8aae/attachment.html>


More information about the cfe-commits mailing list