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

gen: fix arr.last().field (#7076)

This commit is contained in:
Enzo 2020-12-02 04:35:00 +01:00 committed by GitHub
parent ff26f0539c
commit d8b8aca51e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -1002,6 +1002,27 @@ fn test_array_string_pop() {
assert a.cap == 3
}
fn test_array_first() {
a := [3]
assert a.first() == 3
b := [1, 2, 3, 4]
assert b.first() == 1
c := ['abc', 'def']
assert c.first()[0] == `a`
s := [Chunk{'a'}]
assert s.first().val == 'a'
}
fn test_array_last() {
a := [3]
assert a.last() == 3
b := [1, 2, 3, 4]
assert b.last() == 4
c := ['abc', 'def']
assert c.last()[0] == `d`
s := [Chunk{'a'}]
assert s.last().val == 'a'
}
[direct_array_access]
fn test_direct_array_access() {

View File

@ -365,6 +365,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
if node.name == 'str' {
g.gen_str_for_type(node.receiver_type)
}
mut has_cast := false
// TODO performance, detect `array` method differently
if left_sym.kind == .array && node.name in
['repeat', 'sort_with_compare', 'free', 'push_many', 'trim', 'first', 'last', 'pop', 'clone', 'reverse', 'slice'] {
@ -374,7 +375,8 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
receiver_type_name = 'array'
if node.name in ['last', 'first', 'pop'] {
return_type_str := g.typ(node.return_type)
g.write('*($return_type_str*)')
has_cast = true
g.write('(*($return_type_str*)')
}
}
mut name := util.no_dots('${receiver_type_name}_$node.name')
@ -428,6 +430,9 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
} else {
g.expr(node.left)
}
if has_cast {
g.write(')')
}
is_variadic := node.expected_arg_types.len > 0 && node.expected_arg_types[node.expected_arg_types.len -
1].has_flag(.variadic)
if node.args.len > 0 || is_variadic {