This is an archive of the discontinued LLVM Phabricator instance.

Fix PR19876 - Generic lambdas that capture 'this' used within NSDMIs result in a crash
ClosedPublic

Authored by faisalv on May 27 2014, 10:38 PM.

Details

Summary

See http://llvm.org/bugs/show_bug.cgi?id=19876

The following C++1y code results in a crash:

struct X {

int m = 10;
int n = [this](auto) { return m; }(20);

};

This is because when implicitly instantiating the generic lambda's call operator specialization body, Sema gets confused and is unable to determine the current 'this' type when transforming the MemberExpr 'm'.
That is SemaExprCXX.cpp:Sema::getCurrentThisType() is unable to calculate that the this type should be X* since it looks for the nearest enclosing FunctionDeclDC - which is obviously null.

I suppose there are two ways to fix this:

  1. In InstantiateFunctionDefinition, when the context is saved after the lambda scope info is created, retain the 'this' pointer.
  2. Teach getCurrentThisType() to recognize it is within a generic lambda within an NSDMI and return the appropriate this type.

I am not sure which way is definitely better - but either one seemed easy to implement.
I chose #2 - although I can't think of a good reason for choosing it over #1.
Feedback will be welcome - and especially if there is another option that might be even more robust.

Thanks!

Diff Detail

Event Timeline

faisalv updated this revision to Diff 9862.May 27 2014, 10:38 PM
faisalv retitled this revision from to [PATCH] Fix PR19876 - Generic lambdas that capture 'this' used within NSDMIs result in a crash.
faisalv updated this object.
faisalv edited the test plan for this revision. (Show Details)
faisalv added reviewers: rsmith, doug.gregor.
faisalv retitled this revision from [PATCH] Fix PR19876 - Generic lambdas that capture 'this' used within NSDMIs result in a crash to Fix PR19876 - Generic lambdas that capture 'this' used within NSDMIs result in a crash.
faisalv added a subscriber: Unknown Object (MLST).
rsmith accepted this revision.May 29 2014, 8:30 PM
rsmith edited edge metadata.
rsmith added inline comments.
lib/Sema/SemaExprCXX.cpp
744–750

Instead of these comments, I'd say "There are no cv-qualifiers, per [expr.prim.general]p4." Also, please use "default initializer" rather than "NSDMI".

This revision is now accepted and ready to land.May 29 2014, 8:30 PM