tree schema BUGFIX compilation of identities moved

... to the part where the module is parsed. The consequences are shown
in the tests.
diff --git a/src/schema_compile.c b/src/schema_compile.c
index 8ba8ad8..1d6872e 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -249,7 +249,21 @@
     LOG_LOCSET(NULL, NULL, ctx->path, NULL);
 }
 
-LY_ERR
+/**
+ * @brief Compile information from the identity statement
+ *
+ * The backlinks to the identities derived from this one are supposed to be filled later via ::lys_compile_identity_bases().
+ *
+ * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
+ * @param[in] ctx libyang context.
+ * @param[in] parsed_mod Module with the identities.
+ * @param[in] identities_p Array of the parsed identity definitions to precompile.
+ * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
+ * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
+ * to be processed.
+ * @return LY_ERR value.
+ */
+static LY_ERR
 lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
         struct lysp_ident *identities_p, struct lysc_ident **identities)
 {
@@ -1711,19 +1725,20 @@
     return ret;
 }
 
-/**
- * @brief Compile identites in a module and all its submodules.
- *
- * @param[in] mod Module to process.
- * @return LY_ERR value.
- */
-static LY_ERR
+LY_ERR
 lys_compile_identities(struct lys_module *mod)
 {
     struct lysc_ctx ctx = {0};
     struct lysp_submodule *submod;
     LY_ARRAY_COUNT_TYPE u;
 
+    /* pre-compile identities of the module and any submodules */
+    LY_CHECK_RET(lys_identity_precompile(NULL, mod->ctx, mod->parsed, mod->parsed->identities, &mod->identities));
+    LY_ARRAY_FOR(mod->parsed->includes, u) {
+        submod = mod->parsed->includes[u].submodule;
+        LY_CHECK_RET(lys_identity_precompile(NULL, mod->ctx, (struct lysp_module *)submod, submod->identities, &mod->identities));
+    }
+
     /* prepare context */
     ctx.ctx = mod->ctx;
     ctx.cur_mod = mod;
@@ -1821,9 +1836,6 @@
     /* add the module into newly implemented module set */
     LY_CHECK_RET(ly_set_add(&unres->implementing, mod, 1, NULL));
 
-    /* compile identities */
-    LY_CHECK_RET(lys_compile_identities(mod));
-
     /* mark target modules with our augments and deviations */
     LY_CHECK_RET(lys_precompile_augments_deviations(mod, unres));
 
diff --git a/src/schema_compile.h b/src/schema_compile.h
index 65e582c..5d68677 100644
--- a/src/schema_compile.h
+++ b/src/schema_compile.h
@@ -175,23 +175,6 @@
         const struct lys_module *ext_mod);
 
 /**
- * @brief Compile information from the identity statement
- *
- * The backlinks to the identities derived from this one are supposed to be filled later via ::lys_compile_identity_bases().
- *
- * @param[in] ctx_sc Compile context - alternative to the combination of @p ctx and @p parsed_mod.
- * @param[in] ctx libyang context.
- * @param[in] parsed_mod Module with the identities.
- * @param[in] identities_p Array of the parsed identity definitions to precompile.
- * @param[in,out] identities Pointer to the storage of the (pre)compiled identities array where the new identities are
- * supposed to be added. The storage is supposed to be initiated to NULL when the first parsed identities are going
- * to be processed.
- * @return LY_ERR value.
- */
-LY_ERR lys_identity_precompile(struct lysc_ctx *ctx_sc, struct ly_ctx *ctx, struct lysp_module *parsed_mod,
-        struct lysp_ident *identities_p, struct lysc_ident **identities);
-
-/**
  * @brief Find and process the referenced base identities from another identity or identityref
  *
  * For bases in identity set backlinks to them from the base identities. For identityref, store
@@ -210,6 +193,14 @@
         struct lysc_ident *ident, struct lysc_ident ***bases, ly_bool *enabled);
 
 /**
+ * @brief Perform a complet compilation of identites in a module and all its submodules.
+ *
+ * @param[in] mod Module to process.
+ * @return LY_ERR value.
+ */
+LY_ERR lys_compile_identities(struct lys_module *mod);
+
+/**
  * @brief Compile schema into a validated schema linking all the references. Must have been implemented before.
  *
  * @param[in] mod Pointer to the schema structure holding pointers to both schema structure types. The ::lys_module#parsed
diff --git a/src/tree_schema.c b/src/tree_schema.c
index 66c42a4..561d325 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -800,6 +800,7 @@
 _lys_set_implemented(struct lys_module *mod, const char **features, struct lys_glob_unres *unres)
 {
     LY_ERR ret = LY_SUCCESS, r;
+    struct lys_module *mod_iter;
     uint32_t i;
 
     if (mod->implemented) {
@@ -833,6 +834,18 @@
         }
     }
 
+    /* Try to find module with LYS_MOD_IMPORTED_REV flag. */
+    i = 0;
+    while ((mod_iter = ly_ctx_get_module_iter(mod->ctx, &i))) {
+        if (!strcmp(mod_iter->name, mod->name) && (mod_iter->latest_revision & LYS_MOD_IMPORTED_REV)) {
+            LOGVRB("Implemented module \"%s@%s\" was not and will not "
+                    "be imported if the revision-date is missing in the "
+                    "import statement. Instead, the revision \"%s\" "
+                    "is imported.", mod->name, mod->revision, mod_iter->revision);
+            break;
+        }
+    }
+
 cleanup:
     return ret;
 }
