[clang] [clang][bytecode] Fix `Pointer::getLifetime()` for non-primitive array elements (PR #232605)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 15 23:06:56 PDT 2026


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

This code path is not reached for pointers to non-primitive array elements in regular evaluation, but we do call `getLifetime()` for random pointers in `__builtin_is_within_lifetime`.

>From 295120e9c79cebfbcfd6bd01a8d2e6d57e6a7e7c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 16 Oct 2026 08:03:40 +0200
Subject: [PATCH] [clang][bytecode] Fix `Pointer::getLifetime()` for
 non-primitive array elements

This code path is not reached for pointers to non-primitive
array elements in regular evaluation, but we do call `getLifetime()`
for random pointers in `__builtin_is_within_lifetime`.
---
 clang/lib/AST/ByteCode/Pointer.cpp |  2 +-
 clang/test/AST/ByteCode/cxx23.cpp  | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 839a0929d18525..a3c230615e7868 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -769,7 +769,7 @@ Lifetime PtrView::getLifetime() const {
   if (Base < sizeof(InlineDescriptor))
     return Lifetime::Started;
 
-  if (inArray() && !isArrayRoot()) {
+  if (getFieldDesc()->isPrimitiveArray() && !isArrayRoot()) {
     InitMapPtr &IM = getInitMap();
 
     if (!IM.hasInitMap()) {
diff --git a/clang/test/AST/ByteCode/cxx23.cpp b/clang/test/AST/ByteCode/cxx23.cpp
index e011aef22031b6..ec6f7fe57cd55d 100644
--- a/clang/test/AST/ByteCode/cxx23.cpp
+++ b/clang/test/AST/ByteCode/cxx23.cpp
@@ -702,4 +702,16 @@ namespace InvalidRecord {
 
   static_assert(foo(B{1, 2, 3}), "");
 }
+
+namespace WithinLifetime {
+  struct S {};
+  constexpr int f(int n) {
+    S *s = new S[n]; // all-note {{declared here}}
+    if (__builtin_is_within_lifetime(s)) { // all-error {{call to consteval function '__builtin_is_within_lifetime' is not a constant expression}} \
+                                           // all-note {{read of non-constexpr variable 's' is not allowed in a constant expression}}
+    }
+  } // all-note {{control reached end of constexpr function}}
+  static_assert(f(42), ""); // all-error {{not an integral constant expression}} \
+                            // all-note {{in call to}}
+}
 #endif



More information about the cfe-commits mailing list