Skip to content

Commit

Permalink
checker: fix negative cap, len checks in array init (#19694)
Browse files Browse the repository at this point in the history
  • Loading branch information
shove70 committed Oct 29, 2023
1 parent 1e578b1 commit aa28efc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/checker/containers.v
Expand Up @@ -338,6 +338,12 @@ fn (mut c Checker) check_array_init_para_type(para string, mut expr ast.Expr, po
if sym.kind !in [.int, .int_literal] {
c.error('array ${para} needs to be an int', pos)
}
if expr is ast.IntegerLiteral {
lit := expr as ast.IntegerLiteral
if lit.val.int() < 0 {
c.error('array ${para} can not be negative', lit.pos)
}
}
}

fn (mut c Checker) ensure_sumtype_array_has_default_value(node ast.ArrayInit) {
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/checker/tests/array_init_with_len_cap_err.out
@@ -0,0 +1,8 @@
vlib/v/checker/tests/array_init_with_len_cap_err.vv:1:16: error: array len can not be negative
1 | _ = []int{len: -1}
| ~~
2 | _ = []int{cap: -1}
vlib/v/checker/tests/array_init_with_len_cap_err.vv:2:16: error: array cap can not be negative
1 | _ = []int{len: -1}
2 | _ = []int{cap: -1}
| ~~
2 changes: 2 additions & 0 deletions vlib/v/checker/tests/array_init_with_len_cap_err.vv
@@ -0,0 +1,2 @@
_ = []int{len: -1}
_ = []int{cap: -1}

0 comments on commit aa28efc

Please sign in to comment.