Skip to content

Commit 1aad481

Browse files
authored
checker: fix mismatch checking when a function returns sumtype as an argument (fix #19325) (#20264)
1 parent 101a461 commit 1aad481

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

vlib/v/checker/check_types.v

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,12 @@ fn (mut c Checker) check_matching_function_symbols(got_type_sym &ast.TypeSymbol,
436436
if !c.check_basic(got_fn.return_type, exp_fn.return_type) {
437437
return false
438438
}
439+
// The check for sumtype in c.check_basic() in the previous step is only for its variant to be subsumed
440+
// So we need to do a second, more rigorous check of the return value being sumtype.
441+
if c.table.final_sym(exp_fn.return_type).kind == .sum_type
442+
&& got_fn.return_type.idx() != exp_fn.return_type.idx() {
443+
return false
444+
}
439445
for i, got_arg in got_fn.params {
440446
exp_arg := exp_fn.params[i]
441447
exp_arg_typ := c.unwrap_generic(exp_arg.typ)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
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`
2+
12 |
3+
13 | fn main() {
4+
14 | event(foo)
5+
| ~~~
6+
15 | }
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// for issue 19325
2+
type Response = int | string
3+
4+
fn foo() string {
5+
return 'hello'
6+
}
7+
8+
fn event(cb fn () Response) {
9+
resp := cb()
10+
assert resp is string
11+
}
12+
13+
fn main() {
14+
event(foo)
15+
}

0 commit comments

Comments
 (0)