Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporary assignment to nonexistent variable leaves it equal to '' #532

Closed
tw4452852 opened this issue Dec 15, 2017 · 6 comments
Closed
Labels
Milestone

Comments

@tw4452852
Copy link
Contributor

~/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.

@zzamboni
Copy link
Contributor

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.

@xiaq
Copy link
Member

xiaq commented Dec 15, 2017

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

@xiaq xiaq added the bug label Dec 15, 2017
@xiaq xiaq changed the title side effect of temporary assignment Temporary assignment to nonexistent variable leaves it equal to '' Dec 16, 2017
@tw4452852
Copy link
Contributor Author

@xiaq
I think the culprit is this:

elvish/eval/compile_op.go

Lines 246 to 251 in 4df00b0

if val == nil {
// XXX Old value is nonexistent. We should delete the
// variable. However, since the compiler now doesn't delete
// it, we don't delete it in the evaler either.
val = String("")
}

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

@xiaq xiaq added this to the 0.11 milestone Jan 4, 2018
@xiaq xiaq modified the milestones: 0.11, 0.13 Jan 14, 2018
xofyarg added a commit to xofyarg/elvish that referenced this issue Mar 16, 2019
This change makes assignmentOp mutable only on temporary assignment,
otherwise the outer Op needs to know how to deal with it.
Theoretically, lvaluesOp should tell which variable is new, but it
requires more changes to the underlying structures.

This fixes elves#532.
@xiaq xiaq removed this from the 0.14 milestone Apr 6, 2019
@xiaq xiaq added this to the 0.14 milestone Apr 8, 2020
@xiaq xiaq modified the milestones: 0.14, 0.15 Jun 30, 2020
@krader1961
Copy link
Contributor

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.

@krader1961
Copy link
Contributor

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.

@xiaq xiaq removed the A:Language label Oct 28, 2020
@xiaq xiaq modified the milestones: 0.15.0, 0.16.0 Jan 4, 2021
@xiaq xiaq removed this from the 0.16.0 milestone Jun 27, 2021
xiaq added a commit that referenced this issue Nov 29, 2021
Initialization of variable slots were "hoisted" to the beginning of each scope.
This commit moves that initialization to the command that declares the variable,
which make it easier to resolve #1257 and #1235.

Also properly handle variables that are created as part of a temporary
assignment, addressing #532.

There are some failing tests, which will be fixed in upcoming commits.
@xiaq xiaq added this to the 0.18.0 milestone Dec 6, 2021
@xiaq xiaq moved this to Todo in All Elvish issues Feb 25, 2022
@xiaq
Copy link
Member

xiaq commented Feb 27, 2022

This will no longer be irrelevant when the temp assignment form gets deprecated and eventually removed (#1114). The tmp command requires the variable to exist.

@xiaq xiaq closed this as completed Feb 27, 2022
Repository owner moved this from ❓Triage to 👌Done in All Elvish issues Feb 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants