bugGNU Octave - Bugs: bug #53871, In-place increment of a workspace...

 
 

bug #53871: In-place increment of a workspace variable with += in a script issues error

Submitter:  Richard Kirk <richardkirk>
Submitted:  Thu 10 May 2018 01:35:25 PM UTC
   
 
Category:  Interpreter Severity:  3 - Normal
Priority:  3 - Low Item Group:  Unexpected Error or Warning
Status:  Duplicate Assigned to:  None
Originator Name:  Richard Kirk Open/Closed:  * Closed
Release:  * 4.4.0 Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Jump to the original submission

Sat 12 May 2018 08:19:21 AM UTC, comment #9: 

Last comment, then I'm done...

The 'right thing' might be to make a line in the script do the same thing as a line typed at the prompt by default. If the -traditional flag is set, we do the Matlab-compatible thing, and don't allow '+=' and similar operators that cause this problem.

Richard Kirk <richardkirk>
Fri 11 May 2018 09:15:26 AM UTC, comment #8: 

Following comment #5...

A more general statement would be...

The problem is when the var. is defined outside the script, then += incremented within a script.

If I understand things...

At parse time, the line...

count += 1;


..gets parsed using the command syntax as...

count ("+=", "1;")


...whereas the line...

count = 1;


..does not get parsed using the command syntax as...

count ("=", "1;")


..presumably due to some MatLab magic if the first argument begins with a recognised assignment operator.

This is a bit weird. I would have been tempted to try parsing the line as an expression, and then fall through to the command syntax if that fails. However, as MatLab compatibility takes priority, we cannot change this special case. In which case the bug changes to...

When the var. is defined outside the script, then += incremented within a script, we get an obscure error message of the form...

error: count(61,_): but count has size 1x1


In cases where the first parsed argument is a valid Octave assignment that is not supported in MatLab, we could suggest re-writing to assign using '=', or to remove spaces between the var. name and the following operator.

There are some other quirky cases...
This works...

count+=1;


This is legal but does not work..

count ++;


..but this does..

count++;


It's all sort-of logical once you know what it is doing, but it is a bit of a shock when you meet it for the first time. But there are lots of workarounds.





Richard Kirk <richardkirk>
Thu 10 May 2018 05:38:47 PM UTC, comment #7: 

I think this can be closed entirely as a duplicate of bug #47676.

Mike Miller <mtmiller>
Group Member
Thu 10 May 2018 03:45:25 PM UTC, comment #6: 

Confirmed.

jwe's analysis from comment #3 looks accurate.  As a way to test it, I made count a global variable both at the command line,


global count
count = 0

, and in dummy.m


global count
count += 1


This works because when parsing dummy.m the "global" modifier signals to the parser that count is a variable and not a function call.

You can trick the parser in to recognizing that count is a variable by using self-assignemnt in dummy.m.


count = count;
count += 1


That is really weird though.  You are probably better off just using


count = count + 1;


for the time being.

I'm lowering the priority since there are lots of work arounds.

Rik <rik5>
Group administrator
Thu 10 May 2018 03:23:42 PM UTC, comment #5: 

ok. Just to make it clear: The problam is when the var. is defined at the command prompt and then incrumented from a script.

This works in an M file.

count=3

for k=1:4
  count += 1
endfor

But if you define count outside of the M file then it can't parse the M file

Doug Stewart <dastew>
Thu 10 May 2018 02:59:19 PM UTC, comment #4: 

Also, I believe the reason that it worked in that old version of Octave is that back then, scripts weren't parsed into an intermediate form and/or command syntax was not handled in the same way.  So the fix is not as simple as going back to the what we were doing then.


John W. Eaton <jwe>
Group administrator
Thu 10 May 2018 02:57:16 PM UTC, comment #3: 

Yes, this is confusing, and an unfortunate side effect of trying to provide both the "+=" operator and also parse "count += 1" in a Matlab-compatible way.  When the script is parsed, Octave doesn't know that count is a variable, so that += expression is parsed as "command syntax", so it is treated as if it were


count ("+=", "1")


Then, when the script is executed in the context where count is defined as a variable, that indexing operation doesn't make sense.  You see 61 in the error message because that's the ASCII code for the = character, and I think it is just complaining about the maximum index that is out of bounds.

I'm not sure whether this would be easy to fix to work in the expected way.  Maybe we can at least provide a more meaningful error message.




John W. Eaton <jwe>
Group administrator
Thu 10 May 2018 02:23:07 PM UTC, comment #2: 

I have had a look at bug #47676.

I tried re-running my test without setting the 'count' variable...


octave:1> count += 1;
warning: the 'count' function is not yet implemented in Octave

Please read <https://www.octave.org/missing.html> to learn how you can
contribute missing functionality.
error: 'count' undefined near line 1 column 1
octave:1> dummy
warning: the 'count' function is not yet implemented in Octave

Please read <https://www.octave.org/missing.html> to learn how you can
contribute missing functionality.
error: 'count' undefined near line 2 column 1
error: called from
    dummy at line 2 column 1


They both react to 'count' being undefined in a similar (though not identical) way.

If I change 'dummy' to...


count = count + 1;


...then I get the same error message as incrementing 'count' at the octave prompt. This suggests to me that the interpreter is handling the case where 'count' does not exist sensibly, but is doing something that makes no sense to me when it does exist. That would put it somewhere between the two bugs for me, but I am way out of my depth here.


Richard Kirk <richardkirk>
Thu 10 May 2018 01:52:57 PM UTC, comment #1: 

You are correct that it is not quite the same as bug #52363, but I think the same problem has been reported before as bug #47676.

John W. Eaton <jwe>
Group administrator
Thu 10 May 2018 01:35:25 PM UTC, original submission:  

I have a 4.4.0 source build on CenoOS 6.5. Also seen by others on ubuntu 16.04 and octave 4.4.0. Does not happen on my previous 3.4.3 build.

I make a local file "dummy.m" with just 3 lines...

# Increment count

count += 1;


I now try to use += to increment 'count'...


octave:1> count = 0;
octave:2> count += 1;
octave:3> count
count =  1


Okay, that worked. Now let's do that by calling dummy.m...


octave:4> dummy
error: count(61,_): but count has size 1x1
error: called from
    dummy at line 3 column 1


If I replace count += 1; with ++count; in dummy.m then it works.


This seemed similar to bug #52363. Doug Stewart tells me it is new, and needs a separate bug.

Richard Kirk <richardkirk>

 

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

Attach Files:
   
   
Comment:
   

No files currently attached

 

Digest:
   bug dependencies.

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by mtmiller (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by dastew (Posted a comment)
  • -email is unavailable- added by jwe (Posted a comment)
  • -email is unavailable- added by richardkirk (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
    2018-05-10 mtmiller Dependencies- Depends on bugs #47676
    2018-05-10 mtmiller StatusConfirmed Duplicate
        Open/ClosedOpen Closed
    2018-05-10 rik5 CategoryNone Interpreter
        Priority5 - Normal 3 - Low
        Item GroupNone Unexpected Error or Warning
        StatusNone Confirmed
        SummaryIncrement with += gives error in .m file In-place increment of a workspace variable with += in a script issues error

    Back to the top

    Powered by Savane 3.13-f8d8.
    Corresponding source code