Cgen: generic heap sum with fixed-array variant emits invalid C initializer (musl JSON2 R401)

Open
#32,773 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
28/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
c
Domain
compilers

Research direction

Start with vlib/v/gen/c/struct.v:gen_heap_struct_init, the new generic heap-sum literal branch that calls g.gen_expr for lowered variant fields, and compare it with gen_lowered_sum_field_value in vlib/v/gen/c/sumtype.v, which already emits brace-only fixed-array initializers. Reproduce with vlib/json2/tests/legacy_json_edge_cases_test.v and a generic &[2]Item | int heap-sum default. Done means that test compiles on musl/GCC without the invalid array initializer and the existing JSON2 assertions still pass.

Written by the indexing model from the issue text.

Description

R401 — executed Ubuntu musl C-generation regression in an older repair PR

Original executed evidence: PR #32734 Ubuntu musl job 137454807174, completed 2026-11-23 03:01:54 UTC, head 4280999e8dd86a846543a4e66b10acc346255305. The direct R376 cached-dispatch regression passed after building musl SQLite/OpenSSL, as did formatting and test-tool smoke steps. The unchanged full ./v -silent test-self vlib then failed one real test after 634 passed / 126 skipped:

vlib/json2/tests/legacy_json_edge_cases_test.v
src.c:11969:89: error: array initialized from non-constant array expression
FixedItems* result = (FixedItems*)memdup(&(FixedItems){.typ = 1,
 ._2ArrayVariantItem = (main__ArrayVariantItem[2]){(main__ArrayVariantItem){0}, (main__ArrayVariantItem){0}}}, sizeof(FixedItems));

VTEST_FAIL_FAST=1; no GitHub job timeout, intentionally failing harness fixture, or dependency setup failure. The direct original R376 test succeeded and must not be conflated with the different JSON2 failure.

Historical comparison: merged #31750, squash e41a2271177f6863b663496c4722afdabca4cfe8, had already fixed the same GCC complaint in ordinary sum defaults using brace-only fixed-array initializers; its original regression was independently tested. Current vlib/v/gen/c/sumtype.v:gen_lowered_sum_field_value still contains the brace-only logic. However commit 1ce8aef1 introduced a new generic heap-sum literal branch in vlib/v/gen/c/struct.v:gen_heap_struct_init that emits g.gen_expr for lowered variant fields, bypassing the existing brace-only sum-field helper. That path plausibly reintroduces the invalid typed array initialization. The precise introduction is supported by this code-path comparison but needs native before/after V reproduction.

Independent limited check: A minimal GCC11 C reproducer for .field = (Item[2]){...} exits 1 with the exact diagnostic, and replacing that initializer with .field = {...} compiles (exit 0). This is C-only evidence, not a V compiler regression validation.

Next actions: Reuse gen_lowered_sum_field_value for heap-sum variant fields, preserving sum's own type and tag. Add generic &[2]Item | int heap-sum default regression without weakening existing JSON2 assertions. Rebuild V, format and run original vlib/json2/tests/legacy_json_edge_cases_test.v, targeted sumtype tests, compiler diagnostics, Cgen tests and original musl/Clang/GCC controls. No merge until executed corrected native V validation. Current applicability: Cgen source unchanged at the latest master reviewed; prior #31750 merge alone is not resolution.

Status: investigating / fix candidate, linked to R376 gate but separate normalized signature.

[!NOTE]
You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Dominant language
V
Stars
38.1k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
682

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from vlang/v

All issues in vlang/v

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.