[clang] [clang][bytecode] Fix AccessKinds in placement new CheckStore() call (PR #141123)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu May 22 23:19:00 PDT 2025


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/141123

>From 007962efa820248c12dc7fc2c84de67cf54e5c00 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Thu, 22 May 2025 21:01:55 +0200
Subject: [PATCH] [clang][bytecode] Fix AccessKinds in placement new
 CheckStore() call

CheckStore is for assignments, but we're constructing something here, so
pass AK_Construct instead. We already diagnosed the test case, but as an
assignment.
---
 clang/lib/AST/ByteCode/Interp.cpp         | 18 +++++++++++++++++-
 clang/test/AST/ByteCode/placement-new.cpp | 15 +++++++++++++--
 2 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 17660570d24cd..6d6beef73a726 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1703,9 +1703,25 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
                           std::optional<uint64_t> ArraySize) {
   const Pointer &Ptr = S.Stk.peek<Pointer>();
 
+  // Similar to CheckStore(), but with the additional CheckTemporary() call and
+  // the AccessKinds are different.
   if (!CheckTemporary(S, OpPC, Ptr, AK_Construct))
     return false;
-  if (!CheckStore(S, OpPC, Ptr))
+  if (!CheckLive(S, OpPC, Ptr, AK_Construct))
+    return false;
+  if (!CheckDummy(S, OpPC, Ptr, AK_Construct))
+    return false;
+  if (!CheckLifetime(S, OpPC, Ptr, AK_Construct))
+    return false;
+  if (!CheckExtern(S, OpPC, Ptr))
+    return false;
+  if (!CheckRange(S, OpPC, Ptr, AK_Construct))
+    return false;
+  if (!CheckGlobal(S, OpPC, Ptr))
+    return false;
+  if (!CheckConst(S, OpPC, Ptr))
+    return false;
+  if (!S.inConstantContext() && isConstexprUnknown(Ptr))
     return false;
 
   if (!InvalidNewDeleteExpr(S, OpPC, E))
diff --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index 9015690256458..81f7782c6f252 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -16,8 +16,8 @@ namespace std {
     new (p) T((Args&&)args...); // both-note {{in call to}} \
                                 // both-note {{placement new would change type of storage from 'int' to 'float'}} \
                                 // both-note {{construction of subobject of member 'x' of union with active member 'a' is not allowed in a constant expression}} \
-                                // both-note {{construction of temporary is not allowed}}
-
+                                // both-note {{construction of temporary is not allowed}} \
+                                // both-note {{construction of heap allocated object that has been deleted}}
   }
 }
 
@@ -398,3 +398,14 @@ namespace Temp {
   static_assert((std::construct_at<int>(&temporary, 1), true)); // both-error{{not an integral constant expression}} \
                                                                 // both-note {{in call}}
 }
+
+namespace PlacementNewAfterDelete {
+  constexpr bool construct_after_lifetime() {
+    int *p = new int;
+    delete p;
+    std::construct_at<int>(p); // both-note {{in call}}
+    return true;
+  }
+  static_assert(construct_after_lifetime()); // both-error {{}} \
+                                             // both-note {{in call}}
+}



More information about the cfe-commits mailing list