Better error reporting for missing using directives
authorMarek Safar <marek.safar@gmail.com>
Mon, 9 Jul 2012 17:16:53 +0000 (18:16 +0100)
committerMarek Safar <marek.safar@gmail.com>
Mon, 9 Jul 2012 17:16:53 +0000 (18:16 +0100)
31 files changed:
mcs/errors/cs0234.cs
mcs/errors/cs0246-10.cs
mcs/errors/cs0246-11.cs
mcs/errors/cs0246-12.cs
mcs/errors/cs0246-13.cs
mcs/errors/cs0246-14.cs
mcs/errors/cs0246-15.cs
mcs/errors/cs0246-16.cs
mcs/errors/cs0246-17.cs
mcs/errors/cs0246-18.cs
mcs/errors/cs0246-19.cs
mcs/errors/cs0246-2.cs
mcs/errors/cs0246-20.cs
mcs/errors/cs0246-21.cs
mcs/errors/cs0246-22.cs
mcs/errors/cs0246-23.cs
mcs/errors/cs0246-24.cs
mcs/errors/cs0246-25.cs
mcs/errors/cs0246-26.cs
mcs/errors/cs0246-27.cs
mcs/errors/cs0246-28.cs
mcs/errors/cs0246-3.cs
mcs/errors/cs0246-4.cs
mcs/errors/cs0246-5.cs
mcs/errors/cs0246-6.cs
mcs/errors/cs0246-7.cs
mcs/errors/cs0246-8.cs
mcs/errors/cs0246.cs
mcs/errors/cs0400.cs
mcs/mcs/ecore.cs
mcs/mcs/namespace.cs

index 0ebeecc95be419e4db354cffd898ec6763203194..a4ebec291b29a365922da74047642bad13044eb5 100644 (file)
@@ -1,5 +1,5 @@
-// CS0234: The type or namespace name `Linq' does not exist in the namespace `System'. Are you missing an assembly reference?
-// Line: 3
+// CS0234: The type or namespace name `Linq' does not exist in the namespace `System'. Are you missing `System.Core' assembly reference?
+// Line: 5
 // Compiler options: -noconfig
 
 using System.Linq;
index 78289f373da90d4f9df570458b9f630e8ba81f5c..241d856a62ac88ad13799a5bdcb288bb5cb97d26 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `Nonexistant' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `Nonexistant' could not be found. Are you missing an assembly reference?
 // Line: 5
 
 using System;
index 61653f7d78d5fcbacb20b8631dd56fa9a9659e61..b6bd9e809782290f21e19ead23d752959ec8e456 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `aa' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `aa' could not be found. Are you missing an assembly reference?
 // Line: 6
 
 public class Test
index bf50bf3738e3a8039d6b061296cbbec63a57dae6..a2d8cad367827d0bc35ab9ab233d9f62cbdbb42d 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `RNGCryptoServiceProvider' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `RNGCryptoServiceProvider' could not be found. Are you missing `System.Security.Cryptography' using directive?
 // Line: 13
 
 using System;
index 1bc98bc550ab27507664f5102a6b9ae68d4912de..ecf4383faf7cf1788ff13919e52f49bc5bfe8377 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `class' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `class' could not be found. Are you missing an assembly reference?
 // Line: 9
 using System;
 
index 272bf944b670b68a0bfd5ba774d558ee292f176b..27c6c0aee715e65ec2d3bc634c32239186b68bdf 100644 (file)
@@ -1,8 +1,6 @@
-// CS0246: The type or namespace name `Uri' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `Uri' could not be found. Are you missing `System' using directive?
 // Line: 7
 
