1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix struct field name generation in auto free (#15440)

This commit is contained in:
ChAoS_UnItY 2022-08-17 14:11:58 +08:00 committed by GitHub
parent 374186f1f7
commit 70f466460f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View File

@ -72,6 +72,7 @@ fn (mut g Gen) gen_free_for_struct(info ast.Struct, styp string, fn_name string)
}
fn_builder.writeln('void ${fn_name}($styp* it) {')
for field in info.fields {
field_name := c_name(field.name)
sym := g.table.sym(g.unwrap_generic(field.typ))
if sym.kind !in [.string, .array, .map, .struct_] {
@ -88,9 +89,9 @@ fn (mut g Gen) gen_free_for_struct(info ast.Struct, styp string, fn_name string)
g.gen_free_method(field.typ)
}
if is_shared {
fn_builder.writeln('\t${field_styp_fn_name}(&(it->$field.name->val));')
fn_builder.writeln('\t${field_styp_fn_name}(&(it->$field_name->val));')
} else {
fn_builder.writeln('\t${field_styp_fn_name}(&(it->$field.name));')
fn_builder.writeln('\t${field_styp_fn_name}(&(it->$field_name));')
}
}
fn_builder.writeln('}')

View File

@ -0,0 +1,6 @@
&Class{
class: 'abc'
register: '123'
namespace: '456'
normal: 'xyz'
}

View File

@ -0,0 +1,15 @@
// Test, that the autogenerated `free` method compiles,
// on a struct that has field names that are C/C++ keywords.
struct Class {
class string
register string
namespace string
normal string
}
fn main() {
class := &Class{'abc', '123', '456', 'xyz'}
println(class)
class.free()
}