[clang] Reapply "[Clang][CWG1815] Support lifetime extension of temporary created by aggregate initialization using a default member initializer" (PR #97308)

via cfe-commits cfe-commits at lists.llvm.org
Tue Aug 27 09:14:12 PDT 2024


================
@@ -5567,19 +5568,35 @@ ExprResult Sema::BuildCXXDefaultInitExpr(SourceLocation Loc, FieldDecl *Field) {
   ImmediateCallVisitor V(getASTContext());
   if (!NestedDefaultChecking)
     V.TraverseDecl(Field);
-  if (V.HasImmediateCalls) {
+
+  // CWG1815
+  // Support lifetime extension of temporary created by aggregate
+  // initialization using a default member initializer. We should always rebuild
+  // the initializer if it contains any temporaries (if the initializer
+  // expression is an ExprWithCleanups). Then make sure the normal lifetime
+  // extension code recurses into the default initializer and does lifetime
+  // extension when warranted.
+  bool ContainsAnyTemporaries =
+      isa_and_present<ExprWithCleanups>(Field->getInClassInitializer());
+  if (V.HasImmediateCalls || InLifetimeExtendingContext ||
+      ContainsAnyTemporaries) {
----------------
yronglin wrote:

UUIC, the concerns in https://github.com/llvm/llvm-project/pull/87933#discussion_r1589797605 is that lifetime extension context will extend all temporaries in the initializer. Currently, lifetime extension context is just a flag and collect all top-level temporaries, in for-range statements, we extend these collected temporaries to __range var. But here we can ignore the collected temporaries, so I think we can only rebuild lifetime extending context when `Field->getInClassInitializer()->containsErrors() && (V.HasImmediateCalls || (InLifetimeExtendingContext && ContainsAnyTemporaries))`. @zygoloid @cor3ntin What do you think?

https://github.com/llvm/llvm-project/pull/97308


More information about the cfe-commits mailing list