-// This bug actual checks that the compiler doesn't crash
-
 public interface IFoo
 {
         string Heh { get; } // significant to cause the error.
index a71859fd04363ce0fdf84856904dc0191b4e0d03..b91045f49523f9c3c5ed9d156a9aab7a2beaec3f 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `InvalidTypeBlah' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `InvalidTypeBlah' could not be found. Are you missing an assembly reference?
 // Line: 17
 
 //
index da0c8bf38f5260e2c24fb4ab0189bf175aef0b72..9512a6e72a67162a72038227d1ae238876094ffe 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `B' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `B' could not be found. Are you missing an assembly reference?
 // Line: 4
 
 class A : B
index 53b4c4204071344fc8197b316a1c19e72aa07c7e..38b1e64c686bf74c16fcd57193caba0d588e4583 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `Foo' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `Foo' could not be found. Are you missing an assembly reference?
 // Line: 4
 
 using Foo;
index 077c4710fd16ee33010326bbbf0955151118deef..60d6e99bfa7340df1e8898bfad95c1cd28ac6c26 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `dynamic' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `dynamic' could not be found. Are you missing an assembly reference?
 // Line: 9
 // Compiler options: -langversion:ISO-2
 
index 23feece9ae6a0d40082224bdb3f3e4f9996aed49..fe495d39b137527ddffaab2515ee5b7558dd86d6 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `aGgt' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `aGgt' could not be found. Are you missing an assembly reference?
 // Line: 13
 
 using System;
index cbca0ab05e030a0f79bb2fb086a585b880a7256e..730e0bc044aee71f02d4d9acc53049530de9efb8 100644 (file)
@@ -1,6 +1,6 @@
-// CS0246: The type or namespace name `ErrorType' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `ErrorType' could not be found. Are you missing an assembly reference?
 // Line: 5
 
 interface A {
         event ErrorType Test;
-}
\ No newline at end of file
+}
index 2dd3fc3601a84cb872538aa6c72d29ed971d92e0..242af0ef800b69a5b86b773eba1a2038443be8fa 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `C' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `C' could not be found. Are you missing an assembly reference?
 // Line: 5
 
 using C = Cks.Runtime.Data;
