Skip to content

Commit f7cfec2

Browse files
authored
checker: add interface type handling and new testcase (fix #24116) (#26165)
1 parent af98b91 commit f7cfec2

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

vlib/v/checker/containers.v

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ fn (mut c Checker) check_array_init_default_expr(mut node ast.ArrayInit) {
323323
if node.elem_type.is_number() && init_typ.is_number() {
324324
return
325325
}
326+
if c.table.type_kind(node.elem_type) == .interface {
327+
if c.type_implements(init_typ, node.elem_type, init_expr.pos()) {
328+
return
329+
}
330+
}
326331
c.check_expected(init_typ, node.elem_type) or { c.error(err.msg(), init_expr.pos()) }
327332
}
328333

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
interface IValue {}
2+
3+
struct Null {}
4+
5+
struct MyInt {
6+
val int
7+
}
8+
9+
fn test_array_init_with_interface_implicit_cast() {
10+
// Test that array init with interface element type allows implicit cast
11+
a := []IValue{len: 3, init: Null{}}
12+
assert a.len == 3
13+
14+
b := []IValue{len: 2, init: MyInt{42}}
15+
assert b.len == 2
16+
}

0 commit comments

Comments
 (0)