[clang] 707e2b8 - [clang][bytecode] Handle union move assignment operators as well (#125516)

via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 3 08:43:35 PST 2025


Author: Timm Baeder
Date: 2025-02-03T17:43:32+01:00
New Revision: 707e2b83a5d7f5d1a363f992197e3afad6369d6e

URL: https://github.com/llvm/llvm-project/commit/707e2b83a5d7f5d1a363f992197e3afad6369d6e
DIFF: https://github.com/llvm/llvm-project/commit/707e2b83a5d7f5d1a363f992197e3afad6369d6e.diff

LOG: [clang][bytecode] Handle union move assignment operators as well (#125516)

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Compiler.cpp
    clang/lib/AST/ByteCode/Compiler.h
    clang/test/AST/ByteCode/unions.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index f23472f99ac1b5..c1408379b4c1b8 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -5665,7 +5665,7 @@ bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
 }
 
 template <class Emitter>
-bool Compiler<Emitter>::compileUnionCopyAssignmentOperator(
+bool Compiler<Emitter>::compileUnionAssignmentOperator(
     const CXXMethodDecl *MD) {
   if (!this->emitThis(MD))
     return false;
@@ -5693,8 +5693,9 @@ bool Compiler<Emitter>::visitFunc(const FunctionDecl *F) {
   if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
     const RecordDecl *RD = MD->getParent();
 
-    if (RD->isUnion() && MD->isCopyAssignmentOperator())
-      return this->compileUnionCopyAssignmentOperator(MD);
+    if (RD->isUnion() &&
+        (MD->isCopyAssignmentOperator() || MD->isMoveAssignmentOperator()))
+      return this->compileUnionAssignmentOperator(MD);
 
     if (MD->isLambdaStaticInvoker())
       return this->emitLambdaStaticInvokerBody(MD);

diff  --git a/clang/lib/AST/ByteCode/Compiler.h b/clang/lib/AST/ByteCode/Compiler.h
index ecf50662d617bc..0a93c46a40ef5f 100644
--- a/clang/lib/AST/ByteCode/Compiler.h
+++ b/clang/lib/AST/ByteCode/Compiler.h
@@ -383,7 +383,7 @@ class Compiler : public ConstStmtVisitor<Compiler<Emitter>, bool>,
   bool emitBuiltinBitCast(const CastExpr *E);
   bool compileConstructor(const CXXConstructorDecl *Ctor);
   bool compileDestructor(const CXXDestructorDecl *Dtor);
-  bool compileUnionCopyAssignmentOperator(const CXXMethodDecl *MD);
+  bool compileUnionAssignmentOperator(const CXXMethodDecl *MD);
 
   bool checkLiteralType(const Expr *E);
 

diff  --git a/clang/test/AST/ByteCode/unions.cpp b/clang/test/AST/ByteCode/unions.cpp
index 0a1f0f88650f27..b1fbb0c4dfc06a 100644
--- a/clang/test/AST/ByteCode/unions.cpp
+++ b/clang/test/AST/ByteCode/unions.cpp
@@ -447,6 +447,20 @@ namespace CopyAssign {
   }
   static_assert(f2() == 12); // both-error {{not an integral constant expression}} \
                              // both-note {{in call to}}
+}
+
+namespace MoveAssign {
+  union A {
+    int a;
+    int b;
+  };
 
+  constexpr int f() {
+    A b{13};
+
+    b = A{12} ;
+    return b.a;
+  }
+  static_assert(f()== 12);
 }
 #endif


        


More information about the cfe-commits mailing list