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!
This whitespace change shouldn't have been included - as an aside - we still strive unix style line endings right?