From fce4199412cbc66a538423e84b2882c5f28c105e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 3 Oct 2019 01:28:24 +0300 Subject: [PATCH] compiler: let methods return voidptr without a segfault --- compiler/gen_c.v | 10 +++++++--- compiler/tests/return_voidptr_test.v | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 compiler/tests/return_voidptr_test.v diff --git a/compiler/gen_c.v b/compiler/gen_c.v index 038ad578cb..5a620463c0 100644 --- a/compiler/gen_c.v +++ b/compiler/gen_c.v @@ -238,9 +238,13 @@ fn (p mut Parser) gen_method_call(receiver_type, ftyp string, cgen_name string, // Method returns (void*) => cast it to int, string, user etc // number := *(int*)numbers.first() if ftyp == 'void*' { - // array_int => int - cast = receiver_type.all_after('_') - cast = '*($cast*) ' + if receiver_type.starts_with('array_') { + // array_int => int + cast = receiver_type.all_after('_') + cast = '*($cast*) ' + }else{ + cast = '(voidptr) ' + } } p.cgen.set_placeholder(method_ph, '$cast $method_call') //return method_call diff --git a/compiler/tests/return_voidptr_test.v b/compiler/tests/return_voidptr_test.v new file mode 100644 index 0000000000..f7d6947f1d --- /dev/null +++ b/compiler/tests/return_voidptr_test.v @@ -0,0 +1,23 @@ + +struct Zest { val int } + +fn (t Zest) get_a_finger_to_the_moon() voidptr { + return voidptr(0) +} + +fn get_the_dao_way() voidptr { + return voidptr(0) +} + +fn test_returning_a_void_pointer_from_a_method() { + t := &Zest{ val: 123 } + z := voidptr(0) + assert z == t.get_a_finger_to_the_moon() + assert t.get_a_finger_to_the_moon() == 0 +} + +fn test_returning_a_void_pointer_from_a_function() { + z := voidptr(0) + assert z == get_the_dao_way() + assert get_the_dao_way() == 0 +}