zsh-workers
 help / color / mirror / code / Atom feed
* [BUG] POSIX arith: inf, nan should be variables
@ 2021-11-15 17:40 Martijn Dekker
  2021-11-16  9:06 ` Oliver Kiddle
  2021-11-16 12:55 ` Vincent Lefevre
  0 siblings, 2 replies; 7+ messages in thread
From: Martijn Dekker @ 2021-11-15 17:40 UTC (permalink / raw)
  To: Zsh hackers list

$ zsh --emulate sh -c 'inf=1; nan=2; echo $((inf)) $((nan))'
Inf NaN

Expected: 1 2

This is a POSIX compliance issue that ksh93 also has (I've just fixed in 
ksh 93u+m). I can see why this is needed for full floating point 
arithmetic support, but in POSIX sh, all case variants of inf and nan 
are perfectly legitimate variables that portable scripts should be able 
to use, including in arithmetic evaluation. Their recognition should be 
turned off for sh emulation, but not for ksh emulation as ksh93's native 
mode also supports floating point arithmetic.

So, what shell option to tie that to? That's a bit difficult. It needs 
to be an option that is on for sh, but off for ksh (or vice versa). A 
comparison of 'set -o' output between sh and ksh modes shows that the 
options are limited to the following:

bsdecho
ignorebraces
kshglob
kshoptionprint
localoptions
localtraps
octalzeroes
promptbang
sharehistory
singlelinezle

The only one of those that has anything to do with an arithmetic context 
is octalzeroes, so that one may be the least bad one to choose. The 
preliminary patch below does that.

Perhaps octalzeroes could be renamed to something like posixmath, so 
that it becomes legitimate to attach this and any other POSIX arithmetic 
quirks to this option without further crowding the shell option space.

Thoughts/opinions?

--- a/Src/math.c
+++ b/Src/math.c
@@ -863,7 +863,7 @@ zzlex(void)

  		p = ptr;
  		ptr = ie;
