[PATCH] D19783: Fix cv-qualification of '*this' captures (and nasty bug PR27507 introduced by commit 263921 "Implement Lambda Capture of *this by Value as [=, *this]")

Faisal Vali via cfe-commits cfe-commits at lists.llvm.org
Sun May 1 20:22:58 PDT 2016


faisalv created this revision.
faisalv added reviewers: rsmith, hubert.reinterpretcast.
faisalv added subscribers: cfe-commits, gnzlbg.

The bug report by Gonzalo (https://llvm.org/bugs/show_bug.cgi?id=27507 -- which results in clang crashing when generic lambdas that capture 'this' are instantiated in contexts where the functionscopeinfo stack is not in a reliable state - yet getCurrentThisType expects it to be) - unearthed some additional bugs in regards to maintaining proper cv qualification through 'this' when performing by value captures of '*this'.

This patch attempts to correct those bugs and makes the following changes:
  - when capturing 'this', we do not need to remember the type of 'this' within the LambdaScopeInfo's Capture - it is never really used for a this capture - so remove it.
  - teach getCurrentThisType to walk the stack of lambdas (even in scenarios where we run out of LambdaScopeInfo's such as when instantiating call operators) looking
    for by copy captures of '*this' and resetting the type of 'this' based on the constness of that capturing lambda's call operator.

As an example, consider the member function 'foo' below and follow along with the static_asserts:

void foo() const { 
    
    auto L = [*this] () mutable { 
      static_assert(is_same<decltype(this), X*>);
      ++d;
      auto M = [this] { 
        static_assert(is_same<decltype(this), X*>);  
        ++d;
        auto N = [] {
          static_assert(is_same<decltype(this), X*>); 
        };
      };
    };
    
    auto L1 = [*this] { 
      static_assert(is_same<decltype(this), const X*>);
      auto M = [this] () mutable { 
        static_assert(is_same<decltype(this), const X*>);  
        auto N = [] {
          static_assert(is_same<decltype(this), const X*>); 
        };
      };
    };
    auto L2 = [this] () mutable {
      static_assert(is_same<decltype(this), const X*>);  
    };
    auto GL = [*this] (auto a) mutable {
      static_assert(is_same<decltype(this), X*>);
      ++d;
      auto M = [this] (auto b) { 
        static_assert(is_same<decltype(this), X*>);  
        ++d;
        auto N = [] (auto c) {
          static_assert(is_same<decltype(this), X*>); 
        };
        N(3.14);
      };
      M("abc");
    };
    GL(3.14);
  }

Please see the test file for additional tests.

Thanks!

http://reviews.llvm.org/D19783

Files:
  include/clang/Sema/ScopeInfo.h
  lib/Sema/SemaDecl.cpp
  lib/Sema/SemaExprCXX.cpp
  test/SemaCXX/cxx1z-lambda-star-this.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D19783.55769.patch
Type: text/x-patch
Size: 14138 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160502/c2cf107a/attachment-0001.bin>


More information about the cfe-commits mailing list