Skip to content

[clang][bytecode] Handle union move assignment operators as well #125516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 3, 2025

Conversation

tbaederr
Copy link
Contributor

@tbaederr tbaederr commented Feb 3, 2025

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 3, 2025

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/125516.diff

3 Files Affected:

  • (modified) clang/lib/AST/ByteCode/Compiler.cpp (+4-3)
  • (modified) clang/lib/AST/ByteCode/Compiler.h (+1-1)
  • (modified) clang/test/AST/ByteCode/unions.cpp (+14)
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index f23472f99ac1b53..c1408379b4c1b8a 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 ecf50662d617bc8..0a93c46a40ef5f6 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 0a1f0f88650f279..b1fbb0c4dfc06a4 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

@tbaederr tbaederr merged commit 707e2b8 into llvm:main Feb 3, 2025
11 checks passed
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants