bugGNU Octave - Bugs: bug #48086, 2nd-level nested function not...


bug #48086: 2nd-level nested function not found in anonymous function

Submitter:  Marco Caliari <caliari>
Submitted:  Thu 02 Jun 2016 08:03:19 AM UTC
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Matlab Compatibility
Status:  Confirmed Assigned to:  None
Originator Name:  Open/Closed:  * Open
Release:  * dev Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup

Mon 04 Mar 2019 09:24:57 AM UTC, comment #5: 

I'm attaching an incomplete work-in-progress patch.  It appears to make the example Rik posted work, but causes other trouble for the test suite.  I believe this is the right direction to go, but the details are not all correct yet.

(file #46412)

John W. Eaton <jwe>
Group administrator
Mon 04 Mar 2019 12:36:37 AM UTC, comment #4: 

Rik: Thanks, I see it is attached now.  I think the problem is that anonymous functions are not (yet) proper closures and they are not properly grabbing all the context necessary to execute outside of the scope where they are defined.  It works most of the time, but as you can see, when they call a nested function the call stack is not right.  I see that the following modification does work:

function main
  disp('main function')
  a = 1;
  function z = nested(x)
    z = a+x;
    function y = nested2(x,a)
% here it does not work
      y = a.*sin(x);
    function r = wrapper (q)
      r = nested2 (q, a);

Making anonymous functions behave more like a real nested functions should solve this problem.  In the meantime, it appears there are several ways to work around it.

John W. Eaton <jwe>
Group administrator
Sun 03 Mar 2019 11:22:26 PM UTC, comment #3: 

Rik: I don't see the new test file mentioned in comment #2.

John W. Eaton <jwe>
Group administrator
Sun 03 Mar 2019 09:55:45 PM UTC, comment #2: 

Support for handles to nested functions has been added to the next release of Octave (6.0.0).  The test code in this bug report will work if the nested function does not include a disp() function call.  But if it does, there is a nasty error message printed.

octave:1> nesttst
main function
error: internal error: invalid access link in function call stack
error: quad: evaluation of user-supplied function failed
error: called from
    nesttst>nested2 at line 15 column 7
    nested>@<anonymous> at line 8 column 15
    nesttst>nested at line 8 column 5
    nesttst at line 4 column 3

I'm attaching a new m-file called nesttst.m which demonstrates the failure. 

Rik <rik5>
Group administrator
Thu 02 Jun 2016 11:01:26 AM UTC, comment #1: 

Thanks, Marco.

I've checked that nested2 can be accessed directly (not as part of an anonymous function) and that a first-level nested functions can form part of an anonymous function.

I don't think it has got anything to do with being unable to take handles, because it works if this is nested one layer deep instead of two layers deep.

topic: "nested function not found"
-> "2nd-level nested function not found in anonymous function"

Lachlan Andrew <lachlan>
Thu 02 Jun 2016 08:03:19 AM UTC, original submission:  

Dear all,

the included code (quite ugly, I admit) does not work on 4.0.0 and 4.1.0+ (nested2 function not found), while it works in Matlab. Is it related to "handles to nested functions not yet supported"? Anyway, I do not get such an error message.

Marco Caliari <caliari>
Group Member


(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:

Attached Files
file #46412:  anon-fcn-diffs.txt added by jwe (26KiB - text/plain)
file #46408:  nesttst.m added by rik5 (610B - text/x-matlab)
file #37348:  main.m added by caliari (364B - text/x-objcsrc)


Depends on the following items: None found

   bug dependencies.


Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by jwe (Updated the item)
  • -email is unavailable- added by lachlan (Posted a comment)
  • -email is unavailable- added by caliari (Submitted the item)

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.


    Follow 8 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2020-06-09 pantxo Dependencies- bugs #58523 is dependent
    2019-03-04 jwe Attached File- Added anon-fcn-diffs.txt, #46412
    2019-03-03 rik5 Attached File- Added nesttst.m, #46408
    2016-07-29 jwe CategoryNone Interpreter
    2016-06-02 lachlan StatusNone Confirmed
        Release4.0.0 dev
        Summarynested function not found 2nd-level nested function not found in anonymous function
    2016-06-02 caliari Attached File- Added main.m, #37348

    Back to the top

    Powered by Savane 3.13-b921.
    Corresponding source code