Commit b2690e6
authored
Fix multiple object comprehension bugs (#358)
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.1 parent 84ac320 commit b2690e6
File tree
5 files changed
+154
-61
lines changed- sjsonnet
- src/sjsonnet
- test
- src-jvm/sjsonnet
- src/sjsonnet
5 files changed
+154
-61
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
680 | 680 | | |
681 | 681 | | |
682 | 682 | | |
683 | | - | |
684 | | - | |
685 | | - | |
686 | | - | |
687 | | - | |
| 683 | + | |
688 | 684 | | |
689 | 685 | | |
690 | 686 | | |
691 | 687 | | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
692 | 692 | | |
693 | 693 | | |
694 | 694 | | |
| |||
697 | 697 | | |
698 | 698 | | |
699 | 699 | | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
| 700 | + | |
| 701 | + | |
704 | 702 | | |
705 | 703 | | |
706 | 704 | | |
707 | 705 | | |
708 | 706 | | |
709 | | - | |
710 | | - | |
| 707 | + | |
711 | 708 | | |
712 | | - | |
713 | | - | |
| 709 | + | |
714 | 710 | | |
715 | 711 | | |
716 | 712 | | |
| |||
841 | 837 | | |
842 | 838 | | |
843 | 839 | | |
844 | | - | |
845 | | - | |
846 | | - | |
847 | | - | |
848 | | - | |
849 | | - | |
850 | | - | |
851 | | - | |
852 | | - | |
853 | | - | |
854 | | - | |
855 | | - | |
856 | | - | |
857 | | - | |
858 | | - | |
859 | | - | |
860 | | - | |
861 | | - | |
| 840 | + | |
| 841 | + | |
| 842 | + | |
| 843 | + | |
| 844 | + | |
| 845 | + | |
| 846 | + | |
| 847 | + | |
| 848 | + | |
| 849 | + | |
| 850 | + | |
| 851 | + | |
| 852 | + | |
| 853 | + | |
| 854 | + | |
862 | 855 | | |
863 | | - | |
864 | | - | |
865 | | - | |
866 | 856 | | |
867 | | - | |
868 | | - | |
869 | | - | |
870 | | - | |
871 | | - | |
872 | | - | |
873 | | - | |
874 | | - | |
| 857 | + | |
| 858 | + | |
| 859 | + | |
| 860 | + | |
| 861 | + | |
| 862 | + | |
875 | 863 | | |
876 | | - | |
877 | 864 | | |
878 | | - | |
879 | | - | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
880 | 871 | | |
881 | 872 | | |
882 | 873 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
430 | 430 | | |
431 | 431 | | |
432 | 432 | | |
433 | | - | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
434 | 441 | | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
435 | 447 | | |
436 | 448 | | |
437 | 449 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
| 21 | + | |
| 22 | + | |
27 | 23 | | |
28 | 24 | | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
| 25 | + | |
38 | 26 | | |
39 | 27 | | |
40 | 28 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
25 | 36 | | |
26 | 37 | | |
27 | 38 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
145 | 145 | | |
146 | 146 | | |
147 | 147 | | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
148 | 239 | | |
149 | 240 | | |
150 | 241 | | |
| |||
0 commit comments