diff --git a/vlib/builtin/int_test.v b/vlib/builtin/int_test.v index dac0cc1cbe..31f89f76d7 100644 --- a/vlib/builtin/int_test.v +++ b/vlib/builtin/int_test.v @@ -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' diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 35b07e8149..69cfbd0ca8 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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 { diff --git a/vlib/v/tests/str_method_call_from_mut_test.v b/vlib/v/tests/str_method_call_from_mut_test.v new file mode 100644 index 0000000000..ed3d137eea --- /dev/null +++ b/vlib/v/tests/str_method_call_from_mut_test.v @@ -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' +}