tree schema BUGFIX unlinking derived identities on error

Fixes #1808
diff --git a/src/tree_schema_free.c b/src/tree_schema_free.c
index fdb0bd5..c6c9344 100644
--- a/src/tree_schema_free.c
+++ b/src/tree_schema_free.c
@@ -611,6 +611,74 @@
     FREE_ARRAY(ctx, must->exts, lysc_ext_instance_free);
 }
 
+static void
+lysc_ident_derived_unlink(const struct lysc_ident *ident)
+{
+    LY_ARRAY_COUNT_TYPE u, v, w;
+    const struct lysp_submodule *submod;
+    const struct lysp_module *base_pmod;
+    const struct lysp_ident *identp = NULL;
+    const struct lys_module *mod;
+    const char *base_name;
+
+    /* find the parsed identity */
+    LY_ARRAY_FOR(ident->module->parsed->identities, u) {
+        if (ident->module->parsed->identities[u].name == ident->name) {
+            identp = &ident->module->parsed->identities[u];
+            base_pmod = ident->module->parsed;
+            break;
+        }
+    }
+    if (!identp) {
+        LY_ARRAY_FOR(ident->module->parsed->includes, v) {
+            submod = ident->module->parsed->includes[v].submodule;
+            LY_ARRAY_FOR(submod->identities, u) {
+                if (submod->identities[u].name == ident->name) {
+                    identp = &submod->identities[u];
+                    base_pmod = (struct lysp_module *)submod;
+                    break;
+                }
+            }
+        }
+    }
+    assert(identp);
+
+    /* remove link from all the foreign bases, it may not be there if identity compilation failed */
+    LY_ARRAY_FOR(identp->bases, u) {
+        base_name = strchr(identp->bases[u], ':');
+        if (!base_name) {
+            continue;
+        }
+
+        /* prefixed identity */
+        mod = ly_resolve_prefix(ident->module->ctx, identp->bases[u], base_name - identp->bases[u], LY_VALUE_SCHEMA,
+                (void *)base_pmod);
+        if (!mod) {
+            continue;
+        }
+        ++base_name;
+
+        /* find the compiled base */
+        LY_ARRAY_FOR(mod->identities, v) {
+            if (!strcmp(mod->identities[v].name, base_name)) {
+                /* find the derived link */
+                LY_ARRAY_FOR(mod->identities[v].derived, w) {
+                    if (mod->identities[v].derived[w] == ident) {
+                        /* remove the link */
+                        LY_ARRAY_DECREMENT(mod->identities[v].derived);
+                        if (w < LY_ARRAY_COUNT(mod->identities[v].derived)) {
+                            memmove(mod->identities[v].derived + w, mod->identities[v].derived + w + 1,
+                                    (LY_ARRAY_COUNT(mod->identities[v].derived) - w) * sizeof ident);
+                        }
+                        break;
+                    }
+                }
+                break;
+            }
+        }
+    }
+}
+
 void
 lysc_ident_free(struct ly_ctx *ctx, struct lysc_ident *ident)
 {
@@ -1014,14 +1082,23 @@
 }
 
 void
-lys_module_free(struct lys_module *module)
+lys_module_free(struct lys_module *module, ly_bool remove_links)
 {
+    LY_ARRAY_COUNT_TYPE u;
+
     if (!module) {
         return;
     }
+
     assert(!module->implemented);
     assert(!module->compiled);
 
+    if (remove_links) {
+        /* remove derived identity links */
+        LY_ARRAY_FOR(module->identities, u) {
+            lysc_ident_derived_unlink(&module->identities[u]);
+        }
+    }
     FREE_ARRAY(module->ctx, module->identities, lysc_ident_free);
     lysp_module_free(module->parsed);