Bug 522078: [9] intermittent resolve errors from the reconciler

Change-Id: I9c3858ccdb526230bde775b1bf140dd3c483c8e3
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/env/NameEnvironmentAnswer.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/env/NameEnvironmentAnswer.java
index 6d3c33b..8b34a2d 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/env/NameEnvironmentAnswer.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/env/NameEnvironmentAnswer.java
@@ -10,6 +10,8 @@
  *******************************************************************************/
 package org.eclipse.jdt.internal.compiler.env;
 
+import java.util.Arrays;
+
 import org.eclipse.jdt.internal.compiler.lookup.ModuleBinding;
 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
 
@@ -70,7 +72,7 @@
 			baseString = "ICompilationUnit " + this.compilationUnit.toString(); //$NON-NLS-1$
 		}
 		if (this.sourceTypes != null) {
-			baseString = this.sourceTypes.toString();
+			baseString = Arrays.toString(this.sourceTypes);
 		}
 		if (this.accessRestriction != null) {
 			baseString += " " + this.accessRestriction.toString(); //$NON-NLS-1$
@@ -78,6 +80,9 @@
 		if (this.externalAnnotationPath != null) {
 			baseString += " extPath=" + this.externalAnnotationPath.toString(); //$NON-NLS-1$
 		}
+		if (this.moduleName != null) {
+			baseString += " module=" + String.valueOf(this.moduleName); //$NON-NLS-1$
+		}
 		return baseString;
 	}
 	
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java
index 69022e0..eb40746 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/ModuleBinding.java
@@ -537,8 +537,17 @@
 					// visible but foreign (when current is unnamed or auto):
 					for (char[] declaringModuleName : declaringModuleNames) {
 						ModuleBinding declaringModule = this.environment.root.getModule(declaringModuleName);
-						if (declaringModule != null && !declaringModule.isPackageLookupActive)
-							binding = SplitPackageBinding.combine(declaringModule.getDeclaredPackage(parentName, name), binding, this);
+						if (declaringModule != null && !declaringModule.isPackageLookupActive) {
+							PackageBinding declaredPackage = declaringModule.getDeclaredPackage(parentName, name);
+							if (declaredPackage != null) {
+								// don't add foreign package to 'parent' (below), but to its own parent:
+								if (declaredPackage.parent != null)
+									declaredPackage.parent.addPackage(declaredPackage, declaringModule, true);
+								parent = null;
+								//
+								binding = SplitPackageBinding.combine(declaredPackage, binding, this);
+							}
+						}
 					}
 				}
 			}
@@ -556,7 +565,7 @@
 		// remember
 		if (parentName.length == 0)
 			binding.environment.knownPackages.put(name, binding);
-		else
+		else if (parent != null)
 			binding = parent.addPackage(binding, this, false);
 		return addPackage(binding, false);
 	}