zsh-workers
 help / color / mirror / code / Atom feed
* [bug] Math evaluation done twice in subscript in specific conditions
@ 2019-07-23 23:50 Sebastian Gniazdowski
  2019-07-24  5:12 ` Stephane Chazelas
  0 siblings, 1 reply; 3+ messages in thread
From: Sebastian Gniazdowski @ 2019-07-23 23:50 UTC (permalink / raw)
  To: Zsh hackers list

Hello
This is interesting:

idx=0
a=()
: ${a[++idx]::=$idx}
print -rl $a
-> 1
: ${a[++idx]::=$idx}
print -rl $a
-> 1
-> 3
idx=1
: ${a[++idx]::=$idx}
print -rl $a
-> 1
-> 2
-> 3

So apparently the assignment to idx does change something for the
following ::= "call" and makes it work correctly. The second and
following ::= calls cause $idx to increment twice.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [bug] Math evaluation done twice in subscript in specific conditions
  2019-07-23 23:50 [bug] Math evaluation done twice in subscript in specific conditions Sebastian Gniazdowski
@ 2019-07-24  5:12 ` Stephane Chazelas
  2019-07-24  5:33   ` Sebastian Gniazdowski
  0 siblings, 1 reply; 3+ messages in thread
From: Stephane Chazelas @ 2019-07-24  5:12 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list

2019-07-24 01:50:23 +0200, Sebastian Gniazdowski:
[...]
> idx=0
> a=()
> : ${a[++idx]::=$idx}
> print -rl $a
> -> 1
> : ${a[++idx]::=$idx}
> print -rl $a
> -> 1
> -> 3
> idx=1
> : ${a[++idx]::=$idx}
> print -rl $a
> -> 1
> -> 2
> -> 3
> 
> So apparently the assignment to idx does change something for the
> following ::= "call" and makes it work correctly. The second and
> following ::= calls cause $idx to increment twice.
[...]

It is increased by 2 on the first one as well:

$ i=0 a=()
$ : ${a[++i]::=$i}; echo $i $a
2 1
$ : ${a[++i]::=$i}; echo $i $a
4 1 3

With:

$ i='psvar[0`uname>&2`]'
$ : ${a[i]}
Linux
$ : ${a[i]::=}
Linux
Linux
zsh: a: assignment to invalid subscript range

So the extra evaluation of the indice is done *before* the
assignment it would seem.

$ echo ${a[i]:=x}
Linux
Linux
zsh: a: assignment to invalid subscript range

It makes more sense in that latter case where I imaging a[i] is
dereferenced first to check if it's empty or not, and then the
indice is computed again upon assignment. 

-- 
Stephane

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [bug] Math evaluation done twice in subscript in specific conditions
  2019-07-24  5:12 ` Stephane Chazelas
@ 2019-07-24  5:33   ` Sebastian Gniazdowski
  0 siblings, 0 replies; 3+ messages in thread
From: Sebastian Gniazdowski @ 2019-07-24  5:33 UTC (permalink / raw)
  To: Sebastian Gniazdowski, Zsh hackers list

On Wed, 24 Jul 2019 at 07:12, Stephane Chazelas
<stephane.chazelas@gmail.com> wrote:
>
> 2019-07-24 01:50:23 +0200, Sebastian Gniazdowski:
> [...]
> > idx=0
> > a=()
> > : ${a[++idx]::=$idx}
> > print -rl $a
> > -> 1
> > : ${a[++idx]::=$idx}
> > print -rl $a
> > -> 1
> > -> 3
> > idx=1
> > : ${a[++idx]::=$idx}
> > print -rl $a
> > -> 1
> > -> 2
> > -> 3
> >
> > So apparently the assignment to idx does change something for the
> > following ::= "call" and makes it work correctly. The second and
> > following ::= calls cause $idx to increment twice.
> [...]
>
> It is increased by 2 on the first one as well:
>
> $ i=0 a=()
> $ : ${a[++i]::=$i}; echo $i $a
> 2 1
> $ : ${a[++i]::=$i}; echo $i $a
> 4 1 3

Ah, of course.

> It makes more sense in that latter case where I imaging a[i] is
> dereferenced first to check if it's empty or not, and then the
> indice is computed again upon assignment.
When I was debugging Zsh in the past, I've very often occurred double
getter call AFAIR, or at least of something underneath. It is basic to
assume that this would call mathevali twice. I was then thinking that
it is like that by design and didn't report it.

-- 
Sebastian Gniazdowski
News: https://twitter.com/ZdharmaI
IRC: https://kiwiirc.com/client/chat.freenode.net:+6697/#zplugin
Blog: http://zdharma.org

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-07-24  5:34 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-23 23:50 [bug] Math evaluation done twice in subscript in specific conditions Sebastian Gniazdowski
2019-07-24  5:12 ` Stephane Chazelas
2019-07-24  5:33   ` Sebastian Gniazdowski

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).