fix(cgen): fix address-of and as_cast pointer codegen#27481
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. |
|
@codex review |
There was a problem hiding this comment.
💡 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".
|
Addressed the latest review feedback in 528f9ed:
Validation:
|
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 528f9edd0b
ℹ️ 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".
| obj_expr := '(${expr_str})${dot}_${payload_sym.cname}' | ||
| tag_expr := '(${expr_str})${dot}_typ' | ||
| g.write_as_cast_call_start(styp, sym) | ||
| g.write_as_cast_call_start(styp, sym, is_direct_amp) |
There was a problem hiding this comment.
Hoist direct address casts out of statement expressions
When &(expr as T) reaches this temporary-evaluation branch with is_direct_amp set, the emitted value becomes a T* pointing into the branch-local ${tmp_var} declared inside the GCC/Clang statement expression. That temporary dies at the end of the expression, so cases not covered by the direct-call special case above, such as p := &((get_wrapper().sum_field as Foo)) or an if/match operand, leave p dangling while still compiling. The MSVC path already hoists to the enclosing statement; direct address casts that need a temp need the same lifetime on the statement-expression path.
Useful? React with 👍 / 👎.
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.