[clang] [clang][bytecode] Fix an assertion for composite array roots (PR #195530)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sun May 3 07:30:31 PDT 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/195530

The assertion only holds if the array is primitive.

>From 55d56679bfba38864983b3eed57a84a4e53c307f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 3 May 2026 16:25:46 +0200
Subject: [PATCH] [clang][bytecode] Fix an assertion for composite array roots

The assertion only holds if the array is primitive.
---
 clang/lib/AST/ByteCode/Interp.cpp     |  5 +++--
 clang/test/AST/ByteCode/lifetimes.cpp | 15 +++++++++++----
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 6e93c6e88f261..8563ab8d844b7 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -2083,8 +2083,9 @@ static void setLifeStateRecurse(const Pointer &Ptr, Lifetime L) {
 
   if (const Descriptor *FieldDesc = Ptr.getFieldDesc();
       FieldDesc->isCompositeArray()) {
-    // No endLifetime() for array roots.
-    assert(Ptr.getLifetime() == Lifetime::Started);
+    // No endLifetime() for primitive array roots.
+    if (Ptr.getFieldDesc()->isPrimitiveArray())
+      assert(Ptr.getLifetime() == Lifetime::Started);
     for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I)
       setLifeStateRecurse(Ptr.atIndex(I).narrow(), L);
     return;
diff --git a/clang/test/AST/ByteCode/lifetimes.cpp b/clang/test/AST/ByteCode/lifetimes.cpp
index e775bed751821..a68c72e526e02 100644
--- a/clang/test/AST/ByteCode/lifetimes.cpp
+++ b/clang/test/AST/ByteCode/lifetimes.cpp
@@ -96,12 +96,10 @@ namespace CallScope {
   constexpr Q *out_of_lifetime(Q q) { return &q; } // both-warning {{address of stack}} \
                                                    // both-note 2{{declared here}}
   constexpr int k3 = out_of_lifetime({})->n; // both-error {{must be initialized by a constant expression}} \
-                                             // expected-note {{read of object outside its lifetime}} \
-                                             // ref-note {{read of object outside its lifetime}}
+                                             // both-note {{read of object outside its lifetime}}
 
   constexpr int k4 = out_of_lifetime({})->f(); // both-error {{must be initialized by a constant expression}} \
-                                               // expected-note {{member call on object outside its lifetime}} \
-                                               // ref-note {{member call on object outside its lifetime}}
+                                               // both-note {{member call on object outside its lifetime}}
 }
 
 namespace ExprDoubleDestroy {
@@ -115,3 +113,12 @@ namespace ExprDoubleDestroy {
   constexpr bool t = test<S>(); // both-error {{must be initialized by a constant expression}} \
                                 // both-note {{in call to}}
 }
+
+namespace CompositeArrayRootAssertion {
+  class C {
+  public:
+    bool B[2][2];
+    constexpr ~C() {}
+  };
+  void foo(int i) { C c; }
+}



More information about the cfe-commits mailing list