Fix type checking of 'as' clause in for-binding#6091
Open
joewiz wants to merge 1 commit intoeXist-db:developfrom
Open
Fix type checking of 'as' clause in for-binding#6091joewiz wants to merge 1 commit intoeXist-db:developfrom
joewiz wants to merge 1 commit intoeXist-db:developfrom
Conversation
Member
Author
|
cc/ @djbpitt |
The "as" type declaration in a for-binding was incorrectly validated
against the return expression's result instead of the iteration
variable's value. This caused expressions like:
for $i as xs:string in ('a', 'b', 'c') return <p>{$i}</p>
to raise XPTY0004, because the element nodes returned by the return
clause were checked against xs:string.
The fix:
1. Always call var.checkType() per iteration item in processItem(),
regardless of whether a sequenceType is declared. Previously,
checkType() was skipped when sequenceType was set.
2. Remove the incorrect post-loop type check that validated
resultSequence (the return expression's output) against the
for-binding's type declaration.
Closes eXist-db#3553
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
544014e to
a0a2c0c
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.
Summary
asclause inforbindings that checked the return expression result instead of the iteration variable valuefor $i as xs:string in ('a', 'b', 'c') return <p>{$i}</p>no longer incorrectly raise XPTY0004hof-046(function type in for-binding)Details
The
astype declaration in aforbinding specifies the expected type of each item bound to the iteration variable (XQuery 3.1 spec §3.12.2). The previous implementation had two bugs inForExpr.java:sequenceTypewas declared —var.checkType()was only called whensequenceType == nullresultSequence(output of thereturnclause) against the for-binding's type declaration, instead of the binding sequenceThe fix ensures
var.checkType()is always called per iteration item (validating the bound value against the declared type) and removes the incorrect post-loop check on the return expression's result.Closes #3553
Test plan
flwor.xql:for-as-string-return-element: string type declaration with element return (the reported bug)for-as-function: function type declaration (XQTS hof-046 equivalent)for-as-type-mismatch: type mismatch still raises errorfor-as-decimal-with-integers: subtype promotion worksfor-as-string-binding: string binding passes type checkprod-ForClause: 188 tests, 167 pass, 21 failures — identical to develop baseline (no regressions)🤖 Generated with Claude Code