schema compile FEATURE support for glob unres reset

That preserves creating and implementing module sets.
diff --git a/src/context.c b/src/context.c
index 89cb767..2296090 100644
--- a/src/context.c
+++ b/src/context.c
@@ -20,6 +20,7 @@
 
 #include "context.h"
 
+#include <assert.h>
 #include <errno.h>
 #include <pthread.h>
 #include <stdarg.h>
@@ -205,10 +206,10 @@
 cleanup:
     if (ret) {
         lys_compile_unres_glob_revert(ctx, &unres);
-        result = NULL;
+        mod = NULL;
     }
-    lys_compile_unres_glob_erase(ctx, &unres);
-    return result;
+    lys_compile_unres_glob_erase(ctx, &unres, 0);
+    return mod;
 }
 
 API LY_ERR
@@ -298,14 +299,12 @@
     }
 
     ly_in_free(in, 0);
-    lys_compile_unres_glob_erase(ctx, &unres);
-    *new_ctx = ctx;
-    return rc;
-
-error:
-    ly_in_free(in, 0);
-    lys_compile_unres_glob_erase(ctx, &unres);
-    ly_ctx_destroy(ctx);
+    lys_compile_unres_glob_erase(ctx, &unres, 0);
+    if (rc) {
+        ly_ctx_destroy(ctx);
+    } else {
+        *new_ctx = ctx;
+    }
     return rc;
 }
 
diff --git a/src/schema_compile.c b/src/schema_compile.c
index 89db6db..5ae837a 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -1371,12 +1371,14 @@
 }
 
 void
-lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres)
+lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres, ly_bool recompiled)
 {
     uint32_t i;
 
-    ly_set_erase(&unres->implementing, NULL);
-    ly_set_erase(&unres->creating, NULL);
+    if (!recompiled) {
+        ly_set_erase(&unres->implementing, NULL);
+        ly_set_erase(&unres->creating, NULL);
+    }
     for (i = 0; i < unres->dflts.count; ++i) {
         lysc_unres_dflt_free(ctx, unres->dflts.objs[i]);
     }
diff --git a/src/schema_compile.h b/src/schema_compile.h
index 992c34d..d186fb0 100644
--- a/src/schema_compile.h
+++ b/src/schema_compile.h
@@ -257,12 +257,13 @@
 void lys_compile_unres_glob_revert(struct ly_ctx *ctx, struct lys_glob_unres *unres);
 
 /**
- * @brief Erase all the global unres sets.
+ * @brief Erase the global unres.
  *
  * @param[in] ctx libyang context.
  * @param[in] unres Global unres structure with the sets.
+ * @param[in] recompiled Whether to keep the set of new parsed and implemented modules.
  */
-void lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres);
+void lys_compile_unres_glob_erase(const struct ly_ctx *ctx, struct lys_glob_unres *unres, ly_bool recompiled);
 
 /**
  * @brief Recompile the whole context based on the current flags.
diff --git a/src/tree_schema.c b/src/tree_schema.c
index 519df21..02257f5 100644
--- a/src/tree_schema.c
+++ b/src/tree_schema.c
@@ -1457,7 +1457,7 @@
     } else if (module) {
         *module = mod;
     }
-    lys_compile_unres_glob_erase(ctx, &unres);
+    lys_compile_unres_glob_erase(ctx, &unres, 0);
     return ret;
 }