fix: reject duplicate function parameters with correct error in all contexts#1011
Open
He-Pin wants to merge 1 commit into
Open
fix: reject duplicate function parameters with correct error in all contexts#1011He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
32e3062 to
83d39d9
Compare
83d39d9 to
b7288e6
Compare
…ontexts Motivation: go-jsonnet issue databricks#873 reports that (function(x, x, x) x)(null, 3, 2) silently returns 2 instead of rejecting duplicate parameter names. sjsonnet already rejected duplicates in function expressions, but the error message was misleading in local bindings and object methods. Modification: The params parser correctly detects duplicates via flatMapX + Fail. However, in bind (local f(x,x)=...) and field ({f(x,x):...}), the optional group .? around params swallowed the "no duplicate parameter" error and backtracked, producing misleading errors like "Expected )". Replaced .? with ./ (committed-or): "(" ~/ params ~ ")" ./ | Pass(null). The cut ~/ after "(" commits to the params path, and ./ prevents the | alternative from trying the fallback when the committed path fails. Result: All duplicate parameter contexts now produce the correct error: "Expected no duplicate parameter: x" with accurate position info. - (function(x, x, x) x) -> ParseError (already worked) - local f(x, x) = x -> ParseError (was "Expected )") - { f(x, x): x } -> ParseError (was "Expected :") - { local f(x, x) = x } -> ParseError (was "Expected )") Tests pass on Scala 3.3.7, 2.13.18, 2.12.21. References: - google/go-jsonnet#873
b7288e6 to
c7b60b3
Compare
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
go-jsonnet issue #873 reports that
(function(x, x, x) x)(null, 3, 2)silently returns
2instead of rejecting duplicate parameter names.sjsonnet already rejected duplicates in function expressions, but the
error message was misleading in local bindings and object methods
(e.g. "Expected ")"" or "Expected ":"").
Modification
The
paramsparser correctly detects duplicates viaflatMapX+Fail. However, inbind(local f(x,x)=...) andfield(
{f(x,x):...}), the optional group.?aroundparamsswallowedthe "no duplicate parameter" error and backtracked, producing
misleading errors.
Two changes fix this:
field(): added~/(cut) after"("to commit to the paramspath once
"("is matched:"(" ~/ params(...) ~ ")".params(): setctx.cut = trueprogrammatically when aduplicate is detected, preventing outer
.?operators fromswallowing the error. Also captures
Posfor each parameter toposition the error at the duplicate parameter name (not at the
closing paren).
Result
All duplicate parameter contexts now produce the correct error
"Expected no duplicate parameter: x"with accurate position info:(function(x, x, x) x)-> ParseError (already worked)local f(x, x) = x-> ParseError (was"Expected )"){ f(x, x): x }-> ParseError (was"Expected :"){ local f(x, x) = x }-> ParseError (was"Expected )")Tests pass on Scala 3.3.7, 2.13.18, 2.12.21.
References