google / jsonnet

Jsonnet - The data templating language
http://jsonnet.org
Apache License 2.0
6.98k stars 440 forks source link

Behavior of `std` overriding and desugaring. #1128

Closed eduardosm closed 4 months ago

eduardosm commented 9 months ago

Go implementation differs from C++ implementation when overriding the std binding and invoking an operator that desugars to a std library call.

For example with

local std = {}; 1 == 1

which would desugar to

local std = /* std.jsonnet */; local std = {}; std.equals(1, 1)

The C++ implementation fails:

RUNTIME ERROR: field does not exist: equals

while the Go implementation prints true.

To me, the Go behavior makes for sense than the C++ one, since breaking syntax constructs by overriding a binding seems weird. However, the C++ behavior is closer to what the "Desugaring" section of the specification mandates.

The Go implementation behaves as if it desugared to something like

local std = /* std.jsonnet */; local std = {}; (/* std.jsonnet */).equals(1, 1)

I think it would be nice to have consistent behavior across implementations, and have such behavior written in the specification.

sparkprime commented 8 months ago

The optimisations that replace std functions with their native equivalents need to be aware that std has been redefined, which shouldn't be too hard to implement.