parser: add error for array init of Results []!type{} (fix #23360) (#23375)

This commit is contained in:
Adam Oates 2025-01-05 11:09:33 -06:00 committed by GitHub
parent c77292ac2c
commit 66ac23fc57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 16 additions and 3 deletions

View File

@ -1051,6 +1051,7 @@ An array can be of these types:
| Thread | `[]thread int` |
| Reference | `[]&f64` |
| Shared | `[]shared MyStructType` |
| Option | `[]?f64` |
**Example Code:**

View File

@ -29,13 +29,17 @@ fn (mut p Parser) array_init(is_option bool, alias_array_type ast.Type) ast.Arra
line_nr := p.tok.line_nr
p.next()
// []string
if p.tok.kind in [.name, .amp, .lsbr, .question, .key_shared]
if p.tok.kind in [.name, .amp, .lsbr, .question, .key_shared, .not]
&& p.tok.line_nr == line_nr {
elem_type_pos = p.tok.pos()
elem_type = p.parse_type()
// this is set here because it's a known type, others could be the
// result of expr so we do those in checker
if elem_type != 0 {
if elem_type.has_flag(.result) {
p.error_with_pos('arrays do not support storing Result values',
elem_type_pos)
}
idx := p.table.find_or_register_array(elem_type)
if elem_type.has_flag(.generic) {
array_type = ast.new_type(idx).set_flag(.generic)

View File

@ -3,10 +3,17 @@ vlib/v/parser/tests/array_init.vv:2:7: warning: use `x := []Type{}` instead of `
2 | _ := []int
| ~~~~~
3 | _ := [1]int
4 | }
4 | _ := []!int{}
vlib/v/parser/tests/array_init.vv:3:7: warning: use e.g. `x := [1]Type{}` instead of `x := [1]Type`
1 | fn main() {
2 | _ := []int
3 | _ := [1]int
| ~~~~~~
4 | }
4 | _ := []!int{}
5 | }
vlib/v/parser/tests/array_init.vv:4:9: error: arrays do not support storing Result values
2 | _ := []int
3 | _ := [1]int
4 | _ := []!int{}
| ^
5 | }

View File

@ -1,4 +1,5 @@
fn main() {
_ := []int
_ := [1]int
_ := []!int{}
}