Skip to content

Count the characters, not the bytes, of a string literal initializer of an array new-expression in constant evaluation [GH #501] - #502

Merged
daveedvdv-nvidia merged 1 commit into
mainfrom
fix/array-new-wide-string
Nov 10, 2026
Merged

daveedvdv-nvidia merged 1 commit into
mainfrom
fix/array-new-wide-string

Conversation

@daveedvdv-nvidia

@daveedvdv-nvidia daveedvdv-nvidia commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #501.

During constant evaluation, the check that the number of elements allocated by an array new-expression suffices for its initializer took the length of a string literal initializer in bytes, rather than in characters. So, for a character type wider than one byte (e.g., wchar_t, char16_t, or char32_t), an array large enough for the literal could be diagnosed as too small:

constexpr int f(int n) {
  auto p = new wchar_t[n]{L"ab"};
  int r = p[1];
  delete[] p;
  return r;
}
static_assert(f(3) == L'b');  // Previously an error; now okay.

The interpreter now divides the byte length by the size of the element type. The new tests cover wchar_t, char16_t, char32_t, and char8_t (regressions/gh-501), and confirm that a too-short array is still diagnosed (regressions/gh-501-neg).

Summary by CodeRabbit

  • Bug Fixes
    • Corrected compile-time evaluation of dynamically allocated arrays initialized with string literals. Array sizes are now checked in elements rather than bytes, avoiding incorrect size errors for wide-character strings.
    • Added regression coverage for wchar_t, char8_t, char16_t, and char32_t string initializers, including cases where the allocated array is too small.

…of an array new-expression in constant evaluation [GH #501]
@daveedvdv-nvidia
daveedvdv-nvidia requested a review from a team November 9, 2026 20:01
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The constant-evaluation array initializer check now compares the allocated element count with the string’s character count. C++20 regression tests cover wide string literals with sufficient and insufficient allocation sizes.

Changes: Constant-evaluation array initialization

Layer / File(s) Summary
Count string initializer elements
src/interpret.c
The initializer element count is now calculated by dividing the string’s byte length by the array element size.
Test string literal array sizes
tests/tests/regressions/gh-501.sft.cpp, tests/tests/regressions/gh-501-neg.sft.cpp, tests/tests/regressions/.gh-501.rto/default.1.1.txt, tests/tests/regressions/.gh-501-neg.rto/default.1.1.txt, src/pending_changes/gh-501-array-new-wide-string.txt
C++20 regression tests cover accepted and rejected allocation sizes for wide string literals. The test fixtures record the invocation and expected diagnostic. The pending-changes note describes the issue and an example.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested labels: bug

Suggested reviewers: wchilders-nvidia

Merge Risk: Minimal · up to 8c2d9

The change appears ready to merge after normal checks.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: count string literal initializer characters rather than bytes during constant evaluation of array new-expressions.
Linked Issues check Passed Issue #501 requires constant evaluation to compare the allocated element count with the character count of a string-literal initializer. In src/interpret.c, do_constexpr_new now divides the stored…
Out of Scope Changes check Passed The implementation, regression tests, expected outputs, and pending-change note all address issue #501. The diff contains no demonstrated unrelated changes.
Full details: Linked Issues check

Explanation

Issue #501 requires constant evaluation to compare the allocated element count with the character count of a string-literal initializer. In src/interpret.c, do_constexpr_new now divides the stored byte length by the array element type size before the capacity check. tests/tests/regressions/gh-501.sft.cpp covers wchar_t, char16_t, char32_t, and char8_t; gh-501-neg.sft.cpp and its expected output confirm that a two-element wchar_t array remains too small for L"ab".

Finishing Touches

Generate docstrings

  • Commit to this branch
  • Create a new PR
Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 73b1b0f and 8c2d918.

Files selected for processing: 6

  • src/interpret.c
  • src/pending_changes/gh-501-array-new-wide-string.txt
  • tests/tests/regressions/.gh-501-neg.rto/default.1.1.txt
  • tests/tests/regressions/.gh-501.rto/default.1.1.txt
  • tests/tests/regressions/gh-501-neg.sft.cpp
  • tests/tests/regressions/gh-501.sft.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 8c2d918cd55d:

CI success.

@daveedvdv-nvidia
daveedvdv-nvidia added this pull request to the merge queue Nov 10, 2026
Merged via the queue into main with commit 46ba939 Nov 10, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Constant evaluation rejects "new wchar_t[n]{L\"...\"}" although n is large enough

3 participants