eXist-db / exist

eXist Native XML Database and Application Platform
https://exist-db.org
GNU Lesser General Public License v2.1
428 stars 179 forks source link

[BUG] functions declared in main module are not added to the dynamic context #4614

Open line-o opened 1 year ago

line-o commented 1 year ago

NOTE: This is a follow-up on a question I posted on StackOverflow

consider a library module ctx

xquery version "3.1";

module namespace ctx="module/ctx"; 

declare function ctx:resolve (
    $ctx as function(xs:string) as xs:QName
) as function(xs:string, xs:integer) as function(*)? {
    function ($name as xs:string, $arity as xs:integer) as function(*)? {
        function-lookup($ctx($name), $arity)
    }
};

and a library module a

xquery version "3.1";

module namespace a="module/a"; 

declare function a:f () { "a:f" };

And a main module

xquery version "3.1";

import module namespace a="module/a" at "a.xqm";
import module namespace ctx="module/ctx" at "ctx.xqm";

declare namespace app = "app";
declare function app:f () { "app:f" };

ctx:resolve(xs:QName(?))("a:f", 0)() (: this does work :)
, ctx:resolve(xs:QName(?))("app:f", 0)() (: this does not work :)
adamretter commented 1 year ago

I would not expect either to work, and I suspect the fact that one of them works is more of a bug than by design. The XQuery spec states:

the context for evaluating a function body or for a variable's initializing expression is defined based on the context of the module in which the function or variable is defined.

It also states:

Module imports are not transitive—that is, importing a module provides access only to function and variable declarations contained directly in the imported module.

function-lookup should be called on the static context of Module ctx, and it should not find a function named a:f or app:f as they are not imported into the ctx module.

line-o commented 1 year ago

@adamretter please see the discussion and answers here https://stackoverflow.com/questions/74408887/scope-and-statically-known-namespaces-in-xquery

Also note that the above works in baseX 10 and Saxon 11

line-o commented 1 year ago

This sparked also a discussion about rewording the XQuery specification to clarify its meaning: https://github.com/qt4cg/qtspecs/issues/239

line-o commented 1 year ago

I do agree though that it would make sense for the function-lookup to not work. This was my expectation, but I works and across implementations.