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

cgen: fix generics array delete (#16794)

This commit is contained in:
Swastik Baranwal 2022-12-29 03:45:47 +05:30 committed by GitHub
parent 4718a818b8
commit ad9ca349dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 0 deletions

View File

@ -959,6 +959,26 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.expr(node.args[0].expr)
g.write(')')
return
} else if left_sym.kind == .array && node.name == 'drop' {
g.write('array_drop(')
if left_type.has_flag(.shared_f) {
if left_type.is_ptr() {
g.write('&')
}
g.expr(node.left)
g.write('->val')
} else {
if left_type.is_ptr() {
g.expr(node.left)
} else {
g.write('&')
g.expr(node.left)
}
}
g.write(', ')
g.expr(node.args[0].expr)
g.write(')')
return
}
if left_sym.kind in [.sum_type, .interface_] {

View File

@ -0,0 +1,11 @@
fn shift[T](mut a []T) T {
res := a.first()
a.drop(1)
return res
}
fn test_generic_array_drop() {
mut a := ['x', 'y']
assert shift(mut a) == 'x' // 'x'
assert a == ['y'] // ['y']
}