[clang] [clang][bytecode] Fix activating primitive array elements (PR #191772)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 13 01:42:15 PDT 2026


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

For primitive array elements, we would accidentally activate the element and then immediate de-activate the array root, which is wrong.  Ignore the element from the beginning to the later check never even compares with the element.

>From 98b1af66a0728ef70d0f0463045f548c73045dae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Mon, 13 Apr 2026 10:20:54 +0200
Subject: [PATCH] asdf

---
 clang/lib/AST/ByteCode/Pointer.cpp        |  5 +++++
 clang/test/AST/ByteCode/placement-new.cpp | 14 ++++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 16c364d279020..a0a1c64bfd975 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -669,6 +669,11 @@ void Pointer::activate() const {
   };
 
   Pointer B = *this;
+  // Primitive array elements can't be activated individually, so
+  // look at the array root instead.
+  if (B.getFieldDesc()->isPrimitiveArray() && B.isArrayElement())
+    B = B.getArray();
+
   while (!B.isRoot() && B.inUnion()) {
     activate(B);
 
diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index 6091ab5602121..0175d7cfac785 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -539,3 +539,17 @@ namespace DirectBaseHasNoRecord {
   static_assert(test_multidim_single_start() == 13); // both-error {{not an integral constant expression}} \
                                                      // both-note {{in call to}}
 }
+
+namespace PrimArray {
+  constexpr int test_start_lifetime_array() {
+    struct S {
+      union { int storage[4]; };
+    };
+    S s;
+    s.storage[0] = 10;
+    ::new (&s.storage[0]) int(10);
+    ::new (&s.storage[1]) int(20);
+    return s.storage[0] + s.storage[1];
+  }
+  static_assert(test_start_lifetime_array() == 30);
+}



More information about the cfe-commits mailing list