v icon indicating copy to clipboard operation
v copied to clipboard

v: fix mutable option

Open felipensp opened this issue 2 years ago • 3 comments

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_t to mark option types that are passed as mutable parameters or return values (link)
    • Use memcpy to copy the right expression to the data field 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_expr method, which is used for debugging and error reporting (link)
  • Modify the parse_param and parse_return_type methods in vlib/v/parser/fn.v to set the option_mut_param_t flag on option types without any pointer modifiers (link, link)
  • Skip the generation of option types that end with an asterisk in the gen_option_type method in vlib/v/gen/c/cgen.v, since they are already handled by the option_mut_param_t flag (link)

felipensp avatar Aug 09 '23 21:08 felipensp

./v -autofree -o v2 cmd/v is failing for some reason.

spytheman avatar Aug 10 '23 01:08 spytheman

The CI is passing, can it be reviewed/merged now?

spytheman avatar Aug 28 '23 09:08 spytheman

The CI is passing, can it be reviewed/merged now?

It was missing a parameter validation, checking if everything is ok yet.

felipensp avatar Sep 09 '23 12:09 felipensp

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.

spytheman avatar Apr 09 '25 04:04 spytheman

Lack of running v fmt strikes again. :-)

JalonSolov avatar May 31 '25 17:05 JalonSolov

Is it ready for review now?

spytheman avatar Jun 03 '25 07:06 spytheman

Is it ready for review now?

Yes.

felipensp avatar Jun 03 '25 12:06 felipensp