[clang] [clang][bytecode] Handle negative array sizes in constexpr `new` instead of asserting (PR #155737)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 28 22:58:31 PDT 2025


================
@@ -1069,6 +1069,30 @@ namespace BaseCompare {
   static_assert(foo());
 }
 
+
+namespace NegativeArraySize { 
+
+constexpr void f() { // both-error {{constexpr function never produces a constant expression}}
+  int x = -1;
+  int *p = new int[x]; //both-note {{cannot allocate array; evaluated array bound -1 is negative}} 
+}
+
+} // namespace NegativeArraySize
+
----------------
tbaederr wrote:

```suggestion
namespace NegativeArraySize { 
  constexpr void f() { // both-error {{constexpr function never produces a constant expression}}
    int x = -1;
    int *p = new int[x]; //both-note {{cannot allocate array; evaluated array bound -1 is negative}} 
  }
} // namespace NegativeArraySize

```

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


More information about the cfe-commits mailing list