fix(cgen): fix address-of and as_cast pointer codegen#27481
Open
guweigang wants to merge 1 commit into
Open
Conversation
|
Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes a C code generation issue where combining the address-of (
&) operator andas_castexpressions (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:Gen.expr:&) operator codegen path. When the right-hand side is anAsCastexpression, we check if it evaluates to a pointer casting using the newas_cast_will_use_ptr()helper. If so, we avoid writing incorrect prefix operators (like double address-of or invalid dereference syntax).Gen.write_as_cast_call_start: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.Gen.as_cast_will_use_ptr:AsCastnode will result in a pointer-based representation under the hood (e.g. casting interfaces or sum types with differing base types).Checklist
v fmt -w.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.