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:
parent
b2735bf937
commit
01caecc284
@ -87,6 +87,12 @@ fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
|
|||||||
}
|
}
|
||||||
c.ensure_sumtype_array_has_default_value(node)
|
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 {}
|
c.ensure_type_exists(node.elem_type, node.elem_type_pos) or {}
|
||||||
if node.typ.has_flag(.generic) && c.table.cur_fn != unsafe { nil }
|
if node.typ.has_flag(.generic) && c.table.cur_fn != unsafe { nil }
|
||||||
&& c.table.cur_fn.generic_names.len == 0 {
|
&& c.table.cur_fn.generic_names.len == 0 {
|
||||||
|
@ -17,4 +17,12 @@ vlib/v/checker/tests/array_init_option_err.vv:8:34: error: cannot use unwrapped
|
|||||||
8 | arr1 = []int{len: get()?, init: get()}
|
8 | arr1 = []int{len: get()?, init: get()}
|
||||||
| ~~~~~
|
| ~~~~~
|
||||||
9 | dump(arr1)
|
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 | }
|
||||||
|
|
||||||
|
@ -7,6 +7,9 @@ fn main() {
|
|||||||
|
|
||||||
arr1 = []int{len: get()?, init: get()}
|
arr1 = []int{len: get()?, init: get()}
|
||||||
dump(arr1)
|
dump(arr1)
|
||||||
|
|
||||||
|
arr1 = []int{cap: get(), len: get()?, init: get()?}
|
||||||
|
dump(arr1)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get() ?int {
|
fn get() ?int {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user