index 643d4f152ce351bd6c28cada0b6ac24fe4081489..cf3bbb56fb7cef989e9b70c39b57076374e9daef 100644 (file)
@@ -1,5 +1,5 @@
-// CS0246: The type or namespace name `IBase' could not be found. Are you missing a using directive or an assembly reference?
-// Line: 155555
+// CS0246: The type or namespace name `IBase' could not be found. Are you missing `Foo' using directive?
+// Line: 15
 
 namespace Foo {
         public interface IBase {
index e675a304f6459c20a3863594e0aa40b776f21ce4..f3171b5a68c79298fd5e148fa735aba0911fd8e8 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `var' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `var' could not be found. Are you missing an assembly reference?
 // Line: 8
 
 class C
index 453843e04e6ae09f195285f0755e452dba971513..186a7a5e9a79a66487e820df80679eee0a23a8ee 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `var' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `var' could not be found. Are you missing an assembly reference?
 // Line: 8
 
 
index bd7ea63a81f563e01f713f6b7e91726fff05162b..45239fe83ef82334e94fe225f98d13e0bf4588f1 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `TypeMe' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `TypeMe' could not be found. Are you missing an assembly reference?
 // Line: 12
 
 class C
index 1050967f9698951c1e73eaf64d94e0e2588988eb..60aa5e4ce9563d9f100d5ed39a76270584754799 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `X' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `X' could not be found. Are you missing an assembly reference?
 // Line: 4
 
 class A<T> where T : X
index 5411c541d01b6b0c3449d8390f6e77a4052e8a1a..1bf142762a68f7de3b3c9e2dcdae0f144788e194 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `M' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `M' could not be found. Are you missing an assembly reference?
 // Line: 11
 
 interface I<T>
index 775a3daa957f4dd5baf0c41d8cf17dabee77b08e..9a0afb5a97a866603f9680369d7c5680fd2679fb 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `NotExist' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `NotExist' could not be found. Are you missing an assembly reference?
 // Line: 6
 
 class X
index f13cd3d153c7c05d1b2ae0fe5814f4b5e1c3f4ac..35f15e14f4d8f988e7863b0e55deb2db007c1df1 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `B' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `B' could not be found. Are you missing an assembly reference?
 // Line: 4
 
 class A : C<B>
index b60bbc7660e43745232cd276a5b0704227711e24..82e7492789592d96965c90175bd2533ef8005cf2 100644 (file)
@@ -1,6 +1,6 @@
-// CS0246: The type or namespace name `ErrorType' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `ErrorType' could not be found. Are you missing an assembly reference?
 // Line: 5
 
 interface A {
         ErrorType Method();
-}
\ No newline at end of file
+}
index 1e364b3fe4861d391d2f26256ebba0901cdc1af4..4a8482cc52aaa0ea0ee53642290ff234e8e1dd41 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `ErrorType' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `ErrorType' could not be found. Are you missing an assembly reference?
 // Line: 4
 
-public delegate ErrorType Delegate ();
\ No newline at end of file
+public delegate ErrorType Delegate ();
index 7216a4e21318d5d39eca058fea2b9b269b326660..6c464629e7e3713573183893610b5e204438d864 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `AAAA' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `AAAA' could not be found. Are you missing an assembly reference?
 // Line: 7
 
 using System;
index 3379cbc7761e72198251e171a51afb369ccb2129..ffb49427efba4473d20e134b5011dd86e6cc57c5 100644 (file)
@@ -1,6 +1,5 @@
-// CS0246: The type or namespace name `XmlDocument' could not be found. Are you missing a using directive or an assembly reference?
-// Line: 7
-// This is bug 55770
+// CS0246: The type or namespace name `XmlDocument' could not be found. Are you missing `System.Xml' using directive?
+// Line: 6
 
 using System;
 using System.Xml;
@@ -10,4 +9,4 @@ public class Test {
        public static void Main ()
        {
        }
-}
\ No newline at end of file
+}
index 88a9da1e242366296277d2afc04ab0c1f44c37ab..b4116daa8cc9a90f37aca1ee2c37574421b2324f 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `foo' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `foo' could not be found. Are you missing an assembly reference?
 // Line: 5
 
 struct bar {
index 80f4d791385a766ebe38a0a4a0d0ea7c2c6b8c68..126b707b195cfb1902ae84a1c0ceac3953519ffb 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `Reflection' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `Reflection' could not be found. Are you missing an assembly reference?
 // Line: 7
 using System;
 
index 61bc773f6e32fb9bd814ac69d85019d9a38aae76..f774424e85f9a2f8759afb4db9187c7f779ebd5f 100644 (file)
@@ -1,4 +1,4 @@
-// CS0246: The type or namespace name `B' could not be found. Are you missing a using directive or an assembly reference?
+// CS0246: The type or namespace name `B' could not be found. Are you missing an assembly reference?
 // Line: 4
 interface A : B {
 }
index 7a355b64b5a57798643130050e3e359e5e3fb9ec..e279709e45513be6a3807a04f5ecdf6614713870 100644 (file)
@@ -1,4 +1,4 @@
-// CS0400: The type or namespace name `N' could not be found in the global namespace (are you missing an assembly reference?)
+// CS0400: The type or namespace name `N' could not be found in the global namespace. Are you missing an assembly reference?
 // Line: 8
 
 class C
@@ -7,4 +7,4 @@ class C
        {
                global::N n = null;
        }
-}
\ No newline at end of file
+}
index 86918b1c7ad2815c0fdaa0fbf58f3af0e48adcb5..2e9ce0ee4aa55c39e4faf22fea044fc7eee21f42 100644 (file)
@@ -2277,9 +2277,11 @@ namespace Mono.CSharp {
                                }
                        }
 
+                       var report = ctx.Module.Compiler.Report;
+
                        var retval = ctx.LookupNamespaceOrType (Name, Arity, LookupMode.IgnoreAccessibility, loc);
                        if (retval != null) {
-                               ctx.Module.Compiler.Report.SymbolRelatedToPreviousError (retval.Type);
+                               report.SymbolRelatedToPreviousError (retval.Type);
                                ErrorIsInaccesible (ctx, retval.GetSignatureForError (), loc);
                                return;
                        }
@@ -2290,7 +2292,17 @@ namespace Mono.CSharp {
                                return;
                        }
 
-                       NamespaceContainer.Error_NamespaceNotFound (loc, Name, ctx.Module.Compiler.Report);
+                       var ns_candidates = ctx.Module.GlobalRootNamespace.FindTypeNamespaces (ctx, Name, Arity);
+                       if (ns_candidates != null) {
+                               string usings = string.Join ("' or `", ns_candidates);
+                               report.Error (246, loc,
+                                       "The type or namespace name `{0}' could not be found. Are you missing `{1}' using directive?",
+                                       Name, usings);
+                       } else {
+                               report.Error (246, loc,
+                                       "The type or namespace name `{0}' could not be found. Are you missing an assembly reference?",
+                                       Name);
+                       }
                }
 
                public override FullNamedExpression ResolveAsTypeOrNamespace (IMemberContext ec)
