Skip to content

Commit

Permalink
checker: check fn return void fn call (no value) (fix #22389) (#22404)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 authored Oct 4, 2024
1 parent 5b1581d commit 884b842
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 1 deletion.
4 changes: 3 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -2177,7 +2177,9 @@ fn (mut c Checker) stmt(mut node ast.Stmt) {
}
}
}
c.check_expr_option_or_result_call(node.expr, or_typ)
if !c.inside_return {
c.check_expr_option_or_result_call(node.expr, or_typ)
}
// TODO: This should work, even if it's prolly useless .-.
// node.typ = c.check_expr_option_or_result_call(node.expr, ast.void_type)
}
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/checker/match.v
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ fn (mut c Checker) check_match_branch_last_stmt(last_stmt ast.ExprStmt, ret_type
}
c.error('return type mismatch, it should be `${ret_sym.name}`', last_stmt.pos)
}
} else if expr_type == ast.void_type && ret_type.idx() == ast.void_type_idx
&& ret_type.has_option_or_result() {
c.error('`${last_stmt.expr}` used as value', last_stmt.pos)
}
}

Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/fn_return_void_fn_call_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/fn_return_void_fn_call_err.vv:20:4: error: `install_from_github(unit, verbosity)!` used as value
18 | return match source {
19 | 'github' {
20 | install_from_github(unit, verbosity)!
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 | }
22 | else {
30 changes: 30 additions & 0 deletions vlib/v/checker/tests/fn_return_void_fn_call_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
fn main() {
install_command(['github:some/thing'], 1) or { panic(err) }
}

pub fn install_command(args []string, verbosity int) ! {
if args.len == 0 {
return error('${@FN} requires an argument')
}
component := args[0]
if component.count(':') == 0 {
return install_command(['github:${component}'], verbosity)
}
source := component.all_before(':')
if source != 'github' {
return error('${@FN} unknown source `${source}`')
}
unit := component.all_after(':')
return match source {
'github' {
install_from_github(unit, verbosity)!
}
else {
error('${@FN} unknown source `${source}`')
}
}
}

fn install_from_github(unit string, verbosity int) ! {
dump(unit)
}
31 changes: 31 additions & 0 deletions vlib/v/tests/fns/fn_return_match_expr_with_result_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
fn test_fn_return_match_expr_with_result() {
install_command(['github:some/thing'], 1) or { panic(err) }
assert true
}

pub fn install_command(args []string, verbosity int) ! {
if args.len == 0 {
return error('${@FN} requires an argument')
}
component := args[0]
if component.count(':') == 0 {
return install_command(['github:${component}'], verbosity)
}
source := component.all_before(':')
if source != 'github' {
return error('${@FN} unknown source `${source}`')
}
unit := component.all_after(':')
return match source {
'github' {
install_from_github(unit, verbosity)
}
else {
error('${@FN} unknown source `${source}`')
}
}
}

fn install_from_github(unit string, verbosity int) ! {
dump(unit)
}

0 comments on commit 884b842

Please sign in to comment.