zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.w.stephenson@ntlworld.com>
To: Martijn Dekker <martijn@inlv.org>
Cc: zsh-workers@zsh.org
Subject: Re: [BUG] emulate sh: arith assignment assigns variable type
Date: Sat, 2 Jan 2016 18:11:26 +0000	[thread overview]
Message-ID: <20160102181126.364cc450@ntlworld.com> (raw)
In-Reply-To: <5686DB95.2090602@inlv.org>

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


  reply	other threads:[~2016-01-02 18:11 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-01 20:03 Martijn Dekker
2016-01-02 18:11 ` Peter Stephenson [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20160102181126.364cc450@ntlworld.com \
    --to=p.w.stephenson@ntlworld.com \
    --cc=martijn@inlv.org \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).