[PATCH] D54550: Mark lambda decl as invalid if a captured variable has an invalid type.

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 20 11:57:46 PST 2018


rsmith added a comment.

Thanks! Some simplifications are possible here, but otherwise this looks good.



================
Comment at: clang/lib/Sema/SemaExpr.cpp:14946
+  // as invalid as well.
+  if (const CXXRecordDecl *RD = FieldType->getAsCXXRecordDecl()) {
+    if (RD->isInvalidDecl()) {
----------------
jgorbe wrote:
> rsmith wrote:
> > You'll need to use `FieldType->getBaseElementTypeUnsafe()` or similar here to handle the case where the field type is an array whose element type is a class. (That can happen in a lambda if you capture a local array variable by value.)
> Done, I ended up using Context.getBaseElementType because the call to RequireCompleteType below required a QualType.
Hah, OK, now you've switched to using `RequireCompleteType` this has become redundant again (it'll walk to the base element type for you). =)


================
Comment at: clang/lib/Sema/SemaExpr.cpp:14952-14958
+      NamedDecl *Def;
+      EltTy->isIncompleteType(&Def);
+      if (Def && Def->isInvalidDecl()) {
+        Lambda->setInvalidDecl();
+        Field->setInvalidDecl();
+      }
+    }
----------------
I believe the `else` case here is redundant: if `RequireCompleteType` returns `false`, the type is complete and there's nothing more you need to do.


================
Comment at: clang/test/SemaCXX/lambda-invalid-capture.cpp:8-12
+void h() {
+  g child;
+  auto q = [child]{};
+  const int n = sizeof(q);
+}
----------------
Can you also add a test for the "capturing an array of incomplete type" case please?


https://reviews.llvm.org/D54550





More information about the cfe-commits mailing list