1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix method calls to nonarray methods, named first, last or repeat (#17252)

This commit is contained in:
yuyi
2023-02-08 16:45:17 +08:00
committed by GitHub
parent 0b17865c3d
commit 6136b62220
2 changed files with 17 additions and 4 deletions

View File

@@ -1167,7 +1167,8 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write('${name}(') g.write('${name}(')
} }
} }
is_node_name_in_first_last_repeat := node.name in ['first', 'last', 'repeat'] is_array_method_first_last_repeat := final_left_sym.kind == .array
&& node.name in ['first', 'last', 'repeat']
if node.receiver_type.is_ptr() && (!left_type.is_ptr() if node.receiver_type.is_ptr() && (!left_type.is_ptr()
|| node.from_embed_types.len != 0 || (left_type.has_flag(.shared_f) && node.name != 'str')) { || node.from_embed_types.len != 0 || (left_type.has_flag(.shared_f) && node.name != 'str')) {
// The receiver is a reference, but the caller provided a value // The receiver is a reference, but the caller provided a value
@@ -1177,7 +1178,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
if !node.left.is_lvalue() { if !node.left.is_lvalue() {
g.write('ADDR(${rec_cc_type}, ') g.write('ADDR(${rec_cc_type}, ')
has_cast = true has_cast = true
} else if !is_node_name_in_first_last_repeat && !(left_type.has_flag(.shared_f) } else if !is_array_method_first_last_repeat && !(left_type.has_flag(.shared_f)
&& left_type == node.receiver_type) { && left_type == node.receiver_type) {
g.write('&') g.write('&')
} }
@@ -1205,7 +1206,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write('/*af receiver arg*/' + arg_name) g.write('/*af receiver arg*/' + arg_name)
} else { } else {
if left_sym.kind == .array && node.left.is_auto_deref_var() if left_sym.kind == .array && node.left.is_auto_deref_var()
&& is_node_name_in_first_last_repeat { && is_array_method_first_last_repeat {
g.write('*') g.write('*')
} }
if node.left is ast.MapInit { if node.left is ast.MapInit {
@@ -1241,7 +1242,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write(embed_name) g.write(embed_name)
} }
if left_type.has_flag(.shared_f) if left_type.has_flag(.shared_f)
&& (left_type != node.receiver_type || is_node_name_in_first_last_repeat) { && (left_type != node.receiver_type || is_array_method_first_last_repeat) {
g.write('->val') g.write('->val')
} }
} }

View File

@@ -0,0 +1,12 @@
struct Foo {}
fn (l &Foo) first() {}
fn (l &Foo) last() {}
fn test_method_first_last_call() {
f := Foo{}
f.first()
f.last()
assert true
}