This commit is contained in:
Felipe Pena 2025-09-04 09:24:46 -03:00
parent dbd5b5f56c
commit 2899a01c43
2 changed files with 24 additions and 9 deletions

View file

@ -3,7 +3,7 @@ module builtin
// Note: this file will be removed soon // Note: this file will be removed soon
// byteptr.vbytes() - makes a V []u8 structure from a C style memory buffer. Note: the data is reused, NOT copied! // byteptr.vbytes() - makes a V []u8 structure from a C style memory buffer. Note: the data is reused, NOT copied!
@[unsafe] @[reused; unsafe]
pub fn (data byteptr) vbytes(len int) []u8 { pub fn (data byteptr) vbytes(len int) []u8 {
return unsafe { voidptr(data).vbytes(len) } return unsafe { voidptr(data).vbytes(len) }
} }
@ -11,7 +11,7 @@ pub fn (data byteptr) vbytes(len int) []u8 {
// vstring converts a C style string to a V string. Note: the string data is reused, NOT copied. // vstring converts a C style string to a V string. Note: the string data is reused, NOT copied.
// strings returned from this function will be normal V strings beside that (i.e. they would be // strings returned from this function will be normal V strings beside that (i.e. they would be
// freed by V's -autofree mechanism, when they are no longer used). // freed by V's -autofree mechanism, when they are no longer used).
@[unsafe] @[reused; unsafe]
pub fn (bp byteptr) vstring() string { pub fn (bp byteptr) vstring() string {
return string{ return string{
str: bp str: bp
@ -21,7 +21,7 @@ pub fn (bp byteptr) vstring() string {
// vstring_with_len converts a C style string to a V string. // vstring_with_len converts a C style string to a V string.
// Note: the string data is reused, NOT copied. // Note: the string data is reused, NOT copied.
@[unsafe] @[reused; unsafe]
pub fn (bp byteptr) vstring_with_len(len int) string { pub fn (bp byteptr) vstring_with_len(len int) string {
return string{ return string{
str: bp str: bp
@ -32,7 +32,7 @@ pub fn (bp byteptr) vstring_with_len(len int) string {
// vstring converts C char* to V string. // vstring converts C char* to V string.
// Note: the string data is reused, NOT copied. // Note: the string data is reused, NOT copied.
@[unsafe] @[reused; unsafe]
pub fn (cp charptr) vstring() string { pub fn (cp charptr) vstring() string {
return string{ return string{
str: byteptr(cp) str: byteptr(cp)
@ -43,7 +43,7 @@ pub fn (cp charptr) vstring() string {
// vstring_with_len converts C char* to V string. // vstring_with_len converts C char* to V string.
// Note: the string data is reused, NOT copied. // Note: the string data is reused, NOT copied.
@[unsafe] @[reused; unsafe]
pub fn (cp charptr) vstring_with_len(len int) string { pub fn (cp charptr) vstring_with_len(len int) string {
return string{ return string{
str: byteptr(cp) str: byteptr(cp)
@ -59,7 +59,7 @@ pub fn (cp charptr) vstring_with_len(len int) string {
// This is suitable for readonly strings, C string literals etc, // This is suitable for readonly strings, C string literals etc,
// that can be read by the V program, but that should not be // that can be read by the V program, but that should not be
// managed by it, for example `os.args` is implemented using it. // managed by it, for example `os.args` is implemented using it.
@[unsafe] @[reused; unsafe]
pub fn (bp byteptr) vstring_literal() string { pub fn (bp byteptr) vstring_literal() string {
return string{ return string{
str: bp str: bp
@ -70,7 +70,7 @@ pub fn (bp byteptr) vstring_literal() string {
// vstring_with_len converts a C style string to a V string. // vstring_with_len converts a C style string to a V string.
// Note: the string data is reused, NOT copied. // Note: the string data is reused, NOT copied.
@[unsafe] @[reused; unsafe]
pub fn (bp byteptr) vstring_literal_with_len(len int) string { pub fn (bp byteptr) vstring_literal_with_len(len int) string {
return string{ return string{
str: bp str: bp
@ -82,7 +82,7 @@ pub fn (bp byteptr) vstring_literal_with_len(len int) string {
// vstring_literal converts C char* to V string. // vstring_literal converts C char* to V string.
// See also vstring_literal defined on byteptr for more details. // See also vstring_literal defined on byteptr for more details.
// Note: the string data is reused, NOT copied. // Note: the string data is reused, NOT copied.
@[unsafe] @[reused; unsafe]
pub fn (cp charptr) vstring_literal() string { pub fn (cp charptr) vstring_literal() string {
return string{ return string{
str: byteptr(cp) str: byteptr(cp)
@ -94,7 +94,7 @@ pub fn (cp charptr) vstring_literal() string {
// vstring_literal_with_len converts C char* to V string. // vstring_literal_with_len converts C char* to V string.
// See also vstring_literal_with_len defined on byteptr. // See also vstring_literal_with_len defined on byteptr.
// Note: the string data is reused, NOT copied. // Note: the string data is reused, NOT copied.
@[unsafe] @[reused; unsafe]
pub fn (cp charptr) vstring_literal_with_len(len int) string { pub fn (cp charptr) vstring_literal_with_len(len int) string {
return string{ return string{
str: byteptr(cp) str: byteptr(cp)

View file

@ -98,6 +98,21 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int
if obj.expr is ast.IfGuardExpr { if obj.expr is ast.IfGuardExpr {
continue continue
} }
if obj.expr is ast.UnsafeExpr && obj.expr.expr is ast.CallExpr
&& (obj.expr.expr as ast.CallExpr).is_method {
if left_var := scope.objects[obj.expr.expr.left.str()] {
if func := g.table.find_method(g.table.final_sym(left_var.typ),
obj.expr.expr.name)
{
if func.attrs.contains('reused') && left_var is ast.Var
&& left_var.expr is ast.CastExpr {
if left_var.expr.expr.is_literal() {
continue
}
}
}
}
}
g.autofree_variable(obj) g.autofree_variable(obj)
} }
else {} else {}