Skip to content

Commit a33a2ba

Browse files
committed
checker: check for x := []Interface{len: 9} without init:
1 parent d0a0957 commit a33a2ba

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

vlib/v/checker/checker.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3840,6 +3840,13 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) ast.Type {
38403840
}
38413841
}
38423842
if array_init.has_len {
3843+
if array_init.has_len && !array_init.has_default {
3844+
elem_type_sym := c.table.get_type_symbol(array_init.elem_type)
3845+
if elem_type_sym.kind == .interface_ {
3846+
c.error('cannot instantiate an array of interfaces without also giving a default `init:` value',
3847+
array_init.len_expr.position())
3848+
}
3849+
}
38433850
c.ensure_sumtype_array_has_default_value(array_init)
38443851
}
38453852
c.ensure_type_exists(array_init.elem_type, array_init.elem_type_pos) or {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/array_of_interfaces_with_len_without_init.vv:14:37: error: cannot instantiate an array of interfaces without also giving a default `init:` value
2+
12 |
3+
13 | fn main() {
4+
14 | mut parsed_lines := []MObject{len: 9}
5+
| ^
6+
15 | println(parsed_lines)
7+
16 | }
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
interface MObject {
2+
give_string() string
3+
}
4+
5+
struct LeStruct {
6+
le_string string
7+
}
8+
9+
fn (a LeStruct) give_string() string {
10+
return 'V'
11+
}
12+
13+
fn main() {
14+
mut parsed_lines := []MObject{len: 9}
15+
println(parsed_lines)
16+
}

0 commit comments

Comments
 (0)