compile schema FEATURE store cur mod for leafrefs

Because they may differ from the node itself.
Test with deviation included.
diff --git a/src/plugins_types.c b/src/plugins_types.c
index 0564eeb..552ca62 100644
--- a/src/plugins_types.c
+++ b/src/plugins_types.c
@@ -1904,7 +1904,7 @@
     uint32_t i;
 
     /* find all target data instances */
-    ret = lyxp_eval(lref->path, node->schema->module, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, node, tree, &set, 0);
+    ret = lyxp_eval(lref->path, lref->cur_mod, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, node, tree, &set, 0);
     if (ret) {
         ret = LY_ENOTFOUND;
         val_str = lref->plugin->print(value, LY_PREF_JSON, NULL, &dynamic);
diff --git a/src/schema_compile.c b/src/schema_compile.c
index 70e423e..17f3158 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -1091,7 +1091,7 @@
     assert(node->nodetype & (LYS_LEAF | LYS_LEAFLIST));
 
     /* try to find the target */
-    LY_CHECK_RET(ly_path_compile(ctx->ctx, node->module, node, lref->path, LY_PATH_LREF_TRUE, lysc_is_output(node) ?
+    LY_CHECK_RET(ly_path_compile(ctx->ctx, lref->cur_mod, node, lref->path, LY_PATH_LREF_TRUE, lysc_is_output(node) ?
             LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes,
             unres, &p));
 
diff --git a/src/schema_compile_node.c b/src/schema_compile_node.c
index 9593d09..8571aca 100644
--- a/src/schema_compile_node.c
+++ b/src/schema_compile_node.c
@@ -1536,6 +1536,7 @@
             LOGVAL(ctx->ctx, LY_VLOG_STR, ctx->path, LY_VCODE_MISSCHILDSTMT, "path", "leafref type", "");
             return LY_EVALID;
         }
+        lref->cur_mod = type_p->pmod->mod;
         break;
     case LY_TYPE_INST:
         /* RFC 7950 9.9.3 - require-instance */
@@ -1585,6 +1586,7 @@
                             lref->basetype = LY_TYPE_LEAFREF;
                             LY_CHECK_RET(lyxp_expr_dup(ctx->ctx, ((struct lysc_type_leafref *)un_aux->types[v])->path, &lref->path));
                             lref->refcount = 1;
+                            lref->cur_mod = ((struct lysc_type_leafref *)un_aux->types[v])->cur_mod;
                             lref->require_instance = ((struct lysc_type_leafref *)un_aux->types[v])->require_instance;
                             LY_CHECK_RET(lysc_prefixes_dup(((struct lysc_type_leafref *)un_aux->types[v])->prefixes,
                                     &lref->prefixes));
diff --git a/src/tree_schema.h b/src/tree_schema.h
index e977f96..f7b854b 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -1448,6 +1448,7 @@
     uint32_t refcount;               /**< reference counter for type sharing */
     struct lyxp_expr *path;          /**< parsed target path, compiled path cannot be stored because of type sharing */
     struct lysc_prefix *prefixes;    /**< resolved prefixes used in the path */
+    const struct lys_module *cur_mod;/**< current module for the leafref (path) */
     struct lysc_type *realtype;      /**< pointer to the real (first non-leafref in possible leafrefs chain) type. */
     uint8_t require_instance;        /**< require-instance flag */
 };
diff --git a/src/xpath.c b/src/xpath.c
index 14738c7..811a8a0 100644
--- a/src/xpath.c
+++ b/src/xpath.c
@@ -3700,7 +3700,7 @@
             oper = lysc_is_output((struct lysc_node *)sleaf) ? LY_PATH_OPER_OUTPUT : LY_PATH_OPER_INPUT;
 
             /* it was already evaluated on schema, it must succeed */
-            rc = ly_path_compile(set->ctx, sleaf->module, (struct lysc_node *)sleaf, lref->path,
+            rc = ly_path_compile(set->ctx, lref->cur_mod, (struct lysc_node *)sleaf, lref->path,
                     LY_PATH_LREF_TRUE, oper, LY_PATH_TARGET_MANY, LY_PREF_SCHEMA_RESOLVED, lref->prefixes, NULL, &p);
             assert(!rc);