@@ -1523,9 +1536,7 @@
         void *check_data, struct ly_set *new_mods, struct lys_module **module)
 {
     struct lys_module *mod = NULL, *latest, *mod_dup = NULL;
-    struct lysp_submodule *submod;
     LY_ERR ret;
-    LY_ARRAY_COUNT_TYPE u;
     struct lys_yang_parser_ctx *yangctx = NULL;
     struct lys_yin_parser_ctx *yinctx = NULL;
     struct lys_parser_ctx *pctx = NULL;
@@ -1671,13 +1682,8 @@
     /* compile features */
     LY_CHECK_GOTO(ret = lys_compile_feature_iffeatures(mod->parsed), cleanup);
 
-    /* pre-compile identities of the module and any submodules */
-    LY_CHECK_GOTO(ret = lys_identity_precompile(NULL, ctx, mod->parsed, mod->parsed->identities, &mod->identities), cleanup);
-    LY_ARRAY_FOR(mod->parsed->includes, u) {
-        submod = mod->parsed->includes[u].submodule;
-        ret = lys_identity_precompile(NULL, ctx, (struct lysp_module *)submod, submod->identities, &mod->identities);
-        LY_CHECK_GOTO(ret, cleanup);
-    }
+    /* compile identities */
+    LY_CHECK_GOTO(ret = lys_compile_identities(mod), cleanup);
 
     /* success */
 
diff --git a/src/tree_schema.h b/src/tree_schema.h
index 2d1f989..792b285 100644
--- a/src/tree_schema.h
+++ b/src/tree_schema.h
@@ -1460,7 +1460,8 @@
     const char *dsc;                 /**< description */
     const char *ref;                 /**< reference */
     struct lys_module *module;       /**< module structure */
-    struct lysc_ident **derived;     /**< list of (pointers to the) derived identities ([sized array](@ref sizedarrays)) */
+    struct lysc_ident **derived;     /**< list of (pointers to the) derived identities ([sized array](@ref sizedarrays))
+                                          It also contains references to identities located in unimplemented modules. */
     struct lysc_ext_instance *exts;  /**< list of the extension instances ([sized array](@ref sizedarrays)) */
     uint16_t flags;                  /**< [schema node flags](@ref snodeflags) - only LYS_STATUS_ values are allowed */
 };
@@ -2309,8 +2310,9 @@
                                           Available only for implemented modules. */
 
     struct lysc_ident *identities;   /**< List of compiled identities of the module ([sized array](@ref sizedarrays))
-                                          Identities are outside the compiled tree to allow their linkage to the identities from
-                                          the implemented modules. This avoids problems when the module became implemented in
+                                          also contains the disabled identities when their if-feature(s) are evaluated to \"false\",
+                                          and also the list is filled even if the module is not implemented.
+                                          The list is located here because it avoids problems when the module became implemented in
                                           future (no matter if implicitly via augment/deviate or explicitly via
                                           ::lys_set_implemented()). Note that if the module is not implemented (compiled), the
                                           identities cannot be instantiated in data (in identityrefs). */
diff --git a/src/tree_schema_internal.h b/src/tree_schema_internal.h
index e53626a..fab6f2b 100644
--- a/src/tree_schema_internal.h
+++ b/src/tree_schema_internal.h
@@ -894,7 +894,7 @@
  * @return Whether it needs (has) a compiled module or not.
  */
 #define LYSP_HAS_COMPILED(PMOD) \
-        (LYSP_HAS_RECOMPILED(PMOD) || PMOD->identities || PMOD->augments || PMOD->deviations)
+        (LYSP_HAS_RECOMPILED(PMOD) || PMOD->augments || PMOD->deviations)
 
 /**
  * @brief Learn whether the module has statements that need to be compiled or not.