cgen: restore old behaviour for struct Name{field &C.FILE};i:=Name{};unsafe{i.free()} (fix #25325) (#25327)

This commit is contained in:
Delyan Angelov 2025-09-17 13:22:50 +03:00 committed by GitHub
parent 14ef765151
commit 563987c450
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 24 additions and 2 deletions

View File

@ -62,7 +62,7 @@ fn (mut g Gen) gen_free_method(typ ast.Type) string {
match mut sym.info {
ast.Struct {
g.gen_free_for_struct(objtyp, sym.info, styp, fn_name)
g.gen_free_for_struct(objtyp, sym.info, styp, fn_name, sym.is_builtin())
}
ast.Array {
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
fn_builder.writeln('}')
}
fn (mut g Gen) gen_free_for_struct(typ ast.Type, info ast.Struct, styp string, fn_name string) {
fn (mut g Gen) gen_free_for_struct(typ ast.Type, info ast.Struct, styp string, ofn_name string, sym_is_builtin bool) {
mut fn_name := ofn_name
if sym_is_builtin {
fn_name = 'builtin__${fn_name}'
}
g.definitions.writeln('${g.static_non_parallel}void ${fn_name}(${styp}* it);')
mut fn_builder := strings.new_builder(128)
defer {

View File

@ -0,0 +1,5 @@
static void main__Abc_free(main__Abc* it);
static void builtin__FILE_free(FILE* it);
static void builtin__FILE_free(FILE* it) {
static void main__Abc_free(main__Abc* it) {
builtin__FILE_free(&(it->myfile));

View File

@ -0,0 +1,3 @@
Abc{
myfile: &nil
}

View File

@ -0,0 +1,10 @@
struct Abc {
myfile &C.FILE = unsafe { nil }
}
@[manualfree]
fn main() {
a := Abc{}
println(a)
unsafe { a.free() }
}