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

checker: handle array of interfaces

This commit is contained in:
Alexander Medvednikov 2020-04-26 06:29:03 +02:00
parent 8000eb3656
commit 2b4ac0e63a
2 changed files with 9 additions and 1 deletions

View File

@ -996,6 +996,11 @@ pub fn (mut c Checker) array_init(array_init mut ast.ArrayInit) table.Type {
} }
// [1,2,3] // [1,2,3]
if array_init.exprs.len > 0 && array_init.elem_type == table.void_type { if array_init.exprs.len > 0 && array_init.elem_type == table.void_type {
expecting_interface_array := c.expected_type != 0 &&
c.table.get_type_symbol( c.table.value_type(c.expected_type) ).kind == .interface_
//if expecting_interface_array {
//println('ex $c.expected_type')
//}
for i, expr in array_init.exprs { for i, expr in array_init.exprs {
typ := c.expr(expr) typ := c.expr(expr)
// The first element's type // The first element's type
@ -1004,6 +1009,9 @@ pub fn (mut c Checker) array_init(array_init mut ast.ArrayInit) table.Type {
c.expected_type = typ c.expected_type = typ
continue continue
} }
if expecting_interface_array {
continue
}
if !c.table.check(elem_type, typ) { if !c.table.check(elem_type, typ) {
elem_type_sym := c.table.get_type_symbol(elem_type) elem_type_sym := c.table.get_type_symbol(elem_type)
c.error('expected array element with type `$elem_type_sym.name`', array_init.pos) c.error('expected array element with type `$elem_type_sym.name`', array_init.pos)

View File

@ -46,7 +46,7 @@ fn test_perform_speak() {
perform_speak(dog) perform_speak(dog)
cat := Cat{} cat := Cat{}
perform_speak(cat) perform_speak(cat)
perform_speakers([dog, cat]) //perform_speakers([dog, cat])
/* /*
f := Foo { f := Foo {
speaker: dog speaker: dog