checker: make optional array element an error (#9343)

pull/9352/head
zakuro 2021-03-18 22:11:51 +09:00 committed by GitHub
parent a6c2c5ba88
commit 4ae2c22c18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 7 deletions

View File

@ -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 {

View File

@ -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 | }

View File

@ -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()]
}

View File

@ -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 | }