From 69ce8baefda213911d21711363e1c9b2b72e6cfb Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Wed, 16 Jun 2021 11:07:04 +0300 Subject: [PATCH] v.ast: allow for `fn f()&IAbc{return voidptr(0)}` with no additional voidptr implicit conversions to compile --- vlib/v/ast/table.v | 3 +++ ...turning_voidptr_casted_as_interface_test.v | 24 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 vlib/v/tests/interface_edge_cases/fn_returning_voidptr_casted_as_interface_test.v diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index e7eda000bf..6da76c1b94 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -1122,6 +1122,9 @@ fn (mut table Table) does_type_implement_interface(typ Type, inter_typ Type) boo return false } inter_sym.info.types << utyp + if !inter_sym.info.types.contains(voidptr_type) { + inter_sym.info.types << voidptr_type + } } return true } diff --git a/vlib/v/tests/interface_edge_cases/fn_returning_voidptr_casted_as_interface_test.v b/vlib/v/tests/interface_edge_cases/fn_returning_voidptr_casted_as_interface_test.v new file mode 100644 index 0000000000..c66585e9c6 --- /dev/null +++ b/vlib/v/tests/interface_edge_cases/fn_returning_voidptr_casted_as_interface_test.v @@ -0,0 +1,24 @@ +interface IAbc { + name string + xyz() +} + +fn f(i &IAbc) string { + return '$i' +} + +struct Abc { + name string + x int = 123 +} + +fn (a Abc) xyz() {} + +fn resource__null() &IAbc { + return voidptr(0) +} + +fn test_fn_returning_voidptr_casted_as_interface_works() { + pi := resource__null() + assert f(pi) == '&IAbc(0)' +}