[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-04 Thread Macro Terra via cfe-commits

hongtaihu wrote:

> Can you detail the extent of the AI assistance?

It was used to minimize the test case and help me understand the root cause. I 
checked the behavior before and after the fix.

https://github.com/llvm/llvm-project/pull/228675
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-04 Thread Oliver Hunt via cfe-commits

ojhunt wrote:

Can you detail the extent of the AI assistance?

https://github.com/llvm/llvm-project/pull/228675
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-04 Thread Macro Terra via cfe-commits

hongtaihu wrote:

> You need to fix the bytecode interpreter too:
> 
> ```diff
> diff --git i/clang/lib/AST/ByteCode/Compiler.cpp 
> w/clang/lib/AST/ByteCode/Compiler.cpp
> index 313687f53816..58e68de845c9 100644
> --- i/clang/lib/AST/ByteCode/Compiler.cpp
> +++ w/clang/lib/AST/ByteCode/Compiler.cpp
> @@ -4384,6 +4384,10 @@ bool Compiler::VisitCXXNewExpr(const 
> CXXNewExpr *E) {
>  DynamicInit = ILE->getArrayFiller();
>else if (StaticInitElems > 0 && 
> isa(ILE->getInit(0)))
>  ElemT = classifyPrim(CAT->getElementType());
> +} else if (const auto *PLI = dyn_cast(Init)) {
> +  DynamicInit = PLI->getArrayFiller();
> +  if (StaticInitElems > 0 && 
> isa(PLI->getInitExprs()[0]))
> +ElemT = classifyPrim(CAT->getElementType());
>  }
>}
> ```

done

https://github.com/llvm/llvm-project/pull/228675
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-04 Thread Macro Terra via cfe-commits


@@ -0,0 +1,39 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+// expected-no-diagnostics
+

hongtaihu wrote:

changed test file

https://github.com/llvm/llvm-project/pull/228675
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-04 Thread Macro Terra via cfe-commits

https://github.com/hongtaihu updated 
https://github.com/llvm/llvm-project/pull/228675

>From 7a3f90b3e00a2151b0402fd9433dfbe11066cca6 Mon Sep 17 00:00:00 2001
From: hongtaihu <[email protected]>
Date: Sat, 3 Oct 2026 15:40:16 +0800
Subject: [PATCH 1/3] [clang][constexpr] Evaluate parenthesized array new with
 the allocated bound

When evaluating an array new-expression, the actual allocation bound can differ 
from the bound recorded in the initializer's type. This occurs when the array 
bound is a function parameter whose value becomes known during constant 
evaluation.

The branch handling this difference assumes that the initializer is an 
`InitListExpr`. However, parenthesized array initialization produces a 
`CXXParenListInitExpr`, causing the cast to assert.

For example, this valid C++20 program triggers the assertion:

```cpp
constexpr bool f(unsigned n) {
  int *p = new int[n](5);
  delete[] p;
  return true;
}
static_assert(f(3));
```

Preserve the initializer as an `Expr` and dispatch according to its actual node 
type. For parenthesized initialization, reuse the existing array evaluation 
logic with the actual allocation type. This also ensures that the remaining 
elements are initialized using the correct array bound.

Add regression coverage for:

- Parenthesized integer array initialization with different bounds.
- Class array initialization with default construction of remaining elements.
- Existing braced string initialization.

Validation on the existing local assertions build, before rebasing onto current 
main: the regression passes in C++20 and C++23 modes. The original malformed 
input produces normal diagnostics without crashing in both syntax-only and 
assembly compilation modes.

Fixes #204757

Assisted by Codex
---
 clang/lib/AST/ExprConstant.cpp| 38 ++
 .../constexpr-array-new-paren-init.cpp| 39 +++
 2 files changed, 60 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/SemaCXX/constexpr-array-new-paren-init.cpp

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 3f295f35d1361..30116acfe5716 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10963,9 +10963,9 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
   }
 }
 
-static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
- APValue &Result, const InitListExpr *ILE,
- QualType AllocType);
+static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This,
+ APValue &Result, const Expr *Init,
+ QualType AllocType);
 static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
   APValue &Result,
   const CXXConstructExpr *CCE,
@@ -11035,7 +11035,7 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
   }
 
   const Expr *Init = E->getInitializer();
