[clang] [clang][bytecode] Allow more function calls in CPCE mode (PR #192597)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 16 23:10:48 PDT 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/192597

We previously didn't diagnose the attached test cases correctly.

>From a55826eeb0e6aa1ad158ada9fac20db752d16590 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 17 Apr 2026 07:52:42 +0200
Subject: [PATCH] asdf

---
 clang/lib/AST/ByteCode/Interp.cpp      |  3 ---
 clang/lib/AST/ByteCode/Interp.h        |  2 +-
 clang/test/AST/ByteCode/new-delete.cpp | 10 ++++++++++
 clang/test/SemaCXX/cxx2a-consteval.cpp |  1 +
 4 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 17d194355208d..f6fe28c03f0be 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -1006,9 +1006,6 @@ static bool CheckCallable(InterpState &S, CodePtr OpPC, const Function *F) {
     return false;
   }
 
-  if (S.checkingPotentialConstantExpression() && S.Current->getDepth() != 0)
-    return false;
-
   if (F->isValid() && F->hasBody() &&
       (F->isConstexpr() || (S.Current->MSVCConstexprAllowed &&
                             F->getDecl()->hasAttr<MSConstexprAttr>())))
diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h
index 2bc74fcfe7b95..0f7f2c4e1fdec 100644
--- a/clang/lib/AST/ByteCode/Interp.h
+++ b/clang/lib/AST/ByteCode/Interp.h
@@ -2070,7 +2070,7 @@ inline bool GetPtrBasePop(InterpState &S, CodePtr OpPC, uint32_t Off,
 }
 
 inline bool GetPtrThisBase(InterpState &S, CodePtr OpPC, uint32_t Off) {
-  if (S.checkingPotentialConstantExpression())
+  if (S.checkingPotentialConstantExpression() && S.Current->isBottomFrame())
     return false;
   if (!CheckThis(S, OpPC))
     return false;
diff --git a/clang/test/AST/ByteCode/new-delete.cpp b/clang/test/AST/ByteCode/new-delete.cpp
index 3eb0d1a13bf49..6467aa2e1d20b 100644
--- a/clang/test/AST/ByteCode/new-delete.cpp
+++ b/clang/test/AST/ByteCode/new-delete.cpp
@@ -612,6 +612,16 @@ namespace CastedDelete {
   }
   static_assert(foo() == 1); // both-error {{not an integral constant expression}} \
                              // both-note {{in call to}}
+
+  constexpr bool nvdtor() { // both-error {{never produces a constant expression}}
+    struct S {
+      constexpr ~S() {}
+    };
+    struct T : S {};
+    delete (S*)new T; // both-note {{delete of object with dynamic type 'T' through pointer to base class type 'S' with non-virtual destructor}}
+    return true;
+  }
+
 }
 
 constexpr void use_after_free_2() { // both-error {{never produces a constant expression}}
diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp
index 92bfa40caec4e..6440a4e85df83 100644
--- a/clang/test/SemaCXX/cxx2a-consteval.cpp
+++ b/clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value -Wno-vla %s -verify
+// RUN: %clang_cc1 -std=c++2a -emit-llvm-only -Wno-unused-value -Wno-vla %s -verify -fexperimental-new-constant-interpreter
 
 typedef __SIZE_TYPE__ size_t;
 



More information about the cfe-commits mailing list