[clang] [clang][bytecode] Fix activating primitive array elements (PR #191772)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 13 05:20:09 PDT 2026
https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/191772
>From a9a299cace4a0e2e8c0ea90afce93013ab8e8829 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/Interp.cpp | 2 ++
clang/lib/AST/ByteCode/Pointer.cpp | 5 +++++
clang/test/AST/ByteCode/placement-new.cpp | 26 +++++++++++++++++++++++
3 files changed, 33 insertions(+)
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index f4d6ef4dc8bd6..abcf55bfa670d 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1991,6 +1991,8 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
const Pointer &Ptr = S.Stk.peek<Pointer>();
auto directBaseIsUnion = [](const Pointer &Ptr) -> bool {
+ if (Ptr.isArrayElement())
+ return false;
const Record *R = Ptr.getBase().getRecord();
return R && R->isUnion();
};
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..5bad616a0d359 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -539,3 +539,29 @@ 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);
+
+
+ constexpr int primElem() {
+ union {int a[2]; };
+
+ new (&a[1]) int(30); // both-note {{construction of subobject of member 'a' of union with no active member is not allowed in a constant expression}}
+ return a[1];
+ }
+ static_assert(primElem() == 30); // both-error {{not an integral constant expression}} \
+ // both-note {{in call to}}
+
+
+}
More information about the cfe-commits
mailing list