mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix duplicate auto generation of free methods, add test
This commit is contained in:
parent
ee6b23c2a7
commit
0cc63107ff
|
@ -7,15 +7,14 @@ import strings
|
||||||
|
|
||||||
fn (mut g Gen) get_free_method(typ ast.Type) string {
|
fn (mut g Gen) get_free_method(typ ast.Type) string {
|
||||||
g.autofree_methods[typ] = true
|
g.autofree_methods[typ] = true
|
||||||
styp := g.typ(typ).replace('*', '')
|
|
||||||
mut sym := g.table.sym(g.unwrap_generic(typ))
|
mut sym := g.table.sym(g.unwrap_generic(typ))
|
||||||
mut fn_name := styp_to_free_fn_name(styp)
|
|
||||||
if mut sym.info is ast.Alias {
|
if mut sym.info is ast.Alias {
|
||||||
if sym.info.is_import {
|
if sym.info.is_import {
|
||||||
sym = g.table.sym(sym.info.parent_type)
|
sym = g.table.sym(sym.info.parent_type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
styp := g.typ(typ).replace('*', '')
|
||||||
|
fn_name := styp_to_free_fn_name(styp)
|
||||||
if sym.has_method_with_generic_parent('free') {
|
if sym.has_method_with_generic_parent('free') {
|
||||||
return fn_name
|
return fn_name
|
||||||
}
|
}
|
||||||
|
@ -30,21 +29,23 @@ fn (mut g Gen) gen_free_methods() {
|
||||||
|
|
||||||
fn (mut g Gen) gen_free_method(typ ast.Type) string {
|
fn (mut g Gen) gen_free_method(typ ast.Type) string {
|
||||||
styp := g.typ(typ).replace('*', '')
|
styp := g.typ(typ).replace('*', '')
|
||||||
mut sym := g.table.sym(g.unwrap_generic(typ))
|
|
||||||
mut fn_name := styp_to_free_fn_name(styp)
|
mut fn_name := styp_to_free_fn_name(styp)
|
||||||
if typ in g.generated_free_methods {
|
deref_typ := typ.set_nr_muls(0)
|
||||||
|
if deref_typ in g.generated_free_methods {
|
||||||
return fn_name
|
return fn_name
|
||||||
}
|
}
|
||||||
g.generated_free_methods[typ] = true
|
g.generated_free_methods[deref_typ] = true
|
||||||
|
|
||||||
|
mut sym := g.table.sym(g.unwrap_generic(typ))
|
||||||
if mut sym.info is ast.Alias {
|
if mut sym.info is ast.Alias {
|
||||||
if sym.info.is_import {
|
if sym.info.is_import {
|
||||||
sym = g.table.sym(sym.info.parent_type)
|
sym = g.table.sym(sym.info.parent_type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if sym.has_method_with_generic_parent('free') {
|
if sym.has_method_with_generic_parent('free') {
|
||||||
return fn_name
|
return fn_name
|
||||||
}
|
}
|
||||||
|
|
||||||
match mut sym.info {
|
match mut sym.info {
|
||||||
ast.Struct {
|
ast.Struct {
|
||||||
g.gen_free_for_struct(sym.info, styp, fn_name)
|
g.gen_free_for_struct(sym.info, styp, fn_name)
|
||||||
|
|
38
vlib/v/tests/valgrind/struct_of_array_of_same_struct.v
Normal file
38
vlib/v/tests/valgrind/struct_of_array_of_same_struct.v
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
struct Abc {
|
||||||
|
mut:
|
||||||
|
name string
|
||||||
|
children []Abc
|
||||||
|
}
|
||||||
|
|
||||||
|
[manualfree]
|
||||||
|
fn main() {
|
||||||
|
mut a := &Abc{}
|
||||||
|
a.name = 'aaa'
|
||||||
|
a.children = [
|
||||||
|
Abc{
|
||||||
|
name: 'xyz'
|
||||||
|
children: [
|
||||||
|
Abc{
|
||||||
|
name: 'xx'
|
||||||
|
},
|
||||||
|
Abc{
|
||||||
|
name: 'yy'
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
Abc{
|
||||||
|
name: 'def'
|
||||||
|
children: [
|
||||||
|
Abc{
|
||||||
|
name: 'dd'
|
||||||
|
},
|
||||||
|
Abc{
|
||||||
|
name: 'ee'
|
||||||
|
},
|
||||||
|
]
|
||||||
|
},
|
||||||
|
]
|
||||||
|
dump(a)
|
||||||
|
unsafe { a.free() }
|
||||||
|
unsafe { free(a) }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user