diff --git a/doc/docs.md b/doc/docs.md index 9d6fddb340a166..65b6a465fe0b03 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -1051,6 +1051,7 @@ An array can be of these types: | Thread | `[]thread int` | | Reference | `[]&f64` | | Shared | `[]shared MyStructType` | +| Option | `[]?f64` | **Example Code:** diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 496b0770202273..06e3bdad926143 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -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) diff --git a/vlib/v/parser/tests/array_init.out b/vlib/v/parser/tests/array_init.out index 75bdbe31a684bf..bd16e5840e4270 100644 --- a/vlib/v/parser/tests/array_init.out +++ b/vlib/v/parser/tests/array_init.out @@ -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 | } \ No newline at end of file diff --git a/vlib/v/parser/tests/array_init.vv b/vlib/v/parser/tests/array_init.vv index 4bbfb88ed29b83..2d1bf8d62848d8 100644 --- a/vlib/v/parser/tests/array_init.vv +++ b/vlib/v/parser/tests/array_init.vv @@ -1,4 +1,5 @@ fn main() { _ := []int _ := [1]int + _ := []!int{} }