Skip to content

Commit

Permalink
pkg/eval: Restore the compilation order of var forms.
Browse files Browse the repository at this point in the history
This fixes #1829.
  • Loading branch information
xiaq committed Aug 7, 2024
1 parent e022a72 commit 3889bd0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
12 changes: 7 additions & 5 deletions pkg/eval/builtin_special.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,15 @@ func compileLHSRHS(cp *compiler, args []*parse.Compound, end int, lf lvalueFlag)
func compileLHSOptionalRHS(cp *compiler, args []*parse.Compound, end int, lf lvalueFlag) (lvaluesGroup, valuesOp) {
for i, cn := range args {
if parse.SourceText(cn) == "=" {
lhs := cp.compileCompoundLValues(args[:i], lf)
var rhs valuesOp
if i == len(args)-1 {
return lhs, nopValuesOp{diag.PointRanging(end)}
rhs = nopValuesOp{diag.PointRanging(end)}
} else {
rhs = seqValuesOp{
diag.MixedRanging(args[i+1], args[len(args)-1]),
cp.compoundOps(args[i+1:])}
}
return lhs, seqValuesOp{
diag.MixedRanging(args[i+1], args[len(args)-1]),
cp.compoundOps(args[i+1:])}
return cp.compileCompoundLValues(args[:i], lf), rhs
}
}
return cp.compileCompoundLValues(args, lf), nil
Expand Down
6 changes: 6 additions & 0 deletions pkg/eval/builtin_special_test.elvts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ Compilation error: invalid value for unknown-command: bad
▶ new
▶ old

## RHS sees old variable when shadowing (https://b.elv.sh/1829) ##
~> var x = foo
~> var x = [$x]
~> put $x
▶ [foo]

## concurrent creation and access ##
// Ensure that there is no race with "go test -race"
~> var x = 1
Expand Down
12 changes: 11 additions & 1 deletion website/ref/language.md
Original file line number Diff line number Diff line change
Expand Up @@ -1699,7 +1699,7 @@ function as a rest variable.

When declaring a variable that already exists, the existing variable is
shadowed. The shadowed variable may still be accessed indirectly if it is
referenced by a function. Example:
previously referenced by a function. Example:

```elvish-transcript
~> var x = old
Expand All @@ -1711,6 +1711,16 @@ referenced by a function. Example:
▶ old
```

If the right-hand-side of the `var` command references the variable being
shadowed, it sees the old variable:

```elvish-transcript
~> var x = foo
~> var x = [$x] # $x in RHS refers to old $x
~> put $x
▶ [foo]
```

## Assigning variables or elements: `set` {#set}

The `set` special command sets the value of variables or elements.
Expand Down

0 comments on commit 3889bd0

Please sign in to comment.