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

checker: fix returning expression with void type (fix #15543) (#15554)

This commit is contained in:
ChAoS_UnItY 2022-08-27 14:00:14 +08:00 committed by GitHub
parent 70de4e1009
commit 0b843b801f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 7 deletions

View File

@ -321,7 +321,7 @@ fn C.WriteConsole() voidptr
fn C.WriteFile() voidptr
fn C._wchdir(dirname &u16)
fn C._wchdir(dirname &u16) int
fn C._wgetcwd(buffer &u16, maxlen int) int

View File

@ -194,6 +194,14 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
node.typ = c.expected_type
}
}
if last_expr.typ == ast.void_type && !is_noreturn_callexpr(last_expr.expr)
&& !c.skip_flags {
// cannot return void type and use it as expr in any circumstances
// (e.g. argument expression, variable declaration / assignment)
c.error('the final expression in `if` or `match`, must have a value of a non-void type',
last_expr.pos)
continue
}
if !c.check_types(last_expr.typ, node.typ) {
if node.typ == ast.void_type {
// first branch of if expression

View File

@ -19,14 +19,21 @@ vlib/v/checker/tests/if_match_result.vv:7:3: error: `if` expression requires an
8 | }
9 |
vlib/v/checker/tests/if_match_result.vv:11:3: error: assignment mismatch: 1 variable(s) 0 value(s)
9 |
9 |
10 | // void results
11 | _ = match 4 {
| ^
12 | 1 {println('')}
13 | else {exit(0)}
12 | 1 { println('') }
13 | else { exit(0) }
vlib/v/checker/tests/if_match_result.vv:16:2: error: the final expression in `if` or `match`, must have a value of a non-void type
14 | }
15 | _ = if true {
16 | println('')
| ~~~~~~~~~~~
17 | } else {
18 | exit(0)
vlib/v/checker/tests/if_match_result.vv:15:3: error: assignment mismatch: 1 variable(s) 0 value(s)
13 | else {exit(0)}
13 | else { exit(0) }
14 | }
15 | _ = if true {
| ^

View File

@ -9,8 +9,8 @@ _ = if true {
// void results
_ = match 4 {
1 {println('')}
else {exit(0)}
1 { println('') }
else { exit(0) }
}
_ = if true {
println('')