Author: Timm Baeder
Date: 2025-05-15T14:34:35+02:00
New Revision: b26adacc8550bc8786ecba08ec87d0b228930bf4

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

LOG: [clang][bytecode] Check destructors for temporaries (#140039)

Also, increase the EvalID in isPotentialConstantExpr(), since this is
its own evaluation.

Added: 
    

Modified: 
    clang/lib/AST/ByteCode/Context.cpp
    clang/lib/AST/ByteCode/Interp.cpp
    clang/test/AST/ByteCode/cxx20.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ByteCode/Context.cpp 
b/clang/lib/AST/ByteCode/Context.cpp
index dae94fc9829c7..c70a5259b77e2 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -37,6 +37,7 @@ bool Context::isPotentialConstantExpr(State &Parent, const 
FunctionDecl *FD) {
   Compiler<ByteCodeEmitter>(*this, *P).compileFunc(
       FD, const_cast<Function *>(Func));
 
+  ++EvalID;
   // And run it.
   if (!Run(Parent, Func))
     return false;

diff  --git a/clang/lib/AST/ByteCode/Interp.cpp 
b/clang/lib/AST/ByteCode/Interp.cpp
index bc860185fea21..fbe04fe3231dd 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1373,6 +1373,8 @@ static bool checkConstructor(InterpState &S, CodePtr 
OpPC, const Function *Func,
 bool CheckDestructor(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
   if (!CheckLive(S, OpPC, Ptr, AK_Destroy))
     return false;
+  if (!CheckTemporary(S, OpPC, Ptr, AK_Destroy))
+    return false;
 
   // Can't call a dtor on a global variable.
   if (Ptr.block()->isStatic()) {

diff  --git a/clang/test/AST/ByteCode/cxx20.cpp 
b/clang/test/AST/ByteCode/cxx20.cpp
index 8fb19fcfcd3fe..5f62a5648bab7 100644
--- a/clang/test/AST/ByteCode/cxx20.cpp
+++ b/clang/test/AST/ByteCode/cxx20.cpp
@@ -997,3 +997,13 @@ namespace NastyChar {
   template <ToNastyChar t> constexpr auto to_nasty_char() { return t; }
   constexpr auto result = to_nasty_char<"12345">();
 }
+
+namespace TempDtor {
+  struct A {
+    int n;
+  };
+  constexpr A &&a_ref = A(); // both-note {{temporary created here}}
+  constexpr void destroy_extern_2() { // both-error {{never produces a 
constant expression}}
+    a_ref.~A(); // both-note {{destruction of temporary is not allowed in a 
constant expression outside the expression that created the temporary}}
+  }
+}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to