Skip to content

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

Open
guweigang wants to merge 1 commit into
vlang:masterfrom
guweigang:fix-toml-cc
Open

fix(cgen): fix address-of and as_cast pointer codegen#27481
guweigang wants to merge 1 commit 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.

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.

1 participant