From 7a8e7b4db8cbbfc03af5fd08994a979bb1e08512 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 8 Nov 2019 07:43:44 +0300 Subject: [PATCH] fix interface methods that return a value --- vlib/compiler/cgen.v | 1 + vlib/compiler/fn.v | 2 +- vlib/compiler/tests/interface_test.v | 10 ++++++---- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/vlib/compiler/cgen.v b/vlib/compiler/cgen.v index 239eb0222b..ba220acc44 100644 --- a/vlib/compiler/cgen.v +++ b/vlib/compiler/cgen.v @@ -389,6 +389,7 @@ fn sort_structs(types []Type) []Type { return types_sorted } +// Generates interface table and interface indexes fn (v &V) interface_table() string { mut sb := strings.new_builder(100) for _, t in v.table.typesmap { diff --git a/vlib/compiler/fn.v b/vlib/compiler/fn.v index dfdf6308fb..a79951023c 100644 --- a/vlib/compiler/fn.v +++ b/vlib/compiler/fn.v @@ -735,7 +735,7 @@ fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type s p.cgen.resetln('') var := p.expr_var.name iname := f.args[0].typ // Speaker - p.gen('((void (*)())(${iname}_name_table[${var}._interface_idx][$idx]))(${var}._object)') + p.gen('(($f.typ (*)())(${iname}_name_table[${var}._interface_idx][$idx]))(${var}._object)') } } //println('r=$receiver.typ RT=$receiver_type') diff --git a/vlib/compiler/tests/interface_test.v b/vlib/compiler/tests/interface_test.v index f37db18eeb..18364a108b 100644 --- a/vlib/compiler/tests/interface_test.v +++ b/vlib/compiler/tests/interface_test.v @@ -27,16 +27,18 @@ struct Foo { speaker Speaker } -fn perform_speak(s Speaker) bool { +fn perform_speak(s Speaker) { s.speak() - return true + assert true + name := s.name() + assert name == 'Dog' || name == 'Cat' } fn test_perform_speak() { d := Dog{} - assert perform_speak(d) + perform_speak(d) cat := Cat{} - assert perform_speak(cat) + perform_speak(cat) f := Foo { //speaker: d }