Better logic for loading unique assemblies
authorMarek Safar <marek.safar@gmail.com>
Mon, 12 Dec 2011 16:52:52 +0000 (16:52 +0000)
committerMarek Safar <marek.safar@gmail.com>
Mon, 12 Dec 2011 16:54:03 +0000 (16:54 +0000)
mcs/class/IKVM.Reflection/Module.cs
mcs/errors/cs1703-2.cs [new file with mode: 0644]
mcs/mcs/assembly.cs
mcs/mcs/eval.cs
mcs/mcs/ikvm.cs
mcs/mcs/reflection.cs

index e4624803e3a073c69306ad00e7fd1b304e20d98e..95f9955765bbbba978f3173d2d1d374e0dd87686 100644 (file)
@@ -50,6 +50,11 @@ namespace IKVM.Reflection
                        get { return isManifestModule; }
                }
 
+               public Guid ModuleVersionId
+               {
+                       get { return module.ModuleVersionId; }
+               }
+
                private void CheckManifestModule()
                {
                        if (!IsManifestModule)
diff --git a/mcs/errors/cs1703-2.cs b/mcs/errors/cs1703-2.cs
new file mode 100644 (file)
index 0000000..ec57f82
--- /dev/null
@@ -0,0 +1,4 @@
+// CS1703: An assembly with the same identity `System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' has already been imported. Consider removing one of the references
+// Line: 0
+// Compiler options: -r:../class/lib/net_4_0/System.dll
+
index aefe4809d73840bef95aaba0ec51d47118d90500..cb12a9aad7f7945fd354ee8045b5b7926c0dad46 100644 (file)
@@ -1089,8 +1089,7 @@ namespace Mono.CSharp
 
                public abstract bool HasObjectType (T assembly);
                protected abstract string[] GetDefaultReferences ();
-               public abstract T LoadAssemblyFile (string fileName);
-               public abstract T LoadAssemblyDefault (string assembly);
+               public abstract T LoadAssemblyFile (string fileName, bool isImplicitReference);
                public abstract void LoadReferences (ModuleContainer module);
 
                protected void Error_FileNotFound (string fileName)
@@ -1127,14 +1126,14 @@ namespace Mono.CSharp
                        // Load mscorlib.dll as the first
                        //
                        if (module.Compiler.Settings.StdLib) {
-                               corlib_assembly = LoadAssemblyDefault ("mscorlib.dll");
+                               corlib_assembly = LoadAssemblyFile ("mscorlib.dll", true);
                        } else {
                                corlib_assembly = default (T);
                        }
 
                        T a;
                        foreach (string r in module.Compiler.Settings.AssemblyReferences) {
-                               a = LoadAssemblyFile (r);
+                               a = LoadAssemblyFile (r, false);
                                if (a == null || EqualityComparer<T>.Default.Equals (a, corlib_assembly))
                                        continue;
 
@@ -1152,7 +1151,7 @@ namespace Mono.CSharp
                        }
 
                        foreach (var entry in module.Compiler.Settings.AssemblyReferencesAliases) {
-                               a = LoadAssemblyFile (entry.Item2);
+                               a = LoadAssemblyFile (entry.Item2, false);
                                if (a == null)
                                        continue;
 
@@ -1165,7 +1164,7 @@ namespace Mono.CSharp
 
                        if (compiler.Settings.LoadDefaultReferences) {
                                foreach (string r in GetDefaultReferences ()) {
-                                       a = LoadAssemblyDefault (r);
+                                       a = LoadAssemblyFile (r, true);
                                        if (a == null)
                                                continue;
 
index c996bc5e2a699b52153572e2db7d469ee680b0bc..1399ddf882b3bb786639dbc73a416286e0da3439 100644 (file)
@@ -814,7 +814,7 @@ namespace Mono.CSharp
                public void LoadAssembly (string file)
                {
                        var loader = new DynamicLoader (importer, ctx);
-                       var assembly = loader.LoadAssemblyFile (file);
+                       var assembly = loader.LoadAssemblyFile (file, false);
                        if (assembly == null)
                                return;
 
index 18c97861a74fc71ae6a7cd91dad77cdd8f465447..c10a8b476fdb01f8fd928fae7c38ebc65cab1554 100644 (file)
@@ -369,7 +369,7 @@ namespace Mono.CSharp
                        return assembly.GetType (compiler.BuiltinTypes.Object.FullName) != null;
                }
 
-               public override Assembly LoadAssemblyFile (string fileName)
+               public override Assembly LoadAssemblyFile (string fileName, bool isImplicitReference)
                {
                        bool? has_extension = null;
                        foreach (var path in paths) {
@@ -406,7 +406,7 @@ namespace Mono.CSharp
                                                        if (an.Name != loaded_name.Name)
                                                                continue;
 
-                                                       if (an.CodeBase == loaded_name.CodeBase)
+                                                       if (module.ModuleVersionId == entry.Item3.ManifestModule.ModuleVersionId)
                                                                return entry.Item3;
                                                        
                                                        if (((an.Flags | loaded_name.Flags) & AssemblyNameFlags.PublicKey) == 0) {
@@ -438,12 +438,16 @@ namespace Mono.CSharp
                                                return assembly;
                                        }
                                } catch {
-                                       Error_FileCorrupted (file);
+                                       if (!isImplicitReference)
+                                               Error_FileCorrupted (file);
+
                                        return null;
                                }
                        }
 
-                       Error_FileNotFound (fileName);
+                       if (!isImplicitReference)
+                               Error_FileNotFound (fileName);
+
                        return null;
                }
 
@@ -472,39 +476,6 @@ namespace Mono.CSharp
                        return null;                            
                }
 
-               //
-               // Optimized default assembly loader version
-               //
-               public override Assembly LoadAssemblyDefault (string assembly)
-               {
-                       foreach (var path in paths) {
-                               var file = Path.Combine (path, assembly);
-
-                               if (compiler.Settings.DebugFlags > 0)
-                                       Console.WriteLine ("Probing default assembly location `{0}'", file);
-
-                               if (!File.Exists (file))
-                                       continue;
-
-                               try {
-                                       if (compiler.Settings.DebugFlags > 0)
-                                               Console.WriteLine ("Loading default assembly `{0}'", file);
-
-                                       var a = domain.LoadFile (file);
-                                       if (a != null) {
-                                               loaded_names.Add (Tuple.Create (a.GetName (), file, a));
-                                       }
-
-                                       return a;
-                               } catch {
-                                       // Default assemblies can fail to load without error
-                                       return null;
-                               }
-                       }
-
-                       return null;
-               }
-
                public override void LoadReferences (ModuleContainer module)
                {
                        List<Tuple<RootNamespace, Assembly>> loaded;
index 6f2f183a83b97ef251bdabdaba13838bff6956da..5181e3f3253645d491dedad8db7c4c6097ef8784 100644 (file)
@@ -442,12 +442,7 @@ namespace Mono.CSharp
                        return assembly.GetType (compiler.BuiltinTypes.Object.FullName) != null;
                }
 
-               public override Assembly LoadAssemblyFile (string fileName)
-               {
-                       return LoadAssemblyFile (fileName, false);
-               }
-
-               Assembly LoadAssemblyFile (string assembly, bool soft)
+               public override Assembly LoadAssemblyFile (string assembly, bool isImplicitReference)
                {
                        Assembly a = null;
 
@@ -464,7 +459,7 @@ namespace Mono.CSharp
                                                a = Assembly.Load (ass);
                                        }
                                } catch (FileNotFoundException) {
-                                       bool err = !soft;
+                                       bool err = !isImplicitReference;
                                        foreach (string dir in paths) {
                                                string full_path = Path.Combine (dir, assembly);
                                                if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
@@ -490,11 +485,6 @@ namespace Mono.CSharp
                        return a;
                }
 
-               public override Assembly LoadAssemblyDefault (string fileName)
-               {
-                       return LoadAssemblyFile (fileName, true);
-               }
-
                Module LoadModuleFile (AssemblyDefinitionDynamic assembly, string module)
                {
                        string total_log = "";