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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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}(')
}
}
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()
|| 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
@ -1177,7 +1178,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
if !node.left.is_lvalue() {
g.write('ADDR(${rec_cc_type}, ')
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) {
g.write('&')
}
@ -1205,7 +1206,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write('/*af receiver arg*/' + arg_name)
} else {
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('*')
}
if node.left is ast.MapInit {
@ -1241,7 +1242,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write(embed_name)
}
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')
}
}

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
}