changeset 29348:4de13dc0eff2 stable

fix symbol lookup issue with anonymous functions (bug #55989) * pt-fcn-handle.cc (tree_anon_fcn_handle::evaluate): Treat anonymous functions defined inside functions as if they are nested functions. * nest.tst: New test. * test/nest/bug_59989.m: New function for test. * test/nest/module.mk: Update.
author John W. Eaton <jwe@octave.org>
date Tue, 02 Feb 2021 15:59:38 -0500
parents cabb840fc3f6
children 0998695317d8 72e75857a5c6
files libinterp/parse-tree/pt-fcn-handle.cc test/nest/bug_59989.m test/nest/module.mk test/nest/nest.tst
diffstat 4 files changed, 25 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/pt-fcn-handle.cc	Wed Jan 27 09:29:26 2021 -0500
+++ b/libinterp/parse-tree/pt-fcn-handle.cc	Tue Feb 02 15:59:38 2021 -0500
@@ -156,6 +156,16 @@
         // FIXME: maybe it would be better to just stash curr_fcn
         // instead of individual bits of info about it?
 
+        // An anonymous function defined inside another nested function
+        // or parent of a nested function also behaves like a nested
+        // function.
+
+        if (curr_fcn->is_parent_function () || curr_fcn->is_nested_function ())
+          {
+            af->mark_as_nested_function ();
+            new_scope.set_nesting_depth (parent_scope.nesting_depth () + 1);
+          }
+
         af->stash_parent_fcn_name (curr_fcn->name ());
         af->stash_dir_name (curr_fcn->dir_name ());
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/nest/bug_59989.m	Tue Feb 02 15:59:38 2021 -0500
@@ -0,0 +1,12 @@
+function y = bug_59989 ()
+  unpacker = @(x) sum (x);
+  function y = nest1 (x, a)
+    y = a + unpacker (x);
+  endfunction
+  function y = nest2 (fh)
+    x = [2, 3];
+    y = fh (x);
+  endfunction
+  a = 1;
+  y = nest2 (@(x) nest1 (x, a));
+endfunction
--- a/test/nest/module.mk	Wed Jan 27 09:29:26 2021 -0500
+++ b/test/nest/module.mk	Tue Feb 02 15:59:38 2021 -0500
@@ -1,6 +1,7 @@
 nest_TEST_FILES = \
   %reldir%/arg_nest.m \
   %reldir%/arg_ret.m \
+  %reldir%/bug_59989.m \
   %reldir%/counter.m \
   %reldir%/nest.tst \
   %reldir%/nest_eval.m \
--- a/test/nest/nest.tst	Wed Jan 27 09:29:26 2021 -0500
+++ b/test/nest/nest.tst	Tue Feb 02 15:59:38 2021 -0500
@@ -158,3 +158,5 @@
 ## Test visibility of nested function from script called from parent.
 %!assert (script_nest_2 (42), 84);
 %!error script_nest_2 (0)
+
+%!assert (bug_59989 (), 6)