zsh-workers
 help / color / mirror / code / Atom feed
* [BUG] emulate sh: arith assignment assigns variable type
@ 2016-01-01 20:03 Martijn Dekker
  2016-01-02 18:11 ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Martijn Dekker @ 2016-01-01 20:03 UTC (permalink / raw)
  To: zsh-workers

When an assignment is done to an unset variable using an arithmetic
expression, zsh assigns a numerical or arithmetic type to that variable,
causing subsequent normal shell assignments to be interpreted as
arithmetic expressions.

This causes an incompatibility with other shells under 'emulate sh',
because the POSIX shell is supposed to be a typeless language. In sh
emulation mode, arithmetic assignment should not assign a variable type.

Steps to reproduce:

$ emulate sh
$ X=a:b:c
$ : $((X=1))	# no type assignment as variable was already et
$ X=a:b:c
$ unset X
$ : $((X=1))	# variable now restricted to arith expressions
$ X=a:b:c
zsh: bad math expression: ':' without '?'

Expected behaviour: the last assignment should succeed in sh emulation
mode (and does on all other shells).

Confirmed in zsh 4.1.1 through current.

Thanks, and happy new year.

- Martijn


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

* Re: [BUG] emulate sh: arith assignment assigns variable type
  2016-01-01 20:03 [BUG] emulate sh: arith assignment assigns variable type Martijn Dekker
@ 2016-01-02 18:11 ` Peter Stephenson
  2016-01-02 19:37   ` Mikael Magnusson
                     ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Peter Stephenson @ 2016-01-02 18:11 UTC (permalink / raw)
  To: Martijn Dekker; +Cc: zsh-workers

On Fri, 01 Jan 2016 21:03:33 +0100
Martijn Dekker <martijn@inlv.org> wrote:
> When an assignment is done to an unset variable using an arithmetic
> expression, zsh assigns a numerical or arithmetic type to that variable,
> causing subsequent normal shell assignments to be interpreted as
> arithmetic expressions.

Yes, it does.

Does this need a special option?  The linkage is a bit tenouous.

diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo
index f377539..b5e9100 100644
--- a/Doc/Zsh/options.yo
+++ b/Doc/Zsh/options.yo
@@ -2098,6 +2098,12 @@ When it is unset, zsh allows expressions of the form tt($#)var(name)
 to refer to the length of tt($)var(name), even for special variables,
 for example in expressions such as tt($#-) and tt($#*).
 
+Another difference is that with the option set assignment to an
+unset variable in artihmetic context causes the variable to be created
+as a scalar rather than a numeric type.  So after `tt(unset t; (( t = 3
+)))'. without tt(POSIX_IDENTIFIERS) set tt(t) has integer type and with
+it set it has scalar type.
+
 When the option is unset and multibyte character support is enabled (i.e. it
 is compiled in and the option tt(MULTIBYTE) is set), then additionally any
 alphanumeric characters in the local character set may be used in
diff --git a/Src/params.c b/Src/params.c
index 8cab969..054fb1f 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -3061,6 +3061,7 @@ setnparam(char *s, mnumber val)
 	if (ss)
 	    *ss = '\0';
 	pm = createparam(t, ss ? PM_ARRAY :
+			 isset(POSIXIDENTIFIERS) ? PM_SCALAR :
 			 (val.type & MN_INTEGER) ? PM_INTEGER : PM_FFLOAT);
 	if (!pm)
 	    pm = (Param) paramtab->getnode(paramtab, t);
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index c7bd81f..61da763 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -409,3 +409,14 @@
 >2
 >(eval):6: bad math expression: unexpected ')'
 >(eval):7: bad math expression: unexpected ')'
+
+  unset number
+  (( number = 3 ))
+  print ${(t)number}
+  unset number
+  (setopt posix_identifiers
+  (( number = 3 ))
+  print ${(t)number})
+0:type of variable when created in arithmetic context
+>integer
+>scalar


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

* Re: [BUG] emulate sh: arith assignment assigns variable type
  2016-01-02 18:11 ` Peter Stephenson
@ 2016-01-02 19:37   ` Mikael Magnusson
  2016-01-02 20:46   ` Bart Schaefer
  2016-01-13  3:13   ` Martijn Dekker
  2 siblings, 0 replies; 6+ messages in thread
From: Mikael Magnusson @ 2016-01-02 19:37 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Martijn Dekker, zsh workers

On Sat, Jan 2, 2016 at 7:11 PM, Peter Stephenson
<p.w.stephenson@ntlworld.com> wrote:
> On Fri, 01 Jan 2016 21:03:33 +0100
> Martijn Dekker <martijn@inlv.org> wrote:
>> When an assignment is done to an unset variable using an arithmetic
>> expression, zsh assigns a numerical or arithmetic type to that variable,
>> causing subsequent normal shell assignments to be interpreted as
>> arithmetic expressions.
>
> Yes, it does.
>
> Does this need a special option?  The linkage is a bit tenouous.
>
> diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo
> index f377539..b5e9100 100644
> --- a/Doc/Zsh/options.yo
> +++ b/Doc/Zsh/options.yo
> @@ -2098,6 +2098,12 @@ When it is unset, zsh allows expressions of the form tt($#)var(name)
>  to refer to the length of tt($)var(name), even for special variables,
>  for example in expressions such as tt($#-) and tt($#*).
>
> +Another difference is that with the option set assignment to an
> +unset variable in artihmetic context causes the variable to be created

Typo here on artihmetic. I would also put a comma after 'with the option set'?

-- 
Mikael Magnusson


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

* Re: [BUG] emulate sh: arith assignment assigns variable type
  2016-01-02 18:11 ` Peter Stephenson
  2016-01-02 19:37   ` Mikael Magnusson
@ 2016-01-02 20:46   ` Bart Schaefer
  2016-01-02 21:12     ` Peter Stephenson
  2016-01-13  3:13   ` Martijn Dekker
  2 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2016-01-02 20:46 UTC (permalink / raw)
  To: zsh-workers

On Jan 2,  6:11pm, Peter Stephenson wrote:
}
} Does this need a special option?  The linkage is a bit tenouous.