-  const InitListExpr *ResizedArrayILE = nullptr;
+  const Expr *ResizedArrayInit = nullptr;
   const CXXConstructExpr *ResizedArrayCCE = nullptr;
   bool ValueInit = false;
 
@@ -11104,10 +11104,11 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
 return false;
   }
 
-  // If the sizes differ, we must have an initializer list, and we need
-  // special handling for this case when we initialize.
+  // Array initialization can use either braces or parentheses. If the
+  // sizes differ, evaluate it with the allocated bound rather than the
+  // bound used when checking the initializer.
   if (InitBound != AllocBound)
-ResizedArrayILE = cast(Init);
+ResizedArrayInit = Init;
 }
 
 AllocType = Info.Ctx.getConstantArrayType(AllocType, ArrayBound, nullptr,
@@ -11205,9 +11206,8 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
 ImplicitValueInitExpr VIE(AllocType);
 if (!EvaluateInPlace(*Val, Info, Result, &VIE))
   return false;
-  } else if (ResizedArrayILE) {
-if (!EvaluateArrayNewInitList(Info, Result, *Val, ResizedArrayILE,
-  AllocType))
+  } else if (ResizedArrayInit) {
+if (!EvaluateArrayNewInit(Info, Result, *Val, ResizedArrayInit, AllocType))
   return false;
   } else if (ResizedArrayCCE) {
 if (!EvaluateArrayNewConstructExpr(Info, Result, *Val, ResizedArrayCCE,
@@ -15636,14 +15636,18 @@ static bool EvaluateArray(const Expr *E, const LValue 
&This,
   return ArrayExprEvaluator(Info, This, Result).Visit(E);
 }
 
-static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
- APValue &Result, const InitListExpr *ILE,
- QualType AllocType) {
-  assert(!ILE->isValueDependent());
-  assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
+static 

[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-04 Thread Macro Terra via cfe-commits

https://github.com/hongtaihu edited 
https://github.com/llvm/llvm-project/pull/228675
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-03 Thread Timm Baeder via cfe-commits

tbaederr wrote:

You need to fix the bytecode interpreter too:
```diff
diff --git i/clang/lib/AST/ByteCode/Compiler.cpp 
w/clang/lib/AST/ByteCode/Compiler.cpp
index 313687f53816..58e68de845c9 100644
--- i/clang/lib/AST/ByteCode/Compiler.cpp
+++ w/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4384,6 +4384,10 @@ bool Compiler::VisitCXXNewExpr(const CXXNewExpr 
*E) {
 DynamicInit = ILE->getArrayFiller();
   else if (StaticInitElems > 0 && isa(ILE->getInit(0)))
 ElemT = classifyPrim(CAT->getElementType());
+} else if (const auto *PLI = dyn_cast(Init)) {
+  DynamicInit = PLI->getArrayFiller();
+  if (StaticInitElems > 0 && 
isa(PLI->getInitExprs()[0]))
+ElemT = classifyPrim(CAT->getElementType());
 }
   }
```

https://github.com/llvm/llvm-project/pull/228675
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-03 Thread Timm Baeder via cfe-commits


@@ -0,0 +1,39 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+// expected-no-diagnostics
+

tbaederr wrote:

We already have SemaCXX/new-delete.cpp

https://github.com/llvm/llvm-project/pull/228675
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-03 Thread via cfe-commits

llvmorg-github-actions[bot] wrote:




@llvm/pr-subscribers-clang

Author: Macro Terra (hongtaihu)


Changes

When evaluating an array new-expression, the actual allocation bound can differ 
from the bound recorded in the initializer's type. This occurs when the array 
bound is a function parameter whose value becomes known during constant 
evaluation.

The branch handling this difference assumes that the initializer is an 
`InitListExpr`. However, parenthesized array initialization produces a 
`CXXParenListInitExpr`, causing the cast to assert.

For example, this valid C++20 program triggers the assertion:

```cpp
constexpr bool f(unsigned n) {
  int *p = new int[n](5);
  delete[] p;
  return true;
}
static_assert(f(3));
```

Preserve the initializer as an `Expr` and dispatch according to its actual node 
type. For parenthesized initialization, reuse the existing array evaluation 
logic with the actual allocation type. This also ensures that the remaining 
elements are initialized using the correct array bound.

Add regression coverage for:

- Parenthesized integer array initialization with different bounds.
- Class array initialization with default construction of remaining elements.
- Existing braced string initialization.

Validation on the existing local assertions build, before rebasing onto current 
main: the regression passes in C++20 and C++23 modes. The original malformed 
input produces normal diagnostics without crashing in both syntax-only and 
assembly compilation modes.

Fixes #204757

Assisted by Codex


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


2 Files Affected:

- (modified) clang/lib/AST/ExprConstant.cpp (+21-17) 
- (added) clang/test/SemaCXX/constexpr-array-new-paren-init.cpp (+39) 


``diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 3f295f35d1361..30116acfe5716 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10963,9 +10963,9 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
   }
 }
 
-static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
- APValue &Result, const InitListExpr *ILE,
- QualType AllocType);
+static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This,
+ APValue &Result, const Expr *Init,
+ QualType AllocType);
 static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
   APValue &Result,
   const CXXConstructExpr *CCE,
