From fa0eb5242a05f86344fd2331b1c22959988c5efe Mon Sep 17 00:00:00 2001 From: Adam Oates Date: Sat, 4 Jan 2025 13:35:59 -0600 Subject: [PATCH 1/5] parser: add error for `[]!type{}` --- vlib/v/parser/containers.v | 5 ++++- vlib/v/parser/tests/array_init.out | 6 ++++++ vlib/v/parser/tests/array_init.vv | 1 + 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 496b0770202273..c00617e4500c8b 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -29,13 +29,16 @@ 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('array does not support storing storing Result', 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..7dff501a479923 100644 --- a/vlib/v/parser/tests/array_init.out +++ b/vlib/v/parser/tests/array_init.out @@ -10,3 +10,9 @@ vlib/v/parser/tests/array_init.vv:3:7: warning: use e.g. `x := [1]Type{}` instea 3 | _ := [1]int | ~~~~~~ 4 | } +vlib/v/parser/tests/array_init.vv4:9:: error: expression evaluated but not used + 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{} } From a4950f4b6130d976324488649f3d018037a9c3fd Mon Sep 17 00:00:00 2001 From: Adam Oates Date: Sat, 4 Jan 2025 13:52:58 -0600 Subject: [PATCH 2/5] docs: add Option as array type --- doc/docs.md | 1 + 1 file changed, 1 insertion(+) 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:** From 1d24aacbadabbc10025e2767e0915eb82faa0d38 Mon Sep 17 00:00:00 2001 From: Adam Oates Date: Sat, 4 Jan 2025 13:57:47 -0600 Subject: [PATCH 3/5] parser: fmt --- vlib/v/parser/containers.v | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index c00617e4500c8b..25b2a47ae8c85a 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -37,7 +37,8 @@ fn (mut p Parser) array_init(is_option bool, alias_array_type ast.Type) ast.Arra // result of expr so we do those in checker if elem_type != 0 { if elem_type.has_flag(.result) { - p.error_with_pos('array does not support storing storing Result', elem_type_pos) + p.error_with_pos('array does not support storing storing Result', + elem_type_pos) } idx := p.table.find_or_register_array(elem_type) if elem_type.has_flag(.generic) { From e35d31fb6f329de1e2efa83c51a66eaca76d1a5e Mon Sep 17 00:00:00 2001 From: Adam Oates Date: Sat, 4 Jan 2025 14:28:39 -0600 Subject: [PATCH 4/5] parser: fix array_init.out --- vlib/v/parser/tests/array_init.out | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/vlib/v/parser/tests/array_init.out b/vlib/v/parser/tests/array_init.out index 7dff501a479923..e3437627088009 100644 --- a/vlib/v/parser/tests/array_init.out +++ b/vlib/v/parser/tests/array_init.out @@ -3,14 +3,15 @@ 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 | } -vlib/v/parser/tests/array_init.vv4:9:: error: expression evaluated but not used + 4 | _ := []!int{} + 5 | } +vlib/v/parser/tests/array_init.vv:4:9: error: array does not support storing storing Result 2 | _ := []int 3 | _ := [1]int 4 | _ := []!int{} From 903a10842b92841c548bd07ef64fed1cb7dc307b Mon Sep 17 00:00:00 2001 From: Adam Oates Date: Sun, 5 Jan 2025 08:10:58 -0600 Subject: [PATCH 5/5] fix error message --- vlib/v/parser/containers.v | 2 +- vlib/v/parser/tests/array_init.out | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/vlib/v/parser/containers.v b/vlib/v/parser/containers.v index 25b2a47ae8c85a..06e3bdad926143 100644 --- a/vlib/v/parser/containers.v +++ b/vlib/v/parser/containers.v @@ -37,7 +37,7 @@ fn (mut p Parser) array_init(is_option bool, alias_array_type ast.Type) ast.Arra // result of expr so we do those in checker if elem_type != 0 { if elem_type.has_flag(.result) { - p.error_with_pos('array does not support storing storing Result', + p.error_with_pos('arrays do not support storing Result values', elem_type_pos) } idx := p.table.find_or_register_array(elem_type) diff --git a/vlib/v/parser/tests/array_init.out b/vlib/v/parser/tests/array_init.out index e3437627088009..bd16e5840e4270 100644 --- a/vlib/v/parser/tests/array_init.out +++ b/vlib/v/parser/tests/array_init.out @@ -11,7 +11,7 @@ vlib/v/parser/tests/array_init.vv:3:7: warning: use e.g. `x := [1]Type{}` instea | ~~~~~~ 4 | _ := []!int{} 5 | } -vlib/v/parser/tests/array_init.vv:4:9: error: array does not support storing storing Result +vlib/v/parser/tests/array_init.vv:4:9: error: arrays do not support storing Result values 2 | _ := []int 3 | _ := [1]int 4 | _ := []!int{}