From 4ae2c22c18d0a60c02d0c214fc9193fa972e8d76 Mon Sep 17 00:00:00 2001 From: zakuro Date: Thu, 18 Mar 2021 22:11:51 +0900 Subject: [PATCH] checker: make optional array element an error (#9343) --- vlib/v/checker/checker.v | 2 +- vlib/v/checker/tests/optional_fn_err.out | 20 +++++++++++++++++++ ...called_as_normal.vv => optional_fn_err.vv} | 7 +++++++ .../tests/optional_void_called_as_normal.out | 6 ------ 4 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 vlib/v/checker/tests/optional_fn_err.out rename vlib/v/checker/tests/{optional_void_called_as_normal.vv => optional_fn_err.vv} (68%) delete mode 100644 vlib/v/checker/tests/optional_void_called_as_normal.out diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 66653991d9..3583c2d6e8 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3097,7 +3097,7 @@ pub fn (mut c Checker) array_init(mut array_init ast.ArrayInit) table.Type { // println('ex $c.expected_type') // } for i, expr in array_init.exprs { - typ := c.expr(expr) + typ := c.check_expr_opt_call(expr, c.expr(expr)) array_init.expr_types << typ // The first element's type if expecting_interface_array { diff --git a/vlib/v/checker/tests/optional_fn_err.out b/vlib/v/checker/tests/optional_fn_err.out new file mode 100644 index 0000000000..f493ce1b82 --- /dev/null +++ b/vlib/v/checker/tests/optional_fn_err.out @@ -0,0 +1,20 @@ +vlib/v/checker/tests/optional_fn_err.vv:11:2: error: foo() returns an option, so it should have either an `or {}` block, or `?` at the end + 9 | fn main() { + 10 | // Calling foo() without ? or an or block, should be an error. + 11 | foo() + | ~~~~~ + 12 | + 13 | _ := bar() +vlib/v/checker/tests/optional_fn_err.vv:13:7: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 11 | foo() + 12 | + 13 | _ := bar() + | ~~~~~ + 14 | _ := [bar()] + 15 | } +vlib/v/checker/tests/optional_fn_err.vv:14:8: error: bar() returns an option, so it should have either an `or {}` block, or `?` at the end + 12 | + 13 | _ := bar() + 14 | _ := [bar()] + | ~~~~~ + 15 | } diff --git a/vlib/v/checker/tests/optional_void_called_as_normal.vv b/vlib/v/checker/tests/optional_fn_err.vv similarity index 68% rename from vlib/v/checker/tests/optional_void_called_as_normal.vv rename to vlib/v/checker/tests/optional_fn_err.vv index 24017c78c2..716e65d5c8 100644 --- a/vlib/v/checker/tests/optional_void_called_as_normal.vv +++ b/vlib/v/checker/tests/optional_fn_err.vv @@ -2,7 +2,14 @@ fn foo() ? { println('foo is called') } +fn bar() ?int { + return none +} + fn main() { // Calling foo() without ? or an or block, should be an error. foo() + + _ := bar() + _ := [bar()] } diff --git a/vlib/v/checker/tests/optional_void_called_as_normal.out b/vlib/v/checker/tests/optional_void_called_as_normal.out deleted file mode 100644 index 4f5c5b5551..0000000000 --- a/vlib/v/checker/tests/optional_void_called_as_normal.out +++ /dev/null @@ -1,6 +0,0 @@ -vlib/v/checker/tests/optional_void_called_as_normal.vv:7:2: error: foo() returns an option, so it should have either an `or {}` block, or `?` at the end - 5 | fn main() { - 6 | // Calling foo() without ? or an or block, should be an error. - 7 | foo() - | ~~~~~ - 8 | }