File tree Expand file tree Collapse file tree 4 files changed +42
-0
lines changed
Expand file tree Collapse file tree 4 files changed +42
-0
lines changed Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff line change 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 |
Original file line number Diff line number Diff line change 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)
Original file line number Diff line number Diff line change 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]
18vlib/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)
822vlib/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
You can’t perform that action at this time.
0 commit comments