[clang] 3280596 - [clang][bytecode] Diagnose placement-new'ing to a temporary (#141099)

via cfe-commits cfe-commits at lists.llvm.org
Thu May 22 23:17:25 PDT 2025


Author: Timm Baeder
Date: 2025-05-23T08:17:22+02:00
New Revision: 32805964fcc2d5ad6296aae91e18053720e7a8a0

URL: https://github.com/llvm/llvm-project/commit/32805964fcc2d5ad6296aae91e18053720e7a8a0
DIFF: https://github.com/llvm/llvm-project/commit/32805964fcc2d5ad6296aae91e18053720e7a8a0.diff

LOG: [clang][bytecode] Diagnose placement-new'ing to a temporary (#141099)

... that's been created in a different evaluation.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Interp.cpp
    clang/test/AST/ByteCode/placement-new.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 7cc0d2a526480..17660570d24cd 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1703,6 +1703,8 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
                           std::optional<uint64_t> ArraySize) {
   const Pointer &Ptr = S.Stk.peek<Pointer>();
 
+  if (!CheckTemporary(S, OpPC, Ptr, AK_Construct))
+    return false;
   if (!CheckStore(S, OpPC, Ptr))
     return false;
 

diff  --git a/clang/test/AST/ByteCode/placement-new.cpp b/clang/test/AST/ByteCode/placement-new.cpp
index 14b893c7e587b..9015690256458 100644
--- a/clang/test/AST/ByteCode/placement-new.cpp
+++ b/clang/test/AST/ByteCode/placement-new.cpp
@@ -15,7 +15,8 @@ namespace std {
   constexpr void construct_at(void *p, Args &&...args) {
     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 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}}
 
   }
 }
@@ -391,3 +392,9 @@ namespace MemMove {
 
   static_assert(foo() == 123);
 }
+
+namespace Temp {
+  constexpr int &&temporary = 0; // both-note {{created here}}
+  static_assert((std::construct_at<int>(&temporary, 1), true)); // both-error{{not an integral constant expression}} \
+                                                                // both-note {{in call}}
+}


        


More information about the cfe-commits mailing list