-		if (ie - p == 3) {
+		if (ie - p == 3 && !isset(OCTALZEROES)) {
  		    if ((p[0] == 'N' || p[0] == 'n') &&
  			(p[1] == 'A' || p[1] == 'a') &&
  			(p[2] == 'N' || p[2] == 'n')) {

-- 
||	modernish -- harness the shell
||	https://github.com/modernish/modernish
||
||	KornShell lives!
||	https://github.com/ksh93/ksh


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

* Re: [BUG] POSIX arith: inf, nan should be variables
  2021-11-15 17:40 [BUG] POSIX arith: inf, nan should be variables Martijn Dekker
@ 2021-11-16  9:06 ` Oliver Kiddle
  2021-11-16 12:55 ` Vincent Lefevre
  1 sibling, 0 replies; 7+ messages in thread
From: Oliver Kiddle @ 2021-11-16  9:06 UTC (permalink / raw)
  To: Martijn Dekker; +Cc: Zsh hackers list

Martijn Dekker wrote:
> $ zsh --emulate sh -c 'inf=1; nan=2; echo $((inf)) $((nan))'
> Inf NaN
>
> Expected: 1 2

> So, what shell option to tie that to? That's a bit difficult. It needs 

The closest comparable feature is the decision in params.c as to whether
it should setup other special variables that aren't in the standard.

That seems to only check something like !EMULATION(EMULATE_SH) rather
than an actual option. Much of that has to run before zsh has got as far
as sourcing dot files that might setup options so it likely never made
sense to create an option for it. Most uses of directly checking
emulation state seem to be in initialisation code.

I wouldn't rename an option for compatibility. And while OCTAL_ZEROES is
similar in controlling interpretation of numbers in math mode it differs
in that it enables a potentially problematic standard feature while
Inf/NaN are a zsh extension. So I wouldn't view preferences on them as
being intrinsically linked.

Oliver


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

* Re: [BUG] POSIX arith: inf, nan should be variables
  2021-11-15 17:40 [BUG] POSIX arith: inf, nan should be variables Martijn Dekker
  2021-11-16  9:06 ` Oliver Kiddle
@ 2021-11-16 12:55 ` Vincent Lefevre
  2021-11-28 20:34   ` Oliver Kiddle
  1 sibling, 1 reply; 7+ messages in thread
From: Vincent Lefevre @ 2021-11-16 12:55 UTC (permalink / raw)
  To: zsh-workers

On 2021-11-15 18:40:17 +0100, Martijn Dekker wrote:
> $ zsh --emulate sh -c 'inf=1; nan=2; echo $((inf)) $((nan))'
> Inf NaN
> 
> Expected: 1 2

FYI, this had already been discussed in April in this subthread:

  https://www.zsh.org/mla/workers/2021/msg00717.html

-- 
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)


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

* Re: [BUG] POSIX arith: inf, nan should be variables
  2021-11-16 12:55 ` Vincent Lefevre
@ 2021-11-28 20:34   ` Oliver Kiddle
  2021-12-01  3:31     ` Daniel Shahaf
  0 siblings, 1 reply; 7+ messages in thread
From: Oliver Kiddle @ 2021-11-28 20:34 UTC (permalink / raw)
  To: zsh-workers

On 16 Nov, Vincent Lefevre wrote:
> On 2021-11-15 18:40:17 +0100, Martijn Dekker wrote:
> > $ zsh --emulate sh -c 'inf=1; nan=2; echo $((inf)) $((nan))'
> > Inf NaN
> > 
> > Expected: 1 2
>
> FYI, this had already been discussed in April in this subthread:

And it'll probably come up again if we don't do anything. Unless anyone
has better ideas, I'm inclined to go with the following variant of
Martijn's patch.

Oliver

diff --git a/Src/math.c b/Src/math.c
index 4f24361a4..777ad9c31 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -863,7 +863,7 @@ zzlex(void)
 
 		p = ptr;
 		ptr = ie;
-		if (ie - p == 3) {
+		if (ie - p == 3 && !EMULATION(EMULATE_SH)) {
 		    if ((p[0] == 'N' || p[0] == 'n') &&
 			(p[1] == 'A' || p[1] == 'a') &&
 			(p[2] == 'N' || p[2] == 'n')) {


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

* Re: [BUG] POSIX arith: inf, nan should be variables
  2021-11-28 20:34   ` Oliver Kiddle
@ 2021-12-01  3:31     ` Daniel Shahaf
  2021-12-01  3:37       ` Bart Schaefer
  0 siblings, 1 reply; 7+ messages in thread
From: Daniel Shahaf @ 2021-12-01  3:31 UTC (permalink / raw)
  To: zsh-workers

Oliver Kiddle wrote on Sun, Nov 28, 2021 at 21:34:41 +0100:
> On 16 Nov, Vincent Lefevre wrote:
> > On 2021-11-15 18:40:17 +0100, Martijn Dekker wrote:
> > > $ zsh --emulate sh -c 'inf=1; nan=2; echo $((inf)) $((nan))'
> > > Inf NaN
> > > 
> > > Expected: 1 2
> >
> > FYI, this had already been discussed in April in this subthread:
> 
> And it'll probably come up again if we don't do anything. Unless anyone
> has better ideas, I'm inclined to go with the following variant of
> Martijn's patch.

Maybe add a test based on the above code snippet?

Cheers,

Daniel


> 
> diff --git a/Src/math.c b/Src/math.c
> index 4f24361a4..777ad9c31 100644
> --- a/Src/math.c
> +++ b/Src/math.c
> @@ -863,7 +863,7 @@ zzlex(void)
>  
>  		p = ptr;
>  		ptr = ie;
> -		if (ie - p == 3) {
> +		if (ie - p == 3 && !EMULATION(EMULATE_SH)) {
>  		    if ((p[0] == 'N' || p[0] == 'n') &&
>  			(p[1] == 'A' || p[1] == 'a') &&
>  			(p[2] == 'N' || p[2] == 'n')) {
> 


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

* Re: [BUG] POSIX arith: inf, nan should be variables
  2021-12-01  3:31     ` Daniel Shahaf
@ 2021-12-01  3:37       ` Bart Schaefer
  2021-12-01  4:27         ` Writing XFail tests (was: Re: [BUG] POSIX arith: inf, nan should be variables) Daniel Shahaf
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2021-12-01  3:37 UTC (permalink / raw)
  To: Daniel Shahaf; +Cc: Zsh hackers list

On Tue, Nov 30, 2021 at 7:36 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>
> Maybe add a test based on the above code snippet?

There's already an xfail test:

Test ./E03posix.ztst was expected to fail, but passed.
Was testing: All identifiers are variable references in POSIX arithmetic

Just need to flip the state on that one.


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

* Writing XFail tests (was: Re: [BUG] POSIX arith: inf, nan should be variables)
  2021-12-01  3:37       ` Bart Schaefer
@ 2021-12-01  4:27         ` Daniel Shahaf
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Shahaf @ 2021-12-01  4:27 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer wrote on Wed, 01 Dec 2021 03:37 +00:00:
> On Tue, Nov 30, 2021 at 7:36 PM Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
>>
>> Maybe add a test based on the above code snippet?
>
> There's already an xfail test:
>
> Test ./E03posix.ztst was expected to fail, but passed.
> Was testing: All identifiers are variable references in POSIX arithmetic
>
> Just need to flip the state on that one.

I've taken the liberty of doing so.

Incidentally, when writing xfail tests I like to write them with minimal
expectations, so that the test will trigger as soon as zsh's behaviour
changes:
.
     what-is-2-plus-2
    -fD:Unit test for a future builtin
    *>[0-9]

Here, the test looks for something on stdout and is xfail, so it expects
no specific exit code and no specific stderr.  This way, the test will
flip as soon as stdout becomes correct, regardless of whether the exit
code and stderr are at that time also what the they are expected to be
in the end (usually 0 and empty, respectively).

The test also uses a pattern on stdout, so that the test will pass even if
there are, say, whitespace differences between the predicted output (at
the time the xfail test is written) and the actual output (later, when
the bug is fixed).  See workers/48916 for an example of this.

Once the bug is fixed, the test's expectations (all three of them:
stdout, stderr, exit code) can be changed to their permanent form:
.
     what-is-2-plus-2
    0:Unit test for what-is-2-plus-2
    >4

That is: I'd suggest to use «f» flag in conjunction with two of the
three flags [-dD].

Cheers,

Daniel


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

end of thread, other threads:[~2021-12-01  4:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-15 17:40 [BUG] POSIX arith: inf, nan should be variables Martijn Dekker
2021-11-16  9:06 ` Oliver Kiddle
2021-11-16 12:55 ` Vincent Lefevre
2021-11-28 20:34   ` Oliver Kiddle
2021-12-01  3:31     ` Daniel Shahaf
2021-12-01  3:37       ` Bart Schaefer
2021-12-01  4:27         ` Writing XFail tests (was: Re: [BUG] POSIX arith: inf, nan should be variables) Daniel Shahaf

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