You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR fixes multiple bugs in object comprehensions (fixes#357, fixes#331, plus another bug).
## Mis-handling of `self` and `super` in object comprehension locals
when inherited
Object comprehensions may define local variables and those variables may
reference `self` or `super`.
Consider the reproduction reported in
#357:
```jsonnet
local lib = {
foo()::
{
local global = self,
[iterParam]: global.base {
foo: iterParam
}
for iterParam in ["foo"]
},
};
{
base:: {}
}
+ lib.foo()
```
This is supposed to output
```json
{
"foo": {
"foo": "foo"
}
}
```
but sjsonnet outputs
```
sjsonnet.Error: Field does not exist: base
at [Select base].(:7:26)
```
This bug occurs because of how bindings were managed: there's some
tricky circular reference and variable rebinding logic (which I believe
was added to handle references _between_ locals) and that was rebinding
the `self` reference to the comprehension result itself: that is wrong
because the `self` reference may change if an object is inherited or
extended.
While digging into this, I also discovered a related bug where `super`
wasn't being properly re-bound in object comprehension locals or values:
```jsonnet
local lib = {
foo():: {
local sx = super.x,
[k]: sx + 1
for k in ["x"]
},
};
{ x: 2 }
+ lib.foo()
```
was failing with
```
java.lang.Exception: sjsonnet.Error: Attempt to use `super` when there is no super class
at [SelectSuper x].(:4:21)
at [ValidId sx].(:5:10)
at [BinaryOp +].(:5:13)
```
## Silent dropping of fields if key is not a string
The object comprehension
```jsonnet
{[k]: k for k in [1]}
```
fails in regular jsonnet with an error
```
RUNTIME ERROR: field must be string, got: number
<cmdline>:1:1-22
```
but in sjsonnet it was silently returning `{}`. This regression was
[introduced](https://github.com/databricks/sjsonnet/pull/226/files#diff-7e786f55f42d493f85bfb37e5181c1c942ea039b21a307680e4e6913a8084aa2L628-R631)
in #226 by adding a default `case _ =>` (presumably to fix a compiler or
IDE warning).
## Support function definition via object comprehensions
This fixes#331 by adding
parser support for defining functions using method syntax, e.g.
```jsonnet
{
[a](x): x * 2
for a in ["f1", "f2", "f3"]
}
```
## Fixes
This PR fixes the above bugs by changing logic within
`Evaluator.visitObjComp`. It is best reviewed commit-by-commit or using
a whitespace-free diff.
0 commit comments