schema compile BUGFIX uses when parsed module

Fixes #1779
diff --git a/src/schema_compile_node.c b/src/schema_compile_node.c
index dc2afaa..4523e84 100644
--- a/src/schema_compile_node.c
+++ b/src/schema_compile_node.c
@@ -3765,15 +3765,15 @@
     ret = lys_precompile_uses_augments_refines(ctx, uses_p, parent);
     LY_CHECK_GOTO(ret, cleanup);
 
-    /* switch context's parsed module being processed */
-    ctx->pmod = grp_mod;
-
     /* compile special uses status flags */
     uses_flags = uses_p->flags;
     ret = lys_compile_status(ctx, &uses_flags, "<uses>", parent ? parent->flags : 0, parent ? parent->name : NULL, 0);
     LY_CHECK_GOTO(ret, cleanup);
     uses_flags |= LYS_STATUS_USES;
 
+    /* switch context's parsed module being processed */
+    ctx->pmod = grp_mod;
+
     /* compile data nodes */
     LY_LIST_FOR(grp->child, pnode) {
         ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
@@ -3785,16 +3785,6 @@
         LY_CHECK_GOTO(ret = ly_set_merge(child_set, &uses_child_set, 1, NULL), cleanup);
     }
 
-    if (uses_p->when) {
-        /* pass uses's when to all the data children */
-        for (i = 0; i < uses_child_set.count; ++i) {
-            child = uses_child_set.snodes[i];
-
-            ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
-            LY_CHECK_GOTO(ret, cleanup);
-        }
-    }
-
     /* compile actions */
     if (grp->actions) {
         struct lysc_node_action **actions;
@@ -3810,14 +3800,6 @@
             ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
             LY_CHECK_GOTO(ret, cleanup);
         }
-
-        if (uses_p->when) {
-            /* inherit when */
-            LY_LIST_FOR((struct lysc_node *)*actions, child) {
-                ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
-                LY_CHECK_GOTO(ret, cleanup);
-            }
-        }
     }
 
     /* compile notifications */
@@ -3836,13 +3818,18 @@
             ret = lys_compile_node(ctx, pnode, parent, uses_flags, &uses_child_set);
             LY_CHECK_GOTO(ret, cleanup);
         }
+    }
 
-        if (uses_p->when) {
-            /* inherit when */
-            LY_LIST_FOR((struct lysc_node *)*notifs, child) {
-                ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
-                LY_CHECK_GOTO(ret, cleanup);
-            }
+    /* restore the previous context parsed module where uses is defined */
+    ctx->pmod = mod_old;
+
+    if (uses_p->when) {
+        /* pass uses's when to all the data children */
+        for (i = 0; i < uses_child_set.count; ++i) {
+            child = uses_child_set.snodes[i];
+
+            ret = lys_compile_when(ctx, uses_p->when, uses_flags, parent, lysc_data_node(parent), child, &when_shared);
+            LY_CHECK_GOTO(ret, cleanup);
         }
     }
 
@@ -3873,7 +3860,7 @@
     LY_CHECK_GOTO(ret, cleanup);
 
 cleanup:
-    /* reload previous context's parsed module being processed */
+    /* restore previous context's parsed module being processed */
     ctx->pmod = mod_old;
 
     /* remove the grouping from the stack for circular groupings dependency check */