Are there other assignment-time differences?  Maybe a POSIX_ASSIGNMENT
option is in order, though I dislike moving functionality from one
option to another.

On the other hand, is this --

}  	pm = createparam(t, ss ? PM_ARRAY :
} +			 isset(POSIXIDENTIFIERS) ? PM_SCALAR :
}  			 (val.type & MN_INTEGER) ? PM_INTEGER : PM_FFLOAT);

-- really the correct fix?  I was thinking more along the lines of
creating it as an integer but then changing the type back to scalar
at the time of assignment if the assigned string did not parse as an
arithmetic expression.


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

* Re: [BUG] emulate sh: arith assignment assigns variable type
  2016-01-02 20:46   ` Bart Schaefer
@ 2016-01-02 21:12     ` Peter Stephenson
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 2016-01-02 21:12 UTC (permalink / raw)
  To: zsh-workers

On Sat, 2 Jan 2016 12:46:59 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On the other hand, is this --
> 
> }  	pm = createparam(t, ss ? PM_ARRAY :
> } +			 isset(POSIXIDENTIFIERS) ? PM_SCALAR :
> }  			 (val.type & MN_INTEGER) ? PM_INTEGER : PM_FFLOAT);
> 
> -- really the correct fix?  I was thinking more along the lines of
> creating it as an integer but then changing the type back to scalar
> at the time of assignment if the assigned string did not parse as an
> arithmetic expression.

That's obviously not what other POSIX shells do, so I don't see how that
can be right.  It's forcing arithmetic evaaluation in cases those shells
wouldn't.  The POSIX* options aren't supposed to be for anything more
than strict(ish) compatibility.

pws


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

* Re: [BUG] emulate sh: arith assignment assigns variable type
  2016-01-02 18:11 ` Peter Stephenson
  2016-01-02 19:37   ` Mikael Magnusson
  2016-01-02 20:46   ` Bart Schaefer
@ 2016-01-13  3:13   ` Martijn Dekker
  2 siblings, 0 replies; 6+ messages in thread
From: Martijn Dekker @ 2016-01-13  3:13 UTC (permalink / raw)
  To: zsh-workers; +Cc: Peter Stephenson

Peter Stephenson schreef op 02-01-16 om 19:11:
> On Fri, 01 Jan 2016 21:03:33 +0100
> Martijn Dekker <martijn@inlv.org> wrote:
>> > When an assignment is done to an unset variable using an arithmetic
>> > expression, zsh assigns a numerical or arithmetic type to that variable,
>> > causing subsequent normal shell assignments to be interpreted as
>> > arithmetic expressions.
> Yes, it does.
> 
> Does this need a special option?  The linkage is a bit tenouous.

It's working fine in any case. Thanks.

I noticed the fix has made it into the git repository, but without a
corresponding ChangeLog entry. Thought I'd mention it in case that was
an unintentional omission.

- M.


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

end of thread, other threads:[~2016-01-13  3:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-01 20:03 [BUG] emulate sh: arith assignment assigns variable type Martijn Dekker
2016-01-02 18:11 ` Peter Stephenson
2016-01-02 19:37   ` Mikael Magnusson
2016-01-02 20:46   ` Bart Schaefer
2016-01-02 21:12     ` Peter Stephenson
2016-01-13  3:13   ` Martijn Dekker

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).