schema compile BUGFIX uses when parsed module

Fixes #1779
diff --git a/src/schema_compile.c b/src/schema_compile.c
index 869eea8..faa487d 100644
--- a/src/schema_compile.c
+++ b/src/schema_compile.c
@@ -146,7 +146,8 @@
 }
 
 LIBYANG_API_DEF LY_ERR
-lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt, void **instance_p, enum ly_stmt_cardinality *cardinality_p)
+lysc_ext_substmt(const struct lysc_ext_instance *ext, enum ly_stmt substmt, void **instance_p,
+        enum ly_stmt_cardinality *cardinality_p)
 {
     LY_ARRAY_COUNT_TYPE u;
 
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 */
diff --git a/tests/utests/schema/test_tree_schema_compile.c b/tests/utests/schema/test_tree_schema_compile.c
index fd148a9..3c12f70 100644
--- a/tests/utests/schema/test_tree_schema_compile.c
+++ b/tests/utests/schema/test_tree_schema_compile.c
@@ -3688,6 +3688,43 @@
             "    }\n"
             "}",
             LYS_IN_YANG, NULL));
+
+    ly_ctx_set_module_imp_clb(UTEST_LYCTX, test_imp_clb,
+            "module c1 {"
+            "  namespace urn:c1;"
+            "  prefix c1;"
+            "  container my-container {"
+            "    leaf my-type {"
+            "      type string;"
+            "    }"
+            "  }"
+            "}\n"
+            "module c2 {"
+            "  namespace \"urn:c2\";"
+            "  prefix c2;"
+            "  grouping my-group {"
+            "    leaf my-leaf {"
+            "      type string;"
+            "    }"
+            "  }"
+            "}");
+    assert_int_equal(LY_SUCCESS, lys_parse_mem(UTEST_LYCTX,
+            "module c3 {"
+            "  namespace \"urn:c3\";"
+            "  prefix c3;"
+            "  import c1 {"
+            "    prefix c1;"
+            "  }"
+            "  import c2 {"
+            "    prefix c2;"
+            "  }"
+            "  augment \"/c1:my-container\" {"
+            "    uses c2:my-group {"
+            "      when \"./c1:my-type = '42'\";"
+            "    }"
+            "  }"
+            "}",
+            LYS_IN_YANG, NULL));
 }
 
 int