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

checker: fix illegal result propagate on non-result type (fix #15574) (#15578)

This commit is contained in:
ChAoS_UnItY 2022-08-28 18:31:45 +08:00 committed by GitHub
parent 258ff73efd
commit ad76c9c719
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 43 additions and 6 deletions

View File

@ -931,6 +931,9 @@ pub fn (mut c Checker) check_expr_opt_call(expr ast.Expr, ret_type ast.Type) ast
} else if expr.or_block.kind == .propagate_option {
c.error('unexpected `?`, the function `$expr.name` does not return an optional',
expr.or_block.pos)
} else if expr.or_block.kind == .propagate_result {
c.error('unexpected `!`, the function `$expr.name` does not return an optional',
expr.or_block.pos)
}
} else if expr is ast.IndexExpr {
if expr.or_expr.kind != .absent {
@ -2120,6 +2123,9 @@ pub fn (mut c Checker) expr(node_ ast.Expr) ast.Type {
} else if node.or_block.kind == .propagate_option {
c.error('unexpected `?`, the function `$node.name` does neither return an optional nor a result',
node.or_block.pos)
} else if node.or_block.kind == .propagate_result {
c.error('unexpected `!`, the function `$node.name` does neither return an optional nor a result',
node.or_block.pos)
}
}
if node.or_block.kind != .absent {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/result_missing_propagate_err.vv:6:7: error: result() returns a result, so it should have either an `or {}` block, or `!` at the end
4 |
5 | fn main() {
6 | a := result()
| ~~~~~~~~
7 | println(a)
8 | }

View File

@ -0,0 +1,8 @@
fn result() !int {
return 1
}
fn main() {
a := result()
println(a)
}

View File

@ -1,7 +1,14 @@
vlib/v/checker/tests/wrong_propagate_ret_type.vv:6:17: error: to propagate the optional call, `opt_call` must return an optional
4 |
5 | fn opt_call() int {
6 | a := ret_none()?
vlib/v/checker/tests/wrong_propagate_ret_type.vv:10:17: error: to propagate the optional call, `opt_call` must return an optional
8 |
9 | fn opt_call() int {
10 | a := ret_none()?
| ^
7 | return a
8 | }
11 | return a
12 | }
vlib/v/checker/tests/wrong_propagate_ret_type.vv:15:17: error: unexpected `!`, the function `ret_bool` does neither return an optional nor a result
13 |
14 | fn res_call() bool {
15 | a := ret_bool()!
| ^
16 | return a
17 | }

View File

@ -2,7 +2,16 @@ fn ret_none() ?int {
return none
}
fn ret_bool() bool {
return true
}
fn opt_call() int {
a := ret_none()?
return a
}
fn res_call() bool {
a := ret_bool()!
return a
}