[clang] [Clang][Sema] Synthesize a memcpy body for defaulted union assignment (PR #206579)

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 28 06:39:47 PDT 2026


================
@@ -15383,6 +15401,31 @@ static void diagnoseDeprecatedCopyOperation(Sema &S, CXXMethodDecl *CopyOp) {
   }
 }
 
+// A defaulted copy or move assignment operator for a union copies the object
+// representation as if by a memcpy, the same way the defaulted union copy
+// constructor does.  The memberwise loops in DefineImplicitCopyAssignment and
+// DefineImplicitMoveAssignment skip union members, so the whole-object copy is
+// emitted here instead.  Marks AssignOp invalid and returns false on failure.
+static bool buildUnionAssignmentCopy(Sema &S, SourceLocation Loc,
+                                     CXXRecordDecl *ClassDecl,
+                                     std::optional<RefBuilder> &ExplicitObject,
+                                     std::optional<DerefBuilder> &DerefThis,
+                                     const ExprBuilder &From,
+                                     CXXMethodDecl *AssignOp,
+                                     SmallVectorImpl<Stmt *> &Statements) {
+  ExprBuilder &To = ExplicitObject ? static_cast<ExprBuilder &>(*ExplicitObject)
+                                   : static_cast<ExprBuilder &>(*DerefThis);
+  StmtResult Copy = buildMemcpyForAssignmentOp(
----------------
erichkeane wrote:

Since the point is to just pick up diagnostics here, we could probably just use `SFINAETrap`?  Thats a bit hacky, but at least would catch the diagnostics.

We DO have `IgnoringDiagnosticsConsumer` that might be better though, and just install that here.  @AaronBallman : Do you have a though on this?  

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


More information about the cfe-commits mailing list