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:
parent
0b17865c3d
commit
6136b62220
@ -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')
|
||||
}
|
||||
}
|
||||
|
12
vlib/v/tests/method_first_last_call_test.v
Normal file
12
vlib/v/tests/method_first_last_call_test.v
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user