changeset 28699:61a089ada620

maint: merge stable to default.
author John W. Eaton <jwe@octave.org>
date Wed, 09 Sep 2020 13:45:32 -0400
parents 8d04d7c58d49 (current diff) d45d1b4bb919 (diff)
children 712666d48b23
files libinterp/parse-tree/oct-parse.yy libinterp/parse-tree/parse.h test/module.mk
diffstat 9 files changed, 140 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/oct-parse.yy	Tue Sep 08 23:45:14 2020 -0400
+++ b/libinterp/parse-tree/oct-parse.yy	Wed Sep 09 13:45:32 2020 -0400
@@ -2339,7 +2339,15 @@
     full_name += name;
 
     if (m_all_names.find (full_name) != m_all_names.end ())
-      return false;
+      {
+        // Return false (failure) if we are parsing a subfunction, local
+        // function, or nested function.  Otherwise, it is OK to have a
+        // duplicate name.
+
+        return ! (m_parser.parsing_subfunctions ()
+                  || m_parser.parsing_local_functions ()
+                  || m_parser.curr_fcn_depth () > 0);
+      }
 
     m_all_names.insert (full_name);
 
@@ -2381,7 +2389,7 @@
       m_fcn_file_from_relative_lookup (false),
       m_parsing_subfunctions (false), m_parsing_local_functions (false),
       m_max_fcn_depth (-1), m_curr_fcn_depth (-1), m_primary_fcn_scope (),
-      m_curr_class_name (), m_curr_package_name (), m_function_scopes (),
+      m_curr_class_name (), m_curr_package_name (), m_function_scopes (*this),
       m_primary_fcn (), m_subfunction_names (), m_classdef_object (),
       m_stmt_list (), m_lexer (lxr), m_parser_state (yypstate_new ())
   { }
--- a/libinterp/parse-tree/parse.h	Tue Sep 08 23:45:14 2020 -0400
+++ b/libinterp/parse-tree/parse.h	Wed Sep 09 13:45:32 2020 -0400
@@ -113,7 +113,11 @@
       typedef std::deque<value_type>::reverse_iterator reverse_iterator;
       typedef std::deque<value_type>::const_reverse_iterator const_reverse_iterator;
 
-      parent_scope_info (void) = default;
+      parent_scope_info (void) = delete;
+
+      parent_scope_info (base_parser& parser)
+        : m_parser (parser), m_info (), m_all_names ()
+      { }
 
       parent_scope_info (const parent_scope_info&) = default;
 
@@ -141,6 +145,7 @@
 
     private:
 
+      base_parser& m_parser;
       std::deque<value_type> m_info;
       std::set<std::string> m_all_names;
     };
@@ -180,6 +185,16 @@
       return m_stmt_list;
     }
 
+    void parsing_subfunctions (bool flag)
+    {
+      m_parsing_subfunctions = flag;
+    }
+
+    bool parsing_subfunctions (void) const
+    {
+      return m_parsing_subfunctions;
+    }
+
     void parsing_local_functions (bool flag)
     {
       m_parsing_local_functions = flag;
@@ -190,6 +205,11 @@
       return m_parsing_local_functions;
     }
 
+    int curr_fcn_depth (void) const
+    {
+      return m_curr_fcn_depth;
+    }
+
     void endfunction_found (bool flag)
     {
       m_endfunction_found = flag;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-52851/bug-52851.tst	Wed Sep 09 13:45:32 2020 -0400
@@ -0,0 +1,24 @@
+%!test <52851>
+%! script1
+%! assert (r11, 1);
+%! assert (r21, 2);
+%! assert (r22, 2);
+
+%!test <52851>
+%! script2
+%! assert (r1, 1);
+%! assert (r2, 2);
+
+%!test <52851>
+%! flag = true;
+%! script3
+%! assert (r, 1);
+%! flag = false;
+%! script3
+%! assert (r, 2);
+
+%!test <52851>
+%! script4
+%! assert (r1, 1);
+%! assert (r2, 2);
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-52851/module.mk	Wed Sep 09 13:45:32 2020 -0400
@@ -0,0 +1,8 @@
+bug_52851_TEST_FILES = \
+  %reldir%/bug-52851.tst \
+  %reldir%/script1.m \
+  %reldir%/script2.m \
+  %reldir%/script3.m \
+  %reldir%/script4.m
+
+TEST_FILES += $(bug_52851_TEST_FILES)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-52851/script1.m	Wed Sep 09 13:45:32 2020 -0400
@@ -0,0 +1,31 @@
+1; %script identifier
+
+function r = caller (fun, num)
+  persistent funarray
+  if (isempty (fun))
+    r = funarray(num).fun ();
+  else
+    if (isempty (funarray))
+      funarray(1).fun = fun;
+    else
+      funarray(num).fun = fun;
+    endif
+  endif
+endfunction
+
+function r = computation ()
+  r = 1;
+endfunction
+
+caller (@computation, 1);
+
+r11 = caller ([], 1);
+
+function r = computation ()
+  r = 2;
+endfunction
+
+caller (@computation, 2);
+
+r21 = caller ([], 1);
+r22 = caller ([], 2);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-52851/script2.m	Wed Sep 09 13:45:32 2020 -0400
@@ -0,0 +1,17 @@
+1; %script identifier
+
+function r = caller (fun)
+  r = fun ();
+endfunction
+
+function r = computation ()
+  r = 1;
+endfunction
+
+r1 = caller (@computation);
+
+function r = computation ()
+  r = 2;
+endfunction
+
+r2 = caller (@computation);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-52851/script3.m	Wed Sep 09 13:45:32 2020 -0400
@@ -0,0 +1,15 @@
+1; %script identifier
+
+if (flag)
+  function r = computation ()
+    r = 1;
+  endfunction
+
+  r = computation ();
+else
+  function r = computation ()
+    r = 2;
+  endfunction
+
+  r = computation ();
+endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/bug-52851/script4.m	Wed Sep 09 13:45:32 2020 -0400
@@ -0,0 +1,13 @@
+1; %script identifier
+
+function r = computation ()
+  r = 1;
+endfunction
+
+r1 = computation ();
+
+function r = computation ()
+  r = 2;
+endfunction
+
+r2 = computation ();
--- a/test/module.mk	Tue Sep 08 23:45:14 2020 -0400
+++ b/test/module.mk	Wed Sep 09 13:45:32 2020 -0400
@@ -77,6 +77,7 @@
 include %reldir%/bug-51599/module.mk
 include %reldir%/bug-52075/module.mk
 include %reldir%/bug-52722/module.mk
+include %reldir%/bug-52851/module.mk
 include %reldir%/bug-53027/module.mk
 include %reldir%/bug-53468/module.mk
 include %reldir%/bug-53956/module.mk