fix: keep std.trace lazy during static optimization#843
Merged
Conversation
Motivation: std.trace has an observable side effect, but the static optimizer treated it like a safe builtin and could execute it while folding expressions. That allowed traces from unused object fields or unused format expressions and could reorder trace output compared with official Jsonnet runtime behavior. Modification: Mark the std.trace builtin as not static-safe so it is evaluated only when the Jsonnet program forces it at runtime. Add regression tests that unused traces inside objects and format RHS values stay silent while a directly used trace still logs, and update the upstream trace golden to the runtime trace order. Result: Full ./mill --no-server --ticker false --color false -j 1 __.test passed with 438 tests. Three independent reviews found no blockers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
std.tracehas an observable side effect: it writes a trace message and returns therestvalue. The static optimizer previously considered it safe to fold, so it could execute traces while optimizing expressions that the Jsonnet runtime never forces.That caused two correctness problems:
Key Design Decision
Mark
std.traceas not static-safe. This keeps the existing optimizer behavior for pure builtins while preventing the optimizer from executing this side-effecting builtin.Modification
staticSafe = falseon thestd.tracebuiltin.std.tracestill logs and returns itsrestvalue.trace.jsonnet.goldentrace order to match runtime evaluation order.Benchmark Results
This is a correctness/compatibility PR, not a performance PR. No benchmark change is expected.
Analysis
StaticOptimizer.tryStaticApplycan execute static-safe builtins during optimization. That is correct for pure functions, butstd.traceis intentionally observable, so executing it before runtime changes program behavior. Disabling static folding for this builtin preserves Jsonnet laziness and official trace ordering without changing the runtime implementation.References
std.trace(str, rest)outputsstrand returnsrest.e176c85b fix: keep std.trace lazy during static optimization.Result
./mill --no-server --ticker false --color false -j 1 __.testpassed with 438 tests. Formatting was applied with__.reformat,git diff --checkpassed, and three independent reviews reported no blockers.