index 6b44a9f8d8e132bce1a5e2f0f3ae2464ba2d8290..8e46c3c16955498a1e24efdad142f8754c8d1ac0 100644 (file)
@@ -41,6 +41,26 @@ namespace Mono.CSharp {
                        report.Error (1681, loc, "The global extern alias cannot be redefined");
                }
 
+               //
+               // For better error reporting where we try to guess missing using directive
+               //
+               public List<string> FindTypeNamespaces (IMemberContext ctx, string name, int arity)
+               {
+                       List<string> res = null;
+
+                       foreach (var ns in all_namespaces) {
+                               var type = ns.Value.LookupType (ctx, name, arity, LookupMode.Normal, Location.Null);
+                               if (type != null) {
+                                       if (res == null)
+                                               res = new List<string> ();
+
+                                       res.Add (ns.Key);
+                               }
+                       }
+
+                       return res;
+               }
+
                public void RegisterNamespace (Namespace child)
                {
                        if (child != this)
@@ -181,14 +201,51 @@ namespace Mono.CSharp {
                                return;
                        }
 
+                       string assembly = null;
+                       string possible_name = fullname + "." + name;
+
+                       // Only assembly unique name should be added
+                       switch (possible_name) {
+                       case "System.Drawing":
+                       case "System.Web.Services":
+                       case "System.Web":
+                       case "System.Data":
+                       case "System.Configuration":
+                       case "System.Data.Services":
+                       case "System.DirectoryServices":
+                       case "System.Json":
+                       case "System.Net.Http":
+                       case "System.Numerics":
+                       case "System.Runtime.Caching":
+                       case "System.ServiceModel":
+                       case "System.Transactions":
+                       case "System.Web.Routing":
+                       case "System.Xml.Linq":
+                       case "System.Xml":
+                               assembly = possible_name;
+                               break;
+
+                       case "System.Linq":
+                       case "System.Linq.Expressions":
+                               assembly = "System.Core";
+                               break;
+
+                       case "System.Windows.Forms":
+                       case "System.Windows.Forms.Layout":
+                               assembly = "System.Windows.Name";
+                               break;
+                       }
+
+                       assembly = assembly == null ? "an" : "`" + assembly + "'";
+
                        if (this is GlobalRootNamespace) {
                                ctx.Module.Compiler.Report.Error (400, loc,
-                                       "The type or namespace name `{0}' could not be found in the global namespace (are you missing an assembly reference?)",
-                                       name);
+                                       "The type or namespace name `{0}' could not be found in the global namespace. Are you missing {1} assembly reference?",
+                                       name, assembly);
                        } else {
                                ctx.Module.Compiler.Report.Error (234, loc,
-                                       "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing an assembly reference?",
-                                       name, GetSignatureForError ());
+                                       "The type or namespace name `{0}' does not exist in the namespace `{1}'. Are you missing {2} assembly reference?",
+                                       name, GetSignatureForError (), assembly);
                        }
                }
 
@@ -1106,44 +1163,6 @@ namespace Mono.CSharp {
                        return match;
                }
 
-               static void MsgtryRef (string s)
-               {
-                       Console.WriteLine ("    Try using -r:" + s);
-               }
-
-               static void MsgtryPkg (string s)
-               {
-                       Console.WriteLine ("    Try using -pkg:" + s);
-               }
-
-               public static void Error_NamespaceNotFound (Location loc, string name, Report Report)
-               {
-                       Report.Error (246, loc, "The type or namespace name `{0}' could not be found. Are you missing a using directive or an assembly reference?",
-                               name);
-
-                       switch (name) {
-                       case "Gtk": case "GtkSharp":
-                               MsgtryPkg ("gtk-sharp-2.0");
-                               break;
-
-                       case "Gdk": case "GdkSharp":
-                               MsgtryPkg ("gdk-sharp-2.0");
-                               break;
-
-                       case "Glade": case "GladeSharp":
-                               MsgtryPkg ("glade-sharp-2.0");
-                               break;
-
-                       case "System.Drawing":
-                       case "System.Web.Services":
-                       case "System.Web":
-                       case "System.Data":
-                       case "System.Windows.Forms":
-                               MsgtryRef (name);
-                               break;
-                       }
-               }
-
                protected override void DefineNamespace ()
                {
                        if (namespace_using_table == null)