1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
This commit is contained in:
Felipe Pena 2023-08-08 14:08:47 -07:00 committed by GitHub
commit 2fd5465be6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 4 deletions

View File

@ -31,7 +31,7 @@ fn test_str_methods() {
assert u64(-1).str() == '18446744073709551615'
assert voidptr(-1).str() == '0xffffffffffffffff'
assert voidptr(1).str() == '0x1'
assert (&u8(-1)).str() == 'ffffffffffffffff'
assert (&u8(-1)).str() == '255'
assert (&u8(1)).str() == '1'
assert byteptr(-1).str() == '0xffffffffffffffff'
assert byteptr(1).str() == '0x1'

View File

@ -1382,10 +1382,11 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
}
// TODO2
// g.generate_tmp_autofree_arg_vars(node, name)
if !node.receiver_type.is_ptr() && left_type.is_ptr() && node.name == 'str' {
has_str_method := node.name == 'str' && left_sym.has_method('str')
if !has_str_method && !node.receiver_type.is_ptr() && left_type.is_ptr() && node.name == 'str' {
g.write('ptr_str(')
} else if node.receiver_type.is_ptr() && left_type.is_ptr() && node.name == 'str'
&& !left_sym.has_method('str') {
} else if !has_str_method && node.receiver_type.is_ptr() && left_type.is_ptr()
&& node.name == 'str' {
g.gen_expr_to_string(node.left, left_type)
return
} else {
@ -1455,6 +1456,10 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.expr(node.left)
}
} else {
if node.left is ast.Ident && has_str_method && left_type.is_ptr()
&& !node.receiver_type.is_ptr() {
g.write('*')
}
g.expr(node.left)
}
for i, embed in node.from_embed_types {

View File

@ -0,0 +1,21 @@
struct St {
mut:
a []u8
}
pub fn (s St) str() string {
return s.a.bytestr()
}
fn confuser(mut s St) {
assert s.str() == 'a'
}
fn test_str_method_with_mut() {
mut s := St{
a: []u8{}
}
s.a << `a`
confuser(mut s)
assert s.str() == 'a'
}