cgen: implement IError.free() and use it for the builtin `none` const

pull/9349/head
Delyan Angelov 2021-03-17 12:31:36 +02:00
parent 2e84773ef1
commit 99cc02685b
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
2 changed files with 29 additions and 2 deletions

View File

@ -62,3 +62,27 @@ fn opt_ok(data voidptr, mut option Option, size int) {
C.memcpy(byteptr(&option.err) + sizeof(IError), data, size)
}
}
[unsafe]
pub fn (e &Error) free() {
unsafe { e.msg.free() }
}
[unsafe]
pub fn (n &None__) free() {
unsafe { n.msg.free() }
}
[typedef]
struct C.IError {
_object voidptr
}
[unsafe]
pub fn (ie &IError) free() {
unsafe {
ie.msg.free()
cie := &C.IError(ie)
C.free(cie._object)
}
}

View File

@ -975,8 +975,9 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
stmt_pos = stmt.expr.position()
}
if stmt_pos.pos == 0 {
print('autofree: first stmt pos = 0. ')
println(stmt.type_name())
$if trace_autofree ? {
println('autofree: first stmt pos = 0. $stmt.type_name()')
}
return
}
}
@ -4849,6 +4850,8 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, val string, typ ta
g.cleanups[mod].writeln('\tstring_free(&$cname);')
} else if sym.kind == .map {
g.cleanups[mod].writeln('\tmap_free(&$cname);')
} else if styp == 'IError' {
g.cleanups[mod].writeln('\tIError_free(&$cname);')
}
}
}