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

checker: add check for unwrapped option in array cap (#17851)

This commit is contained in:
Swastik Baranwal 2023-04-02 19:17:08 +05:30 committed by GitHub
parent b2735bf937
commit 01caecc284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 4 deletions

View File

@ -87,6 +87,12 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
}
c.ensure_sumtype_array_has_default_value(node)
}
if node.has_cap {
cap_typ := c.check_expr_opt_call(node.cap_expr, c.expr(node.cap_expr))
if cap_typ.has_flag(.option) {
c.error('cannot use unwrapped Option as capacity', node.cap_expr.pos())
}
}
c.ensure_type_exists(node.elem_type, node.elem_type_pos) or {}
if node.typ.has_flag(.generic) && c.table.cur_fn != unsafe { nil }
&& c.table.cur_fn.generic_names.len == 0 {

View File

@ -6,15 +6,23 @@ vlib/v/checker/tests/array_init_option_err.vv:2:38: error: cannot use unwrapped
4 |
vlib/v/checker/tests/array_init_option_err.vv:5:20: error: cannot use unwrapped Option as length
3 | dump(arr1)
4 |
4 |
5 | arr1 = []int{len: get(), init: get()?}
| ~~~~~
6 | dump(arr1)
7 |
vlib/v/checker/tests/array_init_option_err.vv:8:34: error: cannot use unwrapped Option as initializer
6 | dump(arr1)
7 |
7 |
8 | arr1 = []int{len: get()?, init: get()}
| ~~~~~
9 | dump(arr1)
10 | }
10 |
vlib/v/checker/tests/array_init_option_err.vv:11:20: error: cannot use unwrapped Option as capacity
9 | dump(arr1)
10 |
11 | arr1 = []int{cap: get(), len: get()?, init: get()?}
| ~~~~~
12 | dump(arr1)
13 | }

View File

@ -7,8 +7,11 @@ fn main() {
arr1 = []int{len: get()?, init: get()}
dump(arr1)
arr1 = []int{cap: get(), len: get()?, init: get()?}
dump(arr1)
}
fn get() ?int {
return 5
}
}