Skip to content

fix(cgen): fix address-of and as_cast pointer codegen#27481

Open
guweigang wants to merge 2 commits into
vlang:masterfrom
guweigang:fix-toml-cc
Open

fix(cgen): fix address-of and as_cast pointer codegen#27481
guweigang wants to merge 2 commits into
vlang:masterfrom
guweigang:fix-toml-cc

Conversation

@guweigang

Copy link
Copy Markdown
Contributor

Summary

This PR fixes a C code generation issue where combining the address-of (&) operator and as_cast expressions (specifically during smartcasts) results in incorrect pointer casting and dereferencing syntax in the generated C output.

Details of Changes

In vlib/v/gen/c/cgen.v:

  1. Gen.expr:
    • Enhanced the address-of (&) operator codegen path. When the right-hand side is an AsCast expression, we check if it evaluates to a pointer casting using the new as_cast_will_use_ptr() helper. If so, we avoid writing incorrect prefix operators (like double address-of or invalid dereference syntax).
  2. Gen.write_as_cast_call_start:
    • Added a check for g.is_amp (address-of context) to ensure the compiler generates (Type*) pointer casts rather than standard value casts when smartcasting is performed in an address-of context.
  3. Gen.as_cast_will_use_ptr:
    • Introduced this helper method to accurately determine if an AsCast node will result in a pointer-based representation under the hood (e.g. casting interfaces or sum types with differing base types).

Checklist

  • I have formatted my V files with v fmt -w.
  • All existing tests pass.

Tests Run

Rebuilt the V compiler and verified that all compiler and cgen tests pass:

./v -g -keepc -o ./vnew cmd/v
./vnew -silent test vlib/v/gen/c/

All tests passed successfully on local machine.

@guweigang guweigang marked this pull request as ready for review June 17, 2026 14:12
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@medvednikov

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 6045f791fb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread vlib/v/gen/c/cgen.v
Comment thread vlib/v/gen/c/cgen.v Outdated
@guweigang

Copy link
Copy Markdown
Contributor Author

Addressed the latest review feedback in 528f9ed:

  • keep address-of semantics for temporary-backed &(call() as Foo) casts
  • scope pointer-mode codegen to the direct as cast so selectors such as &((sum as Foo).field) remain valid
  • add regression coverage for interface casts, sumtype selectors, and call-backed casts

Validation:

  • macOS: casts tests, 36/36 passed
  • Ubuntu ARM64 VM with VFLAGS="-gc none -cc gcc": 35 passed, 1 JS-only test skipped
  • macOS compiler error and C output suites 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.

2 participants