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

checker: fix generic array delete in skip_unused mode (#17759)

This commit is contained in:
yuyi 2023-03-25 14:31:25 +08:00 committed by GitHub
parent ca198ace7d
commit a9f55de352
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 1 deletions

View File

@ -25,7 +25,7 @@ const (
pub const (
array_builtin_methods = ['filter', 'clone', 'repeat', 'reverse', 'map', 'slice', 'sort',
'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop']
'contains', 'index', 'wait', 'any', 'all', 'first', 'last', 'pop', 'delete']
array_builtin_methods_chk = token.new_keywords_matcher_from_array_trie(array_builtin_methods)
// TODO: remove `byte` from this list when it is no longer supported
reserved_type_names = ['byte', 'bool', 'char', 'i8', 'i16', 'int', 'i64', 'u8', 'u16',

View File

@ -2426,6 +2426,11 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as
} else {
node.receiver_type = left_type
}
} else if method_name == 'delete' {
unwrapped_left_sym := c.table.sym(c.unwrap_generic(left_type))
if method := c.table.find_method(unwrapped_left_sym, method_name) {
node.receiver_type = method.receiver_type
}
}
return node.return_type
}

View File

@ -0,0 +1,16 @@
struct Bar[K] {
}
struct Foo[K] {
mut:
bars []Bar[K]
}
fn main() {
mut f := Foo[int]{[Bar[int]{}]}
f.xy()
}
pub fn (mut f Foo[K]) xy() {
f.bars.delete(0)
}