Skip to content

Commit 1471ba4

Browse files
authored
checker: fix missing check for initializer with function returning options (#17820)
1 parent 6aec824 commit 1471ba4

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

vlib/v/checker/containers.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,18 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
6666
default_expr := node.default_expr
6767
default_typ := c.check_expr_opt_call(default_expr, c.expr(default_expr))
6868
node.default_type = default_typ
69+
if !node.elem_type.has_flag(.option) && default_typ.has_flag(.option) {
70+
c.error('cannot use unwrapped Option as initializer', default_expr.pos())
71+
}
6972
c.check_expected(default_typ, node.elem_type) or {
7073
c.error(err.msg(), default_expr.pos())
7174
}
7275
}
7376
if node.has_len {
77+
len_typ := c.check_expr_opt_call(node.len_expr, c.expr(node.len_expr))
78+
if len_typ.has_flag(.option) {
79+
c.error('cannot use unwrapped Option as length', node.len_expr.pos())
80+
}
7481
if node.has_len && !node.has_default {
7582
elem_type_sym := c.table.sym(node.elem_type)
7683
if elem_type_sym.kind == .interface_ {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
vlib/v/checker/tests/array_init_option_err.vv:2:38: error: cannot use unwrapped Option as initializer
2+
1 | fn main() {
3+
2 | mut arr1 := []int{len: get(), init: get()}
4+
| ~~~~~
5+
3 | dump(arr1)
6+
4 |
7+
vlib/v/checker/tests/array_init_option_err.vv:5:20: error: cannot use unwrapped Option as length
8+
3 | dump(arr1)
9+
4 |
10+
5 | arr1 = []int{len: get(), init: get()?}
11+
| ~~~~~
12+
6 | dump(arr1)
13+
7 |
14+
vlib/v/checker/tests/array_init_option_err.vv:8:34: error: cannot use unwrapped Option as initializer
15+
6 | dump(arr1)
16+
7 |
17+
8 | arr1 = []int{len: get()?, init: get()}
18+
| ~~~~~
19+
9 | dump(arr1)
20+
10 | }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fn main() {
2+
mut arr1 := []int{len: get(), init: get()}
3+
dump(arr1)
4+
5+
arr1 = []int{len: get(), init: get()?}
6+
dump(arr1)
7+
8+
arr1 = []int{len: get()?, init: get()}
9+
dump(arr1)
10+
}
11+
12+
fn get() ?int {
13+
return 5
14+
}

vlib/v/checker/tests/option_fn_err.out

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
vlib/v/checker/tests/option_fn_err.vv:40:9: error: assert can be used only with `bool` expressions, but found `bool` instead
2-
38 |
2+
38 |
33
39 | // assert
44
40 | assert bar(true)
55
| ~~~~~~~~~
6-
41 |
6+
41 |
77
42 | // struct
88
vlib/v/checker/tests/option_fn_err.vv:45:3: error: cannot assign an Option value to a non-option struct field
99
43 | mut v := Data{
@@ -12,6 +12,13 @@ vlib/v/checker/tests/option_fn_err.vv:45:3: error: cannot assign an Option value
1212
| ~~~~~~~~~~~~~
1313
46 | opt: bar(0)
1414
47 | }
15+
vlib/v/checker/tests/option_fn_err.vv:56:27: error: cannot use unwrapped Option as initializer
16+
54 | // init
17+
55 | _ := [bar(0)]
18+
56 | _ := []int{len: 1, init: bar(0)}
19+
| ~~~~~~
20+
57 | _ := [bar(0)]!
21+
58 | _ := [1]int{init: bar(0)}
1522
vlib/v/checker/tests/option_fn_err.vv:60:13: error: cannot use Option or Result as index (array type `[]int`)
1623
58 | _ := [1]int{init: bar(0)}
1724
59 | // index
@@ -38,5 +45,5 @@ vlib/v/checker/tests/option_fn_err.vv:69:18: error: type mismatch, `bar` must re
3845
68 | println(arr.any(bar(true)))
3946
69 | println(arr.all(bar(true)))
4047
| ~~~~~~~~~
41-
70 |
48+
70 |
4249
71 | match bar(0) {

0 commit comments

Comments
 (0)