Skip to content

Fix type checking of 'as' clause in for-binding#6091

Open
joewiz wants to merge 1 commit intoeXist-db:developfrom
joewiz:fix/for-binding-type-check-3553
Open

Fix type checking of 'as' clause in for-binding#6091
joewiz wants to merge 1 commit intoeXist-db:developfrom
joewiz:fix/for-binding-type-check-3553

Conversation

@joewiz
Copy link
Member

@joewiz joewiz commented Mar 3, 2026

Summary

  • Fix incorrect type validation of as clause in for bindings that checked the return expression result instead of the iteration variable value
  • Expressions like for $i as xs:string in ('a', 'b', 'c') return <p>{$i}</p> no longer incorrectly raise XPTY0004
  • Also fixes the related XQTS test case hof-046 (function type in for-binding)

Details

The as type declaration in a for binding 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 in ForExpr.java:

  1. Per-item type checking was skipped when a sequenceType was declared — var.checkType() was only called when sequenceType == null
  2. Post-loop type checking validated the wrong thing — after the loop, the code checked resultSequence (output of the return clause) against the for-binding's type declaration, instead of the binding sequence

The 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

  • Added 5 xqsuite tests to 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 error
    • for-as-decimal-with-integers: subtype promotion works
    • for-as-string-binding: string binding passes type check
  • All 945 XQuery3 tests pass (0 failures)
  • XQTS prod-ForClause: 188 tests, 167 pass, 21 failures — identical to develop baseline (no regressions)

🤖 Generated with Claude Code

@joewiz joewiz requested a review from a team as a code owner March 3, 2026 17:09
@joewiz
Copy link
Member Author

joewiz commented Mar 3, 2026

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>
@joewiz joewiz force-pushed the fix/for-binding-type-check-3553 branch from 544014e to a0a2c0c Compare March 5, 2026 01:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error in type-checking with "as" in for-statement [BUG]

1 participant