From 07465a4e7dc72e5416b5c3583fe3bb8ea0c4fe33 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 24 Sep 2024 00:54:02 -0300 Subject: [PATCH] checker: fix fntype var marked as auto heap (#22290) --- vlib/v/checker/checker.v | 7 +++++++ vlib/v/tests/pointers/ref_callback_test.v | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 vlib/v/tests/pointers/ref_callback_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2f0b155b92..40bfc7a099 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4376,6 +4376,13 @@ fn (mut c Checker) mark_as_referenced(mut node ast.Expr, as_interface bool) { } } .sum_type, .interface_ {} + .function { + if type_sym.info is ast.FnType { + if type_sym.info.is_anon { + node.obj.is_auto_heap = true + } + } + } else { node.obj.is_auto_heap = true } diff --git a/vlib/v/tests/pointers/ref_callback_test.v b/vlib/v/tests/pointers/ref_callback_test.v new file mode 100644 index 0000000000..6035f5740c --- /dev/null +++ b/vlib/v/tests/pointers/ref_callback_test.v @@ -0,0 +1,20 @@ +module main + +struct Struct_voidptr { + func voidptr +} + +fn function() string { + return 'Function!' +} + +fn test_main() { + fun := function + + sct := Struct_voidptr{ + func: &fun + } + + assert fun() == 'Function!' + assert sct.func == &fun +}