Author: Alexander Kornienko
Date: 2025-05-16T16:29:33+02:00
New Revision: 3aeced73088e08c61999a8d011e28481d959b1bc

URL: 
https://github.com/llvm/llvm-project/commit/3aeced73088e08c61999a8d011e28481d959b1bc
DIFF: 
https://github.com/llvm/llvm-project/commit/3aeced73088e08c61999a8d011e28481d959b1bc.diff

LOG: [clang] Fix assertion failure in constexpr union deserialization (#140179)

This commit fixes https://github.com/llvm/llvm-project/issues/140130

Added: 
    clang/test/Modules/pr140130.cpp

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/include/clang/AST/PropertiesBase.td

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index cdb15ba8c804c..b92cef29468ed 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -622,6 +622,7 @@ Bug Fixes in This Version
 - Fixed an assertion failure in constant compound literal statements. 
(#GH139160)
 - Fix crash due to unknown references and pointer implementation and handling 
of
   base classes. (GH139452)
+- Fixed an assertion failure in serialization of constexpr structs containing 
unions. (#GH140130)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/include/clang/AST/PropertiesBase.td 
b/clang/include/clang/AST/PropertiesBase.td
index 33336d57b6298..111a3e44f2fd5 100644
--- a/clang/include/clang/AST/PropertiesBase.td
+++ b/clang/include/clang/AST/PropertiesBase.td
@@ -414,7 +414,8 @@ let Class = PropertyTypeCase<APValue, "Union"> in {
     let Read = [{ node.getUnionValue() }];
   }
   def : Creator<[{
-    return APValue(cast<clang::FieldDecl>(fieldDecl), std::move(value));
+    // node.getUnionField() / fieldDecl can be null, thus, using 
`cast_if_present`
+    return APValue(cast_if_present<clang::FieldDecl>(fieldDecl), 
std::move(value));
   }]>;
 }
 let Class = PropertyTypeCase<APValue, "AddrLabelDiff"> in {

diff  --git a/clang/test/Modules/pr140130.cpp b/clang/test/Modules/pr140130.cpp
new file mode 100644
index 0000000000000..da26a005b04f8
--- /dev/null
+++ b/clang/test/Modules/pr140130.cpp
@@ -0,0 +1,33 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+// RUN: cd %t
+// RUN: %clang_cc1 -iquote . -fmodules -fno-cxx-modules -emit-module \
+// RUN:   -std=c++20 -fmodule-name=c -xc++ c.cppmap -o c.pcm
+// RUN: %clang_cc1 -iquote . -fmodules -fno-cxx-modules -emit-module \
+// RUN:   -std=c++20 -fmodule-name=a -fmodule-map-file=a.cppmap \
+// RUN:   -fmodule-file=c.pcm -xc++ a.cppmap -o a.pcm
+
+//--- a.cppmap
+module "a" {
+ header "a.h"
+}
+//--- a.h
+#include "b.h"
+//--- b.h
+#ifndef _B_H_
+#define _B_H_
+struct B {
+  consteval B() {}
+  union {
+    int a;
+  };
+};
+constexpr B b;
+#endif
+//--- c.cppmap
+module "c" {
+header "c.h"
+}
+//--- c.h
+#include "b.h"


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to