Skip to content

Commit

Permalink
checker: fix mismatch checking when a function returns sumtype as an …
Browse files Browse the repository at this point in the history
…argument (fix #19325) (#20264)
  • Loading branch information
shove70 authored Dec 26, 2023
1 parent 101a461 commit 1aad481
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/checker/check_types.v
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,12 @@ fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSymbol,
if !c.check_basic(got_fn.return_type, exp_fn.return_type) {
return false
}
// The check for sumtype in c.check_basic() in the previous step is only for its variant to be subsumed
// So we need to do a second, more rigorous check of the return value being sumtype.
if c.table.final_sym(exp_fn.return_type).kind == .sum_type
&& got_fn.return_type.idx() != exp_fn.return_type.idx() {
return false
}
for i, got_arg in got_fn.params {
exp_arg := exp_fn.params[i]
exp_arg_typ := c.unwrap_generic(exp_arg.typ)
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/tests/fn_call_arg_fn_mismatch_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vlib/v/checker/tests/fn_call_arg_fn_mismatch_err.vv:14:8: error: cannot use `fn () string` as `fn () Response` in argument 1 to `event`
12 |
13 | fn main() {
14 | event(foo)
| ~~~
15 | }
15 changes: 15 additions & 0 deletions vlib/v/checker/tests/fn_call_arg_fn_mismatch_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// for issue 19325
type Response = int | string

fn foo() string {
return 'hello'
}

fn event(cb fn () Response) {
resp := cb()
assert resp is string
}

fn main() {
event(foo)
}

0 comments on commit 1aad481

Please sign in to comment.