@@ -11035,7 +11035,7 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
   }
 
   const Expr *Init = E->getInitializer();
-  const InitListExpr *ResizedArrayILE = nullptr;
+  const Expr *ResizedArrayInit = nullptr;
   const CXXConstructExpr *ResizedArrayCCE = nullptr;
   bool ValueInit = false;
 
@@ -11104,10 +11104,11 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
 return false;
   }
 
-  // If the sizes differ, we must have an initializer list, and we need
-  // special handling for this case when we initialize.
+  // Array initialization can use either braces or parentheses. If the
+  // sizes differ, evaluate it with the allocated bound rather than the
+  // bound used when checking the initializer.
   if (InitBound != AllocBound)
-ResizedArrayILE = cast(Init);
+ResizedArrayInit = Init;
 }
 
 AllocType = Info.Ctx.getConstantArrayType(AllocType, ArrayBound, nullptr,
@@ -11205,9 +11206,8 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
 ImplicitValueInitExpr VIE(AllocType);
 if (!EvaluateInPlace(*Val, Info, Result, &VIE))
   return false;
-  } else if (ResizedArrayILE) {
-if (!EvaluateArrayNewInitList(Info, Result, *Val, ResizedArrayILE,
-  AllocType))
+  } else if (ResizedArrayInit) {
+if (!EvaluateArrayNewInit(Info, Result, *Val, ResizedArrayInit, AllocType))
   return false;
   } else if (ResizedArrayCCE) {
 if (!EvaluateArrayNewConstructExpr(Info, Result, *Val, ResizedArrayCCE,
@@ -15636,14 +15636,18 @@ static bool EvaluateArray(const Expr *E, const LValue 
&This,
   return ArrayExprEvaluator(Info, This, Result).Visit(E);
 }
 
-static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
- APValue &Result, const InitListExpr *ILE,
- QualType AllocType) {
-  assert(!ILE->isValueDependent());
-  assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
+static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This,
+ APValue &Result, const Expr *Init,
+ QualType AllocType) {
+  assert(!Init->isValueDependent());
+  assert(Init->isPRValue() && 

[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-03 Thread Macro Terra via cfe-commits

https://github.com/hongtaihu updated 
https://github.com/llvm/llvm-project/pull/228675

>From 7a3f90b3e00a2151b0402fd9433dfbe11066cca6 Mon Sep 17 00:00:00 2001
From: hongtaihu <[email protected]>
Date: Sat, 3 Oct 2026 15:40:16 +0800
Subject: [PATCH 1/2] [clang][constexpr] Evaluate parenthesized array new with
 the allocated bound

When evaluating an array new-expression, the actual allocation bound can differ 
from the bound recorded in the initializer's type. This occurs when the array 
bound is a function parameter whose value becomes known during constant 
evaluation.

The branch handling this difference assumes that the initializer is an 
`InitListExpr`. However, parenthesized array initialization produces a 
`CXXParenListInitExpr`, causing the cast to assert.

For example, this valid C++20 program triggers the assertion:

```cpp
constexpr bool f(unsigned n) {
  int *p = new int[n](5);
  delete[] p;
  return true;
}
static_assert(f(3));
```

Preserve the initializer as an `Expr` and dispatch according to its actual node 
type. For parenthesized initialization, reuse the existing array evaluation 
logic with the actual allocation type. This also ensures that the remaining 
elements are initialized using the correct array bound.

Add regression coverage for:

- Parenthesized integer array initialization with different bounds.
- Class array initialization with default construction of remaining elements.
- Existing braced string initialization.

Validation on the existing local assertions build, before rebasing onto current 
main: the regression passes in C++20 and C++23 modes. The original malformed 
input produces normal diagnostics without crashing in both syntax-only and 
assembly compilation modes.

Fixes #204757

Assisted by Codex
---
 clang/lib/AST/ExprConstant.cpp| 38 ++
 .../constexpr-array-new-paren-init.cpp| 39 +++
 2 files changed, 60 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/SemaCXX/constexpr-array-new-paren-init.cpp

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 3f295f35d1361..30116acfe5716 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10963,9 +10963,9 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
   }
 }
 
-static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
- APValue &Result, const InitListExpr *ILE,
- QualType AllocType);
+static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This,
+ APValue &Result, const Expr *Init,
+ QualType AllocType);
 static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
   APValue &Result,
   const CXXConstructExpr *CCE,
@@ -11035,7 +11035,7 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
   }
 
   const Expr *Init = E->getInitializer();
