Skip to content

Commit 86536e4

Browse files
authored
checker: fix check for pushing on an unwrapped option array (fix #24073) (#24093)
1 parent 51e7861 commit 86536e4

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

vlib/v/checker/infix.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
5757
}
5858
// `arr << if n > 0 { 10 } else { 11 }` set the right c.expected_type
5959
if node.op == .left_shift && c.table.sym(left_type).kind == .array {
60+
if left_type.has_flag(.option) {
61+
c.error('cannot push to Option array that was not unwrapped first', node.left.pos())
62+
}
6063
c.markused_infiexpr(!c.is_builtin_mod && c.mod != 'strings')
6164
if mut node.right is ast.IfExpr {
6265
if node.right.is_expr && node.right.branches.len > 0 {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/option_array_push.vv:13:5: error: cannot push to Option array that was not unwrapped first
2+
11 | fn my_func(args Struct2) {
3+
12 | mut s1 := args.Struct1
4+
13 | s1.strings << ['hi']
5+
| ~~~~~~~
6+
14 | }
7+
15 |
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pub struct Struct1 {
2+
pub mut:
3+
strings ?[]string
4+
}
5+
6+
@[params]
7+
pub struct Struct2 {
8+
Struct1
9+
}
10+
11+
fn my_func(args Struct2) {
12+
mut s1 := args.Struct1
13+
s1.strings << ['hi']
14+
}
15+
16+
s2 := Struct2{}
17+
18+
my_func(s2)

vlib/v/checker/tests/wrong_shift_left_option_err.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,24 @@
1+
vlib/v/checker/tests/wrong_shift_left_option_err.vv:7:2: error: cannot push to Option array that was not unwrapped first
2+
5 | fn main() {
3+
6 | mut a := ?[]SomeStruct([SomeStruct{}, SomeStruct{}]) // struct type
4+
7 | a << []SomeStruct{len: 20}
5+
| ^
6+
8 | mut b := ?[]int([2, 8]) // primitive type
7+
9 | b << [1, 3, 4]
18
vlib/v/checker/tests/wrong_shift_left_option_err.vv:7:4: error: unwrapped Option cannot be used in an infix expression
29
5 | fn main() {
310
6 | mut a := ?[]SomeStruct([SomeStruct{}, SomeStruct{}]) // struct type
411
7 | a << []SomeStruct{len: 20}
512
| ~~
613
8 | mut b := ?[]int([2, 8]) // primitive type
714
9 | b << [1, 3, 4]
15+
vlib/v/checker/tests/wrong_shift_left_option_err.vv:9:2: error: cannot push to Option array that was not unwrapped first
16+
7 | a << []SomeStruct{len: 20}
17+
8 | mut b := ?[]int([2, 8]) // primitive type
18+
9 | b << [1, 3, 4]
19+
| ^
20+
10 | dump(a?.len)
21+
11 | dump(b)
822
vlib/v/checker/tests/wrong_shift_left_option_err.vv:9:4: error: unwrapped Option cannot be used in an infix expression
923
7 | a << []SomeStruct{len: 20}
1024
8 | mut b := ?[]int([2, 8]) // primitive type

0 commit comments

Comments
 (0)