From 054bd6749039987694a838e70c0aee286a0ab4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Thu, 22 Jul 2021 00:37:49 +0200 Subject: [PATCH] cgen: don't change field name of c structs (#10894) --- vlib/v/gen/c/cgen.v | 9 +++++---- .../c_struct_free/c_struct_free_property_test.v | 13 +++++++++++++ vlib/v/tests/c_struct_free/free_struct.c | 3 +++ 3 files changed, 21 insertions(+), 4 deletions(-) create mode 100644 vlib/v/tests/c_struct_free/c_struct_free_property_test.v create mode 100644 vlib/v/tests/c_struct_free/free_struct.c diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index c11261b945..d437999af4 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3649,7 +3649,8 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { if node.expr_type == 0 { verror('cgen: SelectorExpr | expr_type: 0 | it.expr: `$node.expr` | field: `$node.field_name` | file: $g.file.path | line: $node.pos.line_nr') } - g.write(c_name(node.field_name)) + field_name := if sym.language == .v { c_name(node.field_name) } else { node.field_name } + g.write(field_name) if sum_type_deref_field != '' { g.write('$sum_type_dot$sum_type_deref_field)') } @@ -5112,7 +5113,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { for i, field in struct_init.fields { inited_fields[field.name] = i if sym.kind != .struct_ { - field_name := c_name(field.name) + field_name := if sym.language == .v { c_name(field.name) } else { field.name } g.write('.$field_name = ') if field.typ == 0 { g.checker_bug('struct init, field.typ is 0', field.pos) @@ -5197,7 +5198,7 @@ fn (mut g Gen) struct_init(struct_init ast.StructInit) { } if field.name in inited_fields { sfield := struct_init.fields[inited_fields[field.name]] - field_name := c_name(sfield.name) + field_name := if sym.language == .v { c_name(field.name) } else { field.name } if sfield.typ == 0 { continue } @@ -5289,7 +5290,7 @@ fn (mut g Gen) zero_struct_field(field ast.StructField) bool { return false } } - field_name := c_name(field.name) + field_name := if sym.language == .v { c_name(field.name) } else { field.name } g.write('.$field_name = ') if field.has_default_expr { if sym.kind in [.sum_type, .interface_] { diff --git a/vlib/v/tests/c_struct_free/c_struct_free_property_test.v b/vlib/v/tests/c_struct_free/c_struct_free_property_test.v new file mode 100644 index 0000000000..c3981a15c5 --- /dev/null +++ b/vlib/v/tests/c_struct_free/c_struct_free_property_test.v @@ -0,0 +1,13 @@ +#flag -I @VEXEROOT/vlib/v/tests/c_struct_free +#include "free_struct.c" + +struct C.foo { +mut: + free int +} + +fn test_free_property_on_c_struct() { + mut a := C.foo{0} + a.free = 2 + assert a.free == 2 +} diff --git a/vlib/v/tests/c_struct_free/free_struct.c b/vlib/v/tests/c_struct_free/free_struct.c new file mode 100644 index 0000000000..d450c9010b --- /dev/null +++ b/vlib/v/tests/c_struct_free/free_struct.c @@ -0,0 +1,3 @@ +struct foo { + int free; +};