Skip to content

Commit f20fe19

Browse files
authored
checker: fix wrapped fn aliases in multiple returns (#27977)
1 parent 6fd6cb0 commit f20fe19

2 files changed

Lines changed: 34 additions & 6 deletions

File tree

vlib/v/ast/table.v

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,10 @@ fn (t &Table) method_types_are_equal(left Type, right Type) bool {
533533
return unaliased_left == unaliased_right
534534
}
535535

536-
fn (t &Table) method_fn_return_types_are_compatible(left Type, right Type) bool {
536+
fn (t &Table) method_return_types_are_compatible(left Type, right Type) bool {
537+
if t.method_types_are_equal(left, right) {
538+
return true
539+
}
537540
unaliased_left := t.fully_unaliased_type(left)
538541
unaliased_right := t.fully_unaliased_type(right)
539542
if unaliased_left.nr_muls() != unaliased_right.nr_muls() {
@@ -552,15 +555,22 @@ fn (t &Table) method_fn_return_types_are_compatible(left Type, right Type) bool
552555
}
553556
return t.fn_types_are_compatible(left_sym.info.func, right_sym.info.func, 0)
554557
}
558+
if left_sym.info is MultiReturn && right_sym.info is MultiReturn {
559+
if left_sym.info.types.len != right_sym.info.types.len {
560+
return false
561+
}
562+
for i, typ in left_sym.info.types {
563+
if !t.method_return_types_are_compatible(typ, right_sym.info.types[i]) {
564+
return false
565+
}
566+
}
567+
return true
568+
}
555569
return false
556570
}
557571

558572
pub fn (t &Table) is_same_method(f &Fn, func &Fn) string {
559-
mut same_return_type := t.method_types_are_equal(f.return_type, func.return_type)
560-
if !same_return_type {
561-
same_return_type = t.method_fn_return_types_are_compatible(f.return_type, func.return_type)
562-
}
563-
if !same_return_type {
573+
if !t.method_return_types_are_compatible(f.return_type, func.return_type) {
564574
s := t.type_to_str(f.return_type)
565575
return 'expected return type `${s}`'
566576
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type Msg = string
2+
3+
type Cmd = fn () ?Msg
4+
5+
interface Model {
6+
update(msg Msg) (Model, ?Cmd)
7+
}
8+
9+
struct ExampleModel {}
10+
11+
fn (_ ExampleModel) update(msg Msg) (Model, ?Cmd) {
12+
_ = msg
13+
panic('not implemented')
14+
}
15+
16+
fn test_interface_method_multi_return_option_fn_alias() {
17+
_ := Model(ExampleModel{})
18+
}

0 commit comments

Comments
 (0)