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

via cfe-commits cfe-commits at lists.llvm.org
Thu May 22 09:46:08 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

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

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


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Interp.cpp (+2) 
- (modified) clang/test/AST/ByteCode/placement-new.cpp (+8-1) 


``````````diff
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index b3ac84ce9278f..573a2b05f3bdc 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1702,6 +1702,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}}
+}

``````````

</details>


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


More information about the cfe-commits mailing list