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

via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 15 23:07:38 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

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`.

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Pointer.cpp (+1-1) 
- (modified) clang/test/AST/ByteCode/cxx23.cpp (+12) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index 839a0929d1852..a3c230615e786 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 e011aef22031b..ec6f7fe57cd55 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

``````````

</details>


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


More information about the cfe-commits mailing list