v
v copied to clipboard
v: fix mutable option
Fix #18818
struct Abc {
a int
}
fn foo(mut baz ?Abc) {
baz = Abc{a:3}
println(baz)
dump(baz)
}
fn main() {
mut a := ?Abc{
a:2
}
dump(a)
foo(mut a)
println('--')
dump(a)
}
🤖 Generated by Copilot at 7d84ddf
This pull request fixes the C code generation for option types that are passed as mutable parameters to functions. It adds a new type flag option_mut_param_t to mark these types and adjusts the C type names, assignments, and string representations accordingly. It also fixes some bugs in the code generation for option expressions and function calls.
🤖 Generated by Copilot at 7d84ddf
- Add a new type flag
option_mut_param_tto mark option types that are passed as mutable parameters or return values (link)- Use
memcpyto copy the right expression to thedatafield of the option struct in assignments (link, link, link) - Remove the asterisk from the C type name and the variable declaration, since the option struct already contains a pointer to the data type (link, link, link, link)
- Skip the double dereference or the reference of the option value, depending on the context, since the option struct already contains a pointer to the data type (link, link, link, link, link)
- Add a dereference to the option value in the
dump_exprmethod, which is used for debugging and error reporting (link)
- Use
- Modify the
parse_paramandparse_return_typemethods invlib/v/parser/fn.vto set theoption_mut_param_tflag on option types without any pointer modifiers (link, link) - Skip the generation of option types that end with an asterisk in the
gen_option_typemethod invlib/v/gen/c/cgen.v, since they are already handled by theoption_mut_param_tflag (link)
./v -autofree -o v2 cmd/v is failing for some reason.
The CI is passing, can it be reviewed/merged now?
The CI is passing, can it be reviewed/merged now?
It was missing a parameter validation, checking if everything is ok yet.
Closing for inactivity. The master branch diverged a lot from this, and I do not see a good way forward for it, that is more efficient than just re-implementing the changes here over the current master.
Lack of running v fmt strikes again. :-)
Is it ready for review now?
Is it ready for review now?
Yes.