Elvish: Temporary assignment to nonexistent variable leaves it equal to ''

Created on 15 Dec 2017  Â·  5Comments  Â·  Source: elves/elvish

~/go/src/github.com/elves/elvish> put $y
Compilation error: variable $y not found
  [interactive], line 1:
    put $y
~/go/src/github.com/elves/elvish> y=1 echo hi
hi
~/go/src/github.com/elves/elvish> put $y
â–¶ ''
~/go/src/github.com/elves/elvish>       

Is this a known issue? I think y should be undefined as before.

bug

Most helpful comment

Yup, it's a known bug. Whoever implemented it must have been too lazy to write correct code :(

All 5 comments

I can confirm this bug. If the variable already had a value (like in the example in the docs), then the old value is preserved correctly after the temporary assignment, but if the variable was undefined, then it gets set to an empty string after the temporary assignment.

Yup, it's a known bug. Whoever implemented it must have been too lazy to write correct code :(

@xiaq
I think the culprit is this:

https://github.com/elves/elvish/blob/4df00b06eb66c2f776f87f027d482a0b4dc0cf9c/eval/compile_op.go#L246-L251

Is there any reason behind this? Maybe we should just skip nil value.

It seems to me that the core challenge in fixing this is modifying the compiler to recognize that a sequence such as this:

x=y put $x
put $x

Should result in this compilation error:

compilation error: variable $x not found
/Users/krader/x.elv, line 2: put $x
Exception: elvish exited with 2

Note that I created that error by commenting out the first line. From the compiler's perspective the temporary assignment should be equivalent to commenting out the first line. The fix for this issue is trivial if you don't care that the compiler does not recognize $x does not exist after the temporary assignment.

The borderline trivial, incomplete, fix would mean that dereferencing an undefined var is caught at run-time rather than compile-time. Is that good enough? My knowledge of the Elvish compiler is almost zero so it's possible the fully correct fix is also borderline trivial.

FYI, Anyone tempted to work on this should read the feedback in P.R. #802.

@xiaq, If you've started working on a fix for this issue it would be a good idea to make note of that fact so no one wastes time taking a stab at the problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

krader1961 picture krader1961  Â·  4Comments

zzamboni picture zzamboni  Â·  6Comments

krader1961 picture krader1961  Â·  8Comments

notramo picture notramo  Â·  6Comments

zzamboni picture zzamboni  Â·  3Comments