-  const InitListExpr *ResizedArrayILE = nullptr;
+  const Expr *ResizedArrayInit = nullptr;
   const CXXConstructExpr *ResizedArrayCCE = nullptr;
   bool ValueInit = false;
 
@@ -11104,10 +11104,11 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
 return false;
   }
 
-  // If the sizes differ, we must have an initializer list, and we need
-  // special handling for this case when we initialize.
+  // Array initialization can use either braces or parentheses. If the
+  // sizes differ, evaluate it with the allocated bound rather than the
+  // bound used when checking the initializer.
   if (InitBound != AllocBound)
-ResizedArrayILE = cast(Init);
+ResizedArrayInit = Init;
 }
 
 AllocType = Info.Ctx.getConstantArrayType(AllocType, ArrayBound, nullptr,
@@ -11205,9 +11206,8 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
 ImplicitValueInitExpr VIE(AllocType);
 if (!EvaluateInPlace(*Val, Info, Result, &VIE))
   return false;
-  } else if (ResizedArrayILE) {
-if (!EvaluateArrayNewInitList(Info, Result, *Val, ResizedArrayILE,
-  AllocType))
+  } else if (ResizedArrayInit) {
+if (!EvaluateArrayNewInit(Info, Result, *Val, ResizedArrayInit, AllocType))
   return false;
   } else if (ResizedArrayCCE) {
 if (!EvaluateArrayNewConstructExpr(Info, Result, *Val, ResizedArrayCCE,
@@ -15636,14 +15636,18 @@ static bool EvaluateArray(const Expr *E, const LValue 
&This,
   return ArrayExprEvaluator(Info, This, Result).Visit(E);
 }
 
-static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
- APValue &Result, const InitListExpr *ILE,
- QualType AllocType) {
-  assert(!ILE->isValueDependent());
-  assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
+static 

[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-03 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff origin/main HEAD --extensions cpp -- 
clang/test/SemaCXX/constexpr-array-new-paren-init.cpp 
clang/lib/AST/ExprConstant.cpp --diff_from_common_commit
``

:warning:
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing `origin/main` to the base branch/commit you want to compare against.
:warning:





View the diff from clang-format here.


``diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 30116acfe..d7a162205 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10963,9 +10963,8 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
   }
 }
 
-static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This,
- APValue &Result, const Expr *Init,
- QualType AllocType);
+static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This, APValue &Result,
+ const Expr *Init, QualType AllocType);
 static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
   APValue &Result,
   const CXXConstructExpr *CCE,
@@ -15636,9 +15635,8 @@ static bool EvaluateArray(const Expr *E, const LValue 
&This,
   return ArrayExprEvaluator(Info, This, Result).Visit(E);
 }
 
-static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This,
- APValue &Result, const Expr *Init,
- QualType AllocType) {
+static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This, APValue &Result,
+ const Expr *Init, QualType AllocType) {
   assert(!Init->isValueDependent());
   assert(Init->isPRValue() && Init->getType()->isArrayType() &&
  "not an array prvalue");

``




https://github.com/llvm/llvm-project/pull/228675
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][constexpr] Evaluate parenthesized array new with the allocated bound (PR #228675)

2026-10-03 Thread Macro Terra via cfe-commits

https://github.com/hongtaihu created 
https://github.com/llvm/llvm-project/pull/228675

When evaluating an array new-expression, the actual allocation bound can differ 
from the bound recorded in the initializer's type. This occurs when the array 
bound is a function parameter whose value becomes known during constant 
evaluation.

The branch handling this difference assumes that the initializer is an 
`InitListExpr`. However, parenthesized array initialization produces a 
`CXXParenListInitExpr`, causing the cast to assert.

For example, this valid C++20 program triggers the assertion:

```cpp
constexpr bool f(unsigned n) {
  int *p = new int[n](5);
  delete[] p;
  return true;
}
static_assert(f(3));
```

Preserve the initializer as an `Expr` and dispatch according to its actual node 
type. For parenthesized initialization, reuse the existing array evaluation 
logic with the actual allocation type. This also ensures that the remaining 
elements are initialized using the correct array bound.

Add regression coverage for:

- Parenthesized integer array initialization with different bounds.
- Class array initialization with default construction of remaining elements.
- Existing braced string initialization.

Validation on the existing local assertions build, before rebasing onto current 
main: the regression passes in C++20 and C++23 modes. The original malformed 
input produces normal diagnostics without crashing in both syntax-only and 
assembly compilation modes.

Fixes #204757

Assisted by Codex


>From 7a3f90b3e00a2151b0402fd9433dfbe11066cca6 Mon Sep 17 00:00:00 2001
From: hongtaihu <[email protected]>
Date: Sat, 3 Oct 2026 15:40:16 +0800
Subject: [PATCH] [clang][constexpr] Evaluate parenthesized array new with the
 allocated bound

When evaluating an array new-expression, the actual allocation bound can differ 
from the bound recorded in the initializer's type. This occurs when the array 
bound is a function parameter whose value becomes known during constant 
evaluation.

The branch handling this difference assumes that the initializer is an 
`InitListExpr`. However, parenthesized array initialization produces a 
`CXXParenListInitExpr`, causing the cast to assert.

For example, this valid C++20 program triggers the assertion:

```cpp
constexpr bool f(unsigned n) {
  int *p = new int[n](5);
  delete[] p;
  return true;
}
static_assert(f(3));
```

Preserve the initializer as an `Expr` and dispatch according to its actual node 
type. For parenthesized initialization, reuse the existing array evaluation 
logic with the actual allocation type. This also ensures that the remaining 
elements are initialized using the correct array bound.

Add regression coverage for:

- Parenthesized integer array initialization with different bounds.
- Class array initialization with default construction of remaining elements.
- Existing braced string initialization.

Validation on the existing local assertions build, before rebasing onto current 
main: the regression passes in C++20 and C++23 modes. The original malformed 
input produces normal diagnostics without crashing in both syntax-only and 
assembly compilation modes.

Fixes #204757

Assisted by Codex
---
 clang/lib/AST/ExprConstant.cpp| 38 ++
 .../constexpr-array-new-paren-init.cpp| 39 +++
 2 files changed, 60 insertions(+), 17 deletions(-)
 create mode 100644 clang/test/SemaCXX/constexpr-array-new-paren-init.cpp

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 3f295f35d1361..30116acfe5716 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10963,9 +10963,9 @@ bool PointerExprEvaluator::VisitBuiltinCallExpr(const 
CallExpr *E,
   }
 }
 
-static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
- APValue &Result, const InitListExpr *ILE,
- QualType AllocType);
+static bool EvaluateArrayNewInit(EvalInfo &Info, LValue &This,
+ APValue &Result, const Expr *Init,
+ QualType AllocType);
 static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
   APValue &Result,
   const CXXConstructExpr *CCE,
@@ -11035,7 +11035,7 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
   }
 
   const Expr *Init = E->getInitializer();
-  const InitListExpr *ResizedArrayILE = nullptr;
+  const Expr *ResizedArrayInit = nullptr;
   const CXXConstructExpr *ResizedArrayCCE = nullptr;
   bool ValueInit = false;
 
@@ -11104,10 +11104,11 @@ bool PointerExprEvaluator::VisitCXXNewExpr(const 
CXXNewExpr *E) {
 return false;
   }
 
-  // If the sizes differ, we must have an initializer list, and we need
-  // special handling for this case when we initialize.
+  // Array initialization can use either