bugGNU Octave - Bugs: bug #45969, Inner scopes of nested anonymous...

 
 

bug #45969: Inner scopes of nested anonymous function definitions are not saved.

Submitter:  Olaf Till <i7tiol>
Submitted:  Tue 15 Sep 2015 08:51:13 AM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  None
Originator Name:  Olaf Till Open/Closed:  * Closed
Release:  * dev Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 28 Aug 2018 12:23:26 AM UTC, comment #4: 

I applied your changeset and didn't see any failures from 'make check'.  I pushed it to the development branch here (https://hg.savannah.gnu.org/hgweb/octave/rev/91e1ca0e3a9d).  Marking as fixed and closing report.

Rik <rik5>
Group administrator
Sat 18 Aug 2018 09:35:49 PM UTC, comment #3: 

A refreshed changeset ('bug-45969-version2.cset') is attached. Octave internals have changed since the first version, so there was more to do than adapting to C++11-isms...

In octave_fcn_handle::save_ascii, ::save_binary, ::save_hdf5 I decided to throw an error if the scope isn't valid, since AFAICS this is not supposed to happen. Also, except in ::save_hdf5 (because it was too complex), I re-ordered the code a bit so that no data is written until after the error checks, to avoid writing incomplete data.

BTW, already in the previous code state, in case of errors in the ::save_... functions, sometimes 'false' is returned and sometimes an error is thrown... and the returned 'false' seems never to be checked at all when saving to files. This is probably not ideal. As long as the returned 'false' is not checked, it's probably better to throw errors...

Two of the constructors of tree_anon_scopes are currently not used. If suitable, they can be used for something like

octave::tree_anon_scopes tas;
if (condition)
  tas = octave::tree_anon_scopes (argument);


The test file has been changed according to the way you (Rik) changed the tests for bug #45972.

(file #44808)

Olaf Till <i7tiol>
Group Member
Wed 15 Aug 2018 08:31:58 PM UTC, comment #2: 

Thanks for looking at this, I'll try to refresh the patch at the weekend...

Olaf Till <i7tiol>
Group Member
Wed 15 Aug 2018 05:50:54 PM UTC, comment #1: 

I started to take a look at this but the patch supplied no longer applies cleanly to the development tree.

@Olaf: Could you refresh the patch?  It seems like the base code has changed to use more C++11 features like for() over a collection which might need to be copied in to pt-anon-scopes.cc.

Rik <rik5>
Group administrator
Tue 15 Sep 2015 08:51:13 AM UTC, original submission:  

Anonymous functions handles contain an own scope with the variables they inherited from the parent scope. If anonymous function handles are saved (ascii, binary, or hdf5), the variables of this scope are also saved. But currently, if anonymous function definitions are nested (e.g.

@() @() a

), the variables in the scopes of the 'inner' anonymous functions (variable 'a' in

... @() a

in the example) are not saved.

Simple example for triggering this bug:


octave:1> a = 1;
octave:2> f = @ () @ () a
f =

@() @() a

octave:3> save ("test-ascii.dat", "f")
octave:4> save ("-binary", "test-binary.dat", "f")
octave:5> save ("-hdf5", "test-hdf5.dat", "f")
octave:6> ascii = load ("test-ascii.dat");
octave:7> binary = load ("test-binary.dat");
octave:8> hdf5 = load ("test-hdf5.dat");
octave:9> f () ()
ans =  1
octave:10> ascii.f () ()
error: 'a' undefined near line 1 column 9
error: called from
     at line -1 column -1
octave:10> binary.f () ()
error: 'a' undefined near line 1 column 9
error: called from
     at line -1 column -1
octave:10> hdf5.f () ()
error: 'a' undefined near line 1 column 9
error: called from
     at line -1 column -1
octave:10>


More realistic example for triggering this bug:


octave:1> a = 2;
octave:2> b = 10;
octave:3> c = 20;
octave:4> f1 = @ (f, x) f (x) + a
f1 =

@(f, x) f (x) + a

octave:5> f2 = @ (y) f1 (@ (z) z^2 + b * y, y) + c
f2 =

@(y) f1 (@(z) z ^ 2 + b * y, y) + c

octave:6> save ("test-ascii.dat", "f2")
octave:7> save ("-binary", "test-binary.dat", "f2")
octave:8> save ("-hdf5", "test-hdf5.dat", "f2")
octave:9> ascii = load ("test-ascii.dat");
octave:10> binary = load ("test-binary.dat");
octave:11> hdf5 = load ("test-hdf5.dat");
octave:12> f2 (5)
ans =  97
octave:13> ascii.f2 (5)
error: 'b' undefined near line 1 column 23
error: called from
     at line -1 column -1
     at line -1 column -1
     at line -1 column -1
octave:13> binary.f2 (5)
error: 'b' undefined near line 1 column 23
error: called from
     at line -1 column -1
     at line -1 column -1
     at line -1 column -1
octave:13> hdf5.f2 (5)
error: 'b' undefined near line 1 column 23
error: called from
     at line -1 column -1
     at line -1 column -1
     at line -1 column -1
octave:13>


The fix (single changeset to be attached as soon as I know the bug number) firstly provides a new class, derived from class tree_walker, to collect the variables of all scopes of potentially nested anonymous function handles, secondly lets the

octave_fcn_handle::save_[ascii/binary/hdf5]()

methods use this class for saving anonymous functions, and thirdly provides a test (with the second example above).

Olaf Till <i7tiol>
Group Member

 

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

Attach Files:
   
   
Comment:
   

Attached Files
file #44808:  bug-45969-version2.cset added by i7tiol (21KiB - application/octet-stream)
file #34887:  bug-45969.cset added by i7tiol (19KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by mtmiller (Updated the item)
  • -email is unavailable- added by i7tiol (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 5 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2018-08-28 rik5 StatusPatch Submitted Fixed
        Open/ClosedOpen Closed
    2018-08-18 i7tiol Attached File- Added bug-45969-version2.cset, #44808
    2015-10-08 mtmiller StatusNone Patch Submitted
    2015-09-15 i7tiol Attached File- Added bug-45969.cset, #34887

    Back to the top

    Powered by Savane 3.13-3230.
    Corresponding source code