Skip to content

v3: use emitted fixed-array names in function pointers - #28902

Merged
medvednikov merged 7 commits into
vlang:masterfrom
antono2:fix/v3-backed-enum-array-fn-ptr
Sep 30, 2026
Merged

medvednikov merged 7 commits into
vlang:masterfrom
antono2:fix/v3-backed-enum-array-fn-ptr

Conversation

@antono3

@antono3 antono3 commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

V3 emits fixed-array typedefs using the backed enum's C name, while several function-pointer paths use a different TypeChecker encoding and refer to an undeclared Array_fixed_int_2. Alias-only callback declarations can also omit their required fixed-array typedef.

Use the emitted fixed-array name consistently in function-pointer keys and collect arrays reachable through type aliases. Resolve named callback aliases in sizeof through the same key.

This supersedes #28867: it covers that PR's imported sizeof(Callback) case while retaining the backed-enum typedef name, instead of changing fixed-array naming globally. It is independent of #28868 (imported constants in array lengths).

Verification against current master

Rebased onto 6c359c228241b44f9d9a942696e83b513931e532 on 2026-09-26.

  • Both imported callback regressions fail on unmodified master.
  • Both pass together with this patch under strict V3 (-new-compiler), including native compilation, execution, and emitted typedef checks.
  • ./v self, formatting verification, and git diff --check pass.
  • The full fixed_array_typedef_codegen_test.v file passes (all seven test functions).
  • VJOBS=4 ./v -new-compiler test vlib/v/gen/c/ passes: 28/28 test files.
  • Both focused callback regressions pass again after the final pointer-preservation refinement. A separate callback-pointer alias compiles and runs, and its generated sizeof expression retains the extra *.

Latest tested head: 45ad1ea2fae51aa54e32545fa024aeba96dccdb1.

The previously failing twelve formatter files now pass on master. The unrelated anonymous-struct compiler regression still fails on unmodified master in its genuinely anonymous parameter case.

@medvednikov

Copy link
Copy Markdown
Member

@codex review

@medvednikov

Copy link
Copy Markdown
Member

@codex review

@medvednikov medvednikov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed b10ec41, including typedef collection, alias emission, return-wrapper registration, and the function-parameter emitter. The new alias-only and sizeof(Callback) cases are useful, but the emitted-name normalization is not yet used by every callback-signature path; see the inline comment.

Please extend the regression to pass and invoke a callback, in addition to declaring/measuring its type. That exercises the parameter declaration and the call ABI rather than only the alias's typedef.

This is a source review; I have not built V3 or executed these tests in this environment.

Comment thread vlib/v/gen/c/fn.v
@medvednikov

Copy link
Copy Markdown
Member

@codex review

@medvednikov

Copy link
Copy Markdown
Member

@codex review

@medvednikov

Copy link
Copy Markdown
Member

@codex review

@medvednikov

Copy link
Copy Markdown
Member

Callback parameters now use normalized type names, including nested signatures. The added regressions actually invoke V and C callbacks and cover aliases, fixed-array pointers, and Option/Result callback returns.

@medvednikov
medvednikov merged commit 47c6c5e into vlang:master Sep 30, 2026

Copy link
Copy Markdown
Member

Reviewed head 9897bbaf6ccce9da3b48164d198647c174c553b2, ignoring CI. The earlier function-parameter normalization finding is addressed. Since the PR is now merged, here is one remaining in-scope follow-up from source inspection:

[P2] Normalize callback signatures when emitting struct fields too

preseed_fn_ptr_type now registers g.fn_ptr_type_key(typ), but write_struct_field in the same file still uses g.tc.c_type(raw_field_type) for ordinary function-valued fields and g.tc.c_type(field_type.base_type) for pointer-to-callback fields. soa_field_c_type has the same fallback. The C-ABI override lookup does not normalize an ordinary V callback field.

Consequently, putting the callback from the new regression into a struct still requests a different typedef key from the one preseeded here: the field-emission path encodes [2]fixture.Mode as Array_fixed_int_2, while fixed-array collection emits Array_fixed_fixture__Mode_2. resolve_fn_ptr_type registers the supplied encoding without repairing it, and emit_fn_ptr_typedef uses that array name unchanged. The generated declarations therefore still have an undeclared-type problem for this use of the callback.

Please route those ordinary field fallbacks through the normalized function-type conversion too, preserving any explicit C-ABI override and pointer depth. Extend test_backed_enum_callbacks_are_passed_and_invoked with a stored callback, for example:

struct Holder {
    cb Callback
}

and, inside its generated main:

holder := Holder{cb: first}
assert holder.cb([fixture.Mode.one, fixture.Mode.two]!) == 26

The existing generated-C assertion rejecting Array_fixed_int_2 should cover this path as well. A pointer-to-callback field is worth covering alongside it.

This is a remaining normalization gap, not a claim that the unchanged field-emission code was introduced by this PR. I have not built V3 or executed the regressions locally: this environment has no V compiler and the repository clone failed because GitHub could not be resolved.

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.

2 participants