595792 fixing a RelaxNG bug introduced in 2.7.4

* relaxng.c: refs definitions added from inported schemas should not
  be processed as refs from the main schemas
* test/relaxng/595792* result/relaxng/595792*: add the test to the
  regression suite
diff --git a/relaxng.c b/relaxng.c
index d124d72..075fc4a 100644
--- a/relaxng.c
+++ b/relaxng.c
@@ -149,6 +149,7 @@
 #define IS_PROCESSED		(1 << 5)
 #define IS_COMPILABLE		(1 << 6)
 #define IS_NOT_COMPILABLE	(1 << 7)
+#define IS_EXTERNAL_REF	        (1 << 8)
 
 struct _xmlRelaxNGDefine {
     xmlRelaxNGType type;        /* the type of definition */
@@ -4662,6 +4663,8 @@
     xmlRelaxNGDefinePtr def = (xmlRelaxNGDefinePtr) payload;
     int tmp;
 
+    def->dflags |= IS_EXTERNAL_REF;
+
     tmp = xmlHashAddEntry(ctxt->grammar->refs, name, def);
     if (tmp < 0) {
         xmlRelaxNGDefinePtr prev;
@@ -5668,6 +5671,12 @@
     xmlRelaxNGGrammarPtr grammar;
     xmlRelaxNGDefinePtr def, cur;
 
+    /*
+     * Those rules don't apply to imported ref from xmlRelaxNGParseImportRef
+     */
+    if (ref->dflags & IS_EXTERNAL_REF)
+        return;
+
     grammar = ctxt->grammar;
     if (grammar == NULL) {
         xmlRngPErr(ctxt, ref->node, XML_ERR_INTERNAL_ERROR,
diff --git a/result/relaxng/595792-ext_err b/result/relaxng/595792-ext_err
new file mode 100644
index 0000000..092fdbd
--- /dev/null
+++ b/result/relaxng/595792-ext_err
@@ -0,0 +1 @@
+./test/relaxng/595792-ext.rng validates
diff --git a/result/relaxng/595792-ext_valid b/result/relaxng/595792-ext_valid
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/result/relaxng/595792-ext_valid
diff --git a/result/relaxng/595792_0 b/result/relaxng/595792_0
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/result/relaxng/595792_0
diff --git a/result/relaxng/595792_0.err b/result/relaxng/595792_0.err
new file mode 100644
index 0000000..abd73cd
--- /dev/null
+++ b/result/relaxng/595792_0.err
@@ -0,0 +1 @@
+./test/relaxng/595792_0.xml validates
diff --git a/result/relaxng/595792_err b/result/relaxng/595792_err
new file mode 100644
index 0000000..e9f3192
--- /dev/null
+++ b/result/relaxng/595792_err
@@ -0,0 +1 @@
+./test/relaxng/595792.rng validates
diff --git a/result/relaxng/595792_valid b/result/relaxng/595792_valid
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/result/relaxng/595792_valid
diff --git a/test/relaxng/595792-ext.rng b/test/relaxng/595792-ext.rng
new file mode 100644
index 0000000..58990cb
--- /dev/null
+++ b/test/relaxng/595792-ext.rng
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0">
+  <start>
+    <element name="t">
+      <ref name="tcont"/>
+    </element>
+  </start>
+  <define name="tcont">
+    <text/>
+  </define>
+</grammar>
diff --git a/test/relaxng/595792.rng b/test/relaxng/595792.rng
new file mode 100644
index 0000000..fb2eb93
--- /dev/null
+++ b/test/relaxng/595792.rng
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0">
+  <start>
+    <element name="root">
+      <externalRef href="595792-ext.rng"/>
+    </element>
+  </start>
+</grammar>
diff --git a/test/relaxng/595792_0.xml b/test/relaxng/595792_0.xml
new file mode 100644
index 0000000..546c589
--- /dev/null
+++ b/test/relaxng/595792_0.xml
@@ -0,0 +1,4 @@
+<?xml version="1.0"?>
+<root>
+  <t>foo</t>
+</root>