Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions exist-core/src/main/java/org/exist/xquery/ForExpr.java
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,12 @@ public Sequence eval(Sequence contextSequence, Item contextItem)
if (sequenceType != null) {
//Type.EMPTY is *not* a subtype of other types ; checking cardinality first
//only a check on empty sequence is accurate here
if (resultSequence.isEmpty() &&
!sequenceType.getCardinality().isSuperCardinalityOrEqualOf(Cardinality.EMPTY_SEQUENCE))
{throw new XPathException(this, ErrorCodes.XPTY0004,
"Invalid cardinality for variable $" + varName + ". Expected " +
sequenceType.getCardinality().getHumanDescription() +
", got " + Cardinality.EMPTY_SEQUENCE.getHumanDescription());}
// if (resultSequence.isEmpty() &&
// !sequenceType.getCardinality().isSuperCardinalityOrEqualOf(Cardinality.EMPTY_SEQUENCE))
// {throw new XPathException(this, ErrorCodes.XPTY0004,
// "Invalid cardinality for variable $" + varName + ". Expected " +
// sequenceType.getCardinality().getHumanDescription() +
// ", got " + Cardinality.EMPTY_SEQUENCE.getHumanDescription());}
//TODO : ignore nodes right now ; they are returned as xs:untypedAtomicType
if (!Type.subTypeOf(sequenceType.getPrimaryType(), Type.NODE)) {
if (!resultSequence.isEmpty() &&
Expand Down
12 changes: 12 additions & 0 deletions exist-core/src/test/java/org/exist/xquery/XQueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,18 @@ public void wrongInvalidTypeError_1787285() throws XMLDBException {
}
}

@Test
public void typedForVariableWithEmptyReturn_5050() throws XMLDBException {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't name this _5050. Instead link to the GitHub issue in the Javadoc for the function.

final String query = "for $x as xs:string in 'X' return ()";

@adamretter adamretter Nov 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to see more tests added. Just two JUnit Test functions please, one for for and one for let.
I would also like to see the XQuery code within each function formatted a bit more nicely please:

  1. let $x := 'X'
    return
      $x
  2. let $x as xs:string := 'X'
    return
      $x
  3. let $result :=
        let $x as xs:string := 'X'
        return
          $x
    return
      empty($result)
  4. let $x := 'X'
    return
      ()
  5. let $x as xs:string := 'X'
    return
      ()
  6. let $result :=
        let $x as xs:string := 'X'
        return
          ()
    return
      empty($result)
  7. let $x := 'X'
    return
      1
  8. let $x as xs:string := 'X'
    return
      1
  9. let $result :=
        let $x as xs:string := 'X'
        return
          1
    return
      empty($result)
  10. let $x := 'X'
    return
      (1, 2, 3)
  11. let $x as xs:string := 'X'
    return
      (1, 2, 3)
  12. let $result :=
        let $x as xs:string := 'X'
        return
          (1, 2, 3)
    return
      empty($result)
  13. for $x in 'X'
    return
      $x
  14. for $x as xs:string in 'X'
    return
      $x
  15. let $result :=
        for $x as xs:string in 'X'
        return
          $x
    return
      empty($result)
  16. for $x in 'X'
    return
      ()
  17. for $x as xs:string in 'X'
    return
      ()
  18. let $result :=
        for $x as xs:string in 'X'
        return
          ()
    return
      empty($result)
  19. for $x in 'X'
    return
      1
  20. for $x as xs:string in 'X'
    return
      1
  21. let $result :=
        for $x as xs:string in 'X'
        return
          1
    return
      empty($result)
  22. for $x in 'X'
    return
      (1, 2, 3)
  23. for $x as xs:string in 'X'
    return
      (1, 2, 3)
  24. let $result :=
        for $x as xs:string in 'X'
        return
          (1, 2, 3)
    return
      empty($result)

Please also rename the test functions you create to make it more reflective of the GitHub issue, and what it is actually testing.

@adamretter adamretter Nov 19, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more for tests to add please:

(: for - returning $x :)

for $x as empty-sequence() allowing empty in ()
return
  $x
,

for $x as xs:integer in ()
return
  $x
,

for $x as xs:integer? in ()
return
  $x
,

for $x as xs:integer+ in ()
return
  $x
,

for $x as xs:integer* in ()
return
  $x
,

for $x as empty-sequence() allowing empty in (1, 2, 3)
return
  $x
,

for $x as xs:integer in (1,2,3)
return
  $x
,

for $x as xs:integer? in (1,2,3)
return
  $x
,

for $x as xs:integer+ in (1,2,3)
return
  $x
,

for $x as xs:integer* in (1,2,3)
return
  $x
,

(: for - returning empty-sequence() :)
for $x as empty-sequence() allowing empty in ()
return
  ()
,

for $x as xs:integer in ()
return
  ()
,

for $x as xs:integer? in ()
return
  ()
,

for $x as xs:integer+ in ()
return
  ()
,

for $x as xs:integer* in ()
return
  ()
,

for $x as empty-sequence() allowing empty in (1,2,3)
return
  ()
,

for $x as xs:integer in (1,2,3)
return
  ()
,

for $x as xs:integer? in (1,2,3)
return
  ()
,

for $x as xs:integer+ in (1,2,3)
return
  ()
,

for $x as xs:integer* in (1,2,3)
return
  ()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more let tests to add please:

(: let - returning $x :)
let $x as empty-sequence() := ()
return
  $x
,

let $x as xs:integer := ()
return
  $x
,

let $x as xs:integer? := ()
return
  $x
,

let $x as xs:integer+ := ()
return
  $x
,

let $x as xs:integer* := ()
return
  $x
,

let $x as xs:integer := (1,2,3)
return
  $x
,

let $x as xs:integer? := (1,2,3)
return
  $x
,

let $x as xs:integer+ := (1,2,3)
return
  $x
,

let $x as xs:integer* := (1,2,3)
return
  $x
,

(: let - returning empty-sequence() :)

let $x as empty-sequence() := ()
return
  ()
,

let $x as xs:integer := ()
return
  ()
,

let $x as xs:integer? := ()
return
  ()
,

let $x as xs:integer+ := ()
return
  ()
,

let $x as xs:integer* := ()
return
  ()
,


let $x as xs:integer := (1,2,3)
return
  ()
,

let $x as xs:integer? := (1,2,3)
return
  ()
,

let $x as xs:integer+ := (1,2,3)
return
  ()
,

let $x as xs:integer* := (1,2,3)
return
  ()

@Laurettta Laurettta Nov 19, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adamretter while adding your queries as tests I found that for $x as xs:string in 'X' return 1 (and the same with return (1, 2, 3)) fails with XPTY0004 Invalid type for variable $x. Expected xs:string, got xs:integer. I think they should work, since $x is a string. Should I fix this in this PR or open a separate issue?

try (final Collection testCollection = getTestCollection()) {
final XPathQueryService service = testCollection.getService(XPathQueryService.class);
try (final EXistResourceSet result = (EXistResourceSet) service.query(query)) {
assertEquals(0, result.getSize());
}
}
}


/**
* Regression
*
Expand Down
Loading