[mcs] Better error reporting for incorrect async lambdas
authorMarek Safar <marek.safar@gmail.com>
Tue, 3 Jun 2014 09:31:24 +0000 (11:31 +0200)
committerMarek Safar <marek.safar@gmail.com>
Tue, 3 Jun 2014 09:32:02 +0000 (11:32 +0200)
mcs/errors/cs1997-2.cs [deleted file]
mcs/errors/cs4029.cs [new file with mode: 0644]
mcs/errors/cs8031.cs [new file with mode: 0644]
mcs/mcs/statement.cs

diff --git a/mcs/errors/cs1997-2.cs b/mcs/errors/cs1997-2.cs
deleted file mode 100644 (file)
index 9f8c533..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-// CS1997: `System.Func<System.Threading.Tasks.Task>': A return keyword must not be followed by an expression when async delegate returns `Task'. Consider using `Task<T>' return type
-// Line: 12
-
-using System;
-using System.Threading.Tasks;
-
-class Test
-{
-       public static void Main()
-       {
-               Func<Task> t = async delegate {
-                       return null;
-               };
-
-               return;
-       }
-}
diff --git a/mcs/errors/cs4029.cs b/mcs/errors/cs4029.cs
new file mode 100644 (file)
index 0000000..14fce4f
--- /dev/null
@@ -0,0 +1,19 @@
+// CS4029: Cannot return an expression of type `void'
+// Line: 15
+
+using System;
+using System.Threading.Tasks;
+
+class C
+{
+       static void Foo<T> (Func<Task<T>> f)
+       {
+       }
+
+       static void Main ()
+       {
+               Foo (async () => {
+                       return await Task.Factory.StartNew (() => { });
+               });
+       }
+}
\ No newline at end of file
diff --git a/mcs/errors/cs8031.cs b/mcs/errors/cs8031.cs
new file mode 100644 (file)
index 0000000..dbb83bc
--- /dev/null
@@ -0,0 +1,17 @@
+// CS8031: Async lambda expression or anonymous method converted to a `Task' cannot return a value. Consider returning `Task<T>'
+// Line: 12
+
+using System;
+using System.Threading.Tasks;
+
+class Test
+{
+       public static void Main()
+       {
+               Func<Task> t = async delegate {
+                       return null;
+               };
+
+               return;
+       }
+}
index 07da0a80cbe35cb9f2609cf345b0c1e08ac00350..80296bec4568528dcd2d4a3fd6f32152516935ce 100644 (file)
@@ -1198,7 +1198,10 @@ namespace Mono.CSharp {
                                                var async_type = storey.ReturnType;
 
                                                if (async_type == null && async_block.ReturnTypeInference != null) {
-                                                       async_block.ReturnTypeInference.AddCommonTypeBoundAsync (expr.Type);
+                                                       if (expr.Type.Kind == MemberKind.Void && !(this is ContextualReturn))
+                                                               ec.Report.Error (4029, loc, "Cannot return an expression of type `void'");
+                                                       else
+                                                               async_block.ReturnTypeInference.AddCommonTypeBoundAsync (expr.Type);
                                                        return true;
                                                }
 
@@ -1214,18 +1217,14 @@ namespace Mono.CSharp {
                                                        if (this is ContextualReturn)
                                                                return true;
 
-                                                       // Same error code as .NET but better error message
                                                        if (async_block.DelegateType != null) {
-                                                               ec.Report.Error (1997, loc,
-                                                                       "`{0}': A return keyword must not be followed by an expression when async delegate returns `Task'. Consider using `Task<T>' return type",
-                                                                       async_block.DelegateType.GetSignatureForError ());
+                                                               ec.Report.Error (8031, loc,
+                                                                       "Async lambda expression or anonymous method converted to a `Task' cannot return a value. Consider returning `Task<T>'");
                                                        } else {
                                                                ec.Report.Error (1997, loc,
                                                                        "`{0}': A return keyword must not be followed by an expression when async method returns `Task'. Consider using `Task<T>' return type",
                                                                        ec.GetSignatureForError ());
-
                                                        }
-
                                                        return false;
                                                }