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

cgen: fix generic array of interface method call (fix #12882) (#12888)

This commit is contained in:
yuyi 2021-12-19 00:08:19 +08:00 committed by GitHub
parent 5e5c78ed37
commit 2ab861ef89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 4 deletions

View File

@ -1557,6 +1557,7 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
g.checker_bug('ref_or_deref_arg expected_type is 0', arg.pos)
}
exp_sym := g.table.get_type_symbol(expected_type)
arg_typ := g.unwrap_generic(arg.typ)
mut needs_closing := false
if arg.is_mut && !arg_is_ptr {
g.write('&/*mut*/')
@ -1578,10 +1579,10 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
}
}
if !g.is_json_fn {
if arg.typ == 0 {
if arg_typ == 0 {
g.checker_bug('ref_or_deref_arg arg.typ is 0', arg.pos)
}
arg_typ_sym := g.table.get_type_symbol(arg.typ)
arg_typ_sym := g.table.get_type_symbol(arg_typ)
expected_deref_type := if expected_type.is_ptr() {
expected_type.deref()
} else {
@ -1606,7 +1607,7 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
}
}
}
} else if arg.typ.has_flag(.shared_f) && !expected_type.has_flag(.shared_f) {
} else if arg_typ.has_flag(.shared_f) && !expected_type.has_flag(.shared_f) {
if expected_type.is_ptr() {
g.write('&')
}
@ -1614,7 +1615,7 @@ fn (mut g Gen) ref_or_deref_arg(arg ast.CallArg, expected_type ast.Type, lang as
g.write('->val')
return
}
g.expr_with_cast(arg.expr, arg.typ, expected_type)
g.expr_with_cast(arg.expr, arg_typ, expected_type)
if needs_closing {
g.write(')')
}

View File

@ -0,0 +1,36 @@
struct Array<T> {
pub mut:
elements []T
}
struct String {
str string
}
interface IObject {
equals(IObject) bool
}
pub fn (s1 String) equals(s2 IObject) bool {
if s2 is String {
return s1.str == s2.str
}
return false
}
pub fn (mut m Array<T>) contains(e T) bool {
for mut element in m.elements {
if element.equals(e) {
return true
}
}
return false
}
fn test_generic_array_of_interface_method_call() {
s := String{'hello'}
mut a := Array<IObject>{[s]}
ret := a.contains(IObject(s))
println(ret)
assert ret
}