1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: fix missing check for or-block on selectorexpr (#17975)

This commit is contained in:
Felipe Pena 2023-04-17 11:11:34 -03:00 committed by GitHub
parent fe4ccbc4cf
commit 3b2e58eace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 3 deletions

View File

@ -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) c.expected_or_type = ret_type.clear_flags(.option, .result)
last_stmt_typ := c.expr(stmt.expr) last_stmt_typ := c.expr(stmt.expr)
if ret_type.has_flag(.option) if last_stmt_typ.has_flag(.option) || last_stmt_typ == ast.none_type {
&& (last_stmt_typ.has_flag(.option) || last_stmt_typ == ast.none_type) {
if stmt.expr in [ast.Ident, ast.SelectorExpr, ast.CallExpr, ast.None] { 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, expected_type_name := c.table.type_to_str(ret_type.clear_flags(.option,
.result)) .result))
@ -1440,6 +1439,7 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
if node.or_block.kind == .block { if node.or_block.kind == .block {
c.expected_or_type = node.typ.clear_flags(.option, .result) c.expected_or_type = node.typ.clear_flags(.option, .result)
c.stmts_ending_with_expression(node.or_block.stmts) 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 c.expected_or_type = ast.void_type
} }
return field.typ return field.typ

View File

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

View File

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

View File

@ -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 6 | `ő`: 25
7 | } 7 | }
8 | println(m[`4`] or { none }) 8 | println(m[`4`] or { none })