Skip to content

Commit 66ac23f

Browse files
authored
parser: add error for array init of Results []!type{} (fix #23360) (#23375)
1 parent c77292a commit 66ac23f

File tree

4 files changed

+16
-3
lines changed

4 files changed

+16
-3
lines changed

doc/docs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,7 @@ An array can be of these types:
10511051
| Thread | `[]thread int` |
10521052
| Reference | `[]&f64` |
10531053
| Shared | `[]shared MyStructType` |
1054+
| Option | `[]?f64` |
10541055

10551056
**Example Code:**
10561057

vlib/v/parser/containers.v

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,17 @@ fn (mut p Parser) array_init(is_option bool, alias_array_type ast.Type) ast.Arra
2929
line_nr := p.tok.line_nr
3030
p.next()
3131
// []string
32-
if p.tok.kind in [.name, .amp, .lsbr, .question, .key_shared]
32+
if p.tok.kind in [.name, .amp, .lsbr, .question, .key_shared, .not]
3333
&& p.tok.line_nr == line_nr {
3434
elem_type_pos = p.tok.pos()
3535
elem_type = p.parse_type()
3636
// this is set here because it's a known type, others could be the
3737
// result of expr so we do those in checker
3838
if elem_type != 0 {
39+
if elem_type.has_flag(.result) {
40+
p.error_with_pos('arrays do not support storing Result values',
41+
elem_type_pos)
42+
}
3943
idx := p.table.find_or_register_array(elem_type)
4044
if elem_type.has_flag(.generic) {
4145
array_type = ast.new_type(idx).set_flag(.generic)

vlib/v/parser/tests/array_init.out

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@ vlib/v/parser/tests/array_init.vv:2:7: warning: use `x := []Type{}` instead of `
33
2 | _ := []int
44
| ~~~~~
55
3 | _ := [1]int
6-
4 | }
6+
4 | _ := []!int{}
77
vlib/v/parser/tests/array_init.vv:3:7: warning: use e.g. `x := [1]Type{}` instead of `x := [1]Type`
88
1 | fn main() {
99
2 | _ := []int
1010
3 | _ := [1]int
1111
| ~~~~~~
12-
4 | }
12+
4 | _ := []!int{}
13+
5 | }
14+
vlib/v/parser/tests/array_init.vv:4:9: error: arrays do not support storing Result values
15+
2 | _ := []int
16+
3 | _ := [1]int
17+
4 | _ := []!int{}
18+
| ^
19+
5 | }

vlib/v/parser/tests/array_init.vv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
fn main() {
22
_ := []int
33
_ := [1]int
4+
_ := []!int{}
45
}

0 commit comments

Comments
 (0)