[clang] [Clang] Fix crash during constexpr virtual destructor evaluation (PR #238088)

via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 4 11:08:37 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Daniel Petrovic (daniel-petrovic)

<details>
<summary>Changes</summary>

Pass the designated object's type to findCompleteObject in checkDynamicType instead of null QualType.

This prevents assertions when contant evaluation attempts to destroy objects without ordinary storage, such as type_info objects produced by typeid expressions.

Use the designator's type, which also correctly identifies the currently designated object.

Fixes: #<!-- -->237168

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


2 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+2-1) 
- (added) clang/test/SemaCXX/constexpr-typeid-destruction.cpp (+20) 


``````````diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 57d5bb475d0f9b..dcee61ce87d22e 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -6675,7 +6675,8 @@ static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,
   if (This.Designator.Invalid)
     return false;
 
-  CompleteObject Obj = findCompleteObject(Info, E, AK, This, QualType());
+  CompleteObject Obj =
+      findCompleteObject(Info, E, AK, This, This.Designator.getType(Info.Ctx));
 
   if (!Obj)
     return false;
diff --git a/clang/test/SemaCXX/constexpr-typeid-destruction.cpp b/clang/test/SemaCXX/constexpr-typeid-destruction.cpp
new file mode 100644
index 00000000000000..04c2409b1526af
--- /dev/null
+++ b/clang/test/SemaCXX/constexpr-typeid-destruction.cpp
@@ -0,0 +1,20 @@
+// RUN: %clang_cc1 -std=c++14 -verify=expected,ref,cxx20 %s
+// RUN: %clang_cc1 -std=c++20 -verify=expected,ref,cxx20 %s
+// RUN: %clang_cc1 -std=c++23 -verify=expected,ref %s
+// RUN: %clang_cc1 -std=c++23 -fexperimental-new-constant-interpreter -verify=expected %s
+
+namespace std {
+struct type_info {
+  virtual ~type_info();
+};
+}
+
+// Ensure that diagnosing virtual dispatch on type_info object does not crash (GH237168).
+constexpr void destroy_type_info() { // cxx20-error {{constexpr function never produces a constant expression}}
+  typeid(int).~type_info(); // ref-note {{destruction of object 'typeid(int)' whose value is not known}}
+}
+
+#if __cplusplus > 202002L
+static_assert((destroy_type_info(), true)); // expected-error {{static assertion expression is not an integral constant expression}} \
+                                           // ref-note {{in call to 'destroy_type_info()'}}
+#endif

``````````

</details>


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


More information about the cfe-commits mailing list