Skip to content

Commit 563987c

Browse files
authored
cgen: restore old behaviour for struct Name{field &C.FILE};i:=Name{};unsafe{i.free()} (fix #25325) (#25327)
1 parent 14ef765 commit 563987c

4 files changed

Lines changed: 24 additions & 2 deletions

File tree

‎vlib/v/gen/c/auto_free_methods.v‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn (mut g Gen) gen_free_method(typ ast.Type) string {
6262

6363
match mut sym.info {
6464
ast.Struct {
65-
g.gen_free_for_struct(objtyp, sym.info, styp, fn_name)
65+
g.gen_free_for_struct(objtyp, sym.info, styp, fn_name, sym.is_builtin())
6666
}
6767
ast.Array {
6868
g.gen_free_for_array(sym.info, styp, fn_name)
@@ -105,7 +105,11 @@ fn (mut g Gen) gen_free_for_interface(sym ast.TypeSymbol, info ast.Interface, st
105105
fn_builder.writeln('}')
106106
}
107107

108-
fn (mut g Gen) gen_free_for_struct(typ ast.Type, info ast.Struct, styp string, fn_name string) {
108+
fn (mut g Gen) gen_free_for_struct(typ ast.Type, info ast.Struct, styp string, ofn_name string, sym_is_builtin bool) {
109+
mut fn_name := ofn_name
110+
if sym_is_builtin {
111+
fn_name = 'builtin__${fn_name}'
112+
}
109113
g.definitions.writeln('${g.static_non_parallel}void ${fn_name}(${styp}* it);')
110114
mut fn_builder := strings.new_builder(128)
111115
defer {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
static void main__Abc_free(main__Abc* it);
2+
static void builtin__FILE_free(FILE* it);
3+
static void builtin__FILE_free(FILE* it) {
4+
static void main__Abc_free(main__Abc* it) {
5+
builtin__FILE_free(&(it->myfile));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Abc{
2+
myfile: &nil
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
struct Abc {
2+
myfile &C.FILE = unsafe { nil }
3+
}
4+
5+
@[manualfree]
6+
fn main() {
7+
a := Abc{}
8+
println(a)
9+
unsafe { a.free() }
10+
}

0 commit comments

Comments
 (0)