diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c497a0a0f7..012b9d1304 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1127,8 +1127,7 @@ fn (mut c Checker) check_or_last_stmt(stmt ast.Stmt, ret_type ast.Type, expr_ret c.expected_or_type = ret_type.clear_flags(.option, .result) last_stmt_typ := c.expr(stmt.expr) - if ret_type.has_flag(.option) - && (last_stmt_typ.has_flag(.option) || last_stmt_typ == ast.none_type) { + if last_stmt_typ.has_flag(.option) || last_stmt_typ == ast.none_type { if stmt.expr in [ast.Ident, ast.SelectorExpr, ast.CallExpr, ast.None] { expected_type_name := c.table.type_to_str(ret_type.clear_flags(.option, .result)) @@ -1440,6 +1439,7 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type { if node.or_block.kind == .block { c.expected_or_type = node.typ.clear_flags(.option, .result) c.stmts_ending_with_expression(node.or_block.stmts) + c.check_or_expr(node.or_block, node.typ, c.expected_or_type, node) c.expected_or_type = ast.void_type } return field.typ diff --git a/vlib/v/checker/tests/option_selector_or_block_err.out b/vlib/v/checker/tests/option_selector_or_block_err.out new file mode 100644 index 0000000000..d643fd6c16 --- /dev/null +++ b/vlib/v/checker/tests/option_selector_or_block_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/option_selector_or_block_err.vv:9:10: error: `or` block must provide a value of type `[]string`, not `?[]string` + 7 | return post.authors or { + 8 | post.authors = []string{} + 9 | post.authors + | ~~~~~~~ + 10 | } + 11 | } diff --git a/vlib/v/checker/tests/option_selector_or_block_err.vv b/vlib/v/checker/tests/option_selector_or_block_err.vv new file mode 100644 index 0000000000..10854eb305 --- /dev/null +++ b/vlib/v/checker/tests/option_selector_or_block_err.vv @@ -0,0 +1,17 @@ +struct Post { +mut: + authors ?[]string +} + +fn populate(mut post Post) []string { + return post.authors or { + post.authors = []string{} + post.authors + } +} + +fn main() { + mut test := Post {} + populate(mut test) + println(test) +} \ No newline at end of file diff --git a/vlib/v/checker/tests/wrong_none_on_or_block_err.out b/vlib/v/checker/tests/wrong_none_on_or_block_err.out index 30ea39aabe..07ebb28364 100644 --- a/vlib/v/checker/tests/wrong_none_on_or_block_err.out +++ b/vlib/v/checker/tests/wrong_none_on_or_block_err.out @@ -1,4 +1,4 @@ -vlib/v/checker/tests/wrong_none_on_or_block_err.vv:8:22: error: wrong return type `none` in the `or {}` block, expected `int` +vlib/v/checker/tests/wrong_none_on_or_block_err.vv:8:22: error: `or` block must provide a value of type `int`, not `none` 6 | `ő`: 25 7 | } 8 | println(m[`4`] or { none })