From: Marek Safar Date: Tue, 8 Oct 2013 17:30:32 +0000 (+0200) Subject: Adds async type inference of void return type. Fixes #15238 X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=commitdiff_plain;ds=inline;h=f33bd8ad7fe9470b1fae626b4e425a632a0963ca;p=mono.git Adds async type inference of void return type. Fixes #15238 --- diff --git a/mcs/mcs/anonymous.cs b/mcs/mcs/anonymous.cs index ce83e7674c1..94ab2796880 100644 --- a/mcs/mcs/anonymous.cs +++ b/mcs/mcs/anonymous.cs @@ -1535,10 +1535,14 @@ namespace Mono.CSharp { // // If e is synchronous the inferred return type is T - // If e is asynchronous the inferred return type is Task + // If e is asynchronous and the body of F is either an expression classified as nothing + // or a statement block where no return statements have expressions, the inferred return type is Task + // If e is async and has an inferred result type T, the inferred return type is Task // if (block.IsAsync && ReturnType != null) { - ReturnType = ec.Module.PredefinedTypes.TaskGeneric.TypeSpec.MakeGenericType (ec, new [] { ReturnType }); + ReturnType = ReturnType.Kind == MemberKind.Void ? + ec.Module.PredefinedTypes.Task.TypeSpec : + ec.Module.PredefinedTypes.TaskGeneric.TypeSpec.MakeGenericType (ec, new [] { ReturnType }); } } diff --git a/mcs/mcs/generic.cs b/mcs/mcs/generic.cs index 203290b70b6..13836984421 100644 --- a/mcs/mcs/generic.cs +++ b/mcs/mcs/generic.cs @@ -2948,15 +2948,20 @@ namespace Mono.CSharp { public void AddCommonTypeBound (TypeSpec type) { - AddToBounds (new BoundInfo (type, BoundKind.Lower), 0); + AddToBounds (new BoundInfo (type, BoundKind.Lower), 0, false); } - void AddToBounds (BoundInfo bound, int index) + public void AddCommonTypeBoundAsync (TypeSpec type) + { + AddToBounds (new BoundInfo (type, BoundKind.Lower), 0, true); + } + + void AddToBounds (BoundInfo bound, int index, bool voidAllowed) { // // Some types cannot be used as type arguments // - if (bound.Type.Kind == MemberKind.Void || bound.Type.IsPointer || bound.Type.IsSpecialRuntimeType || + if ((bound.Type.Kind == MemberKind.Void && !voidAllowed) || bound.Type.IsPointer || bound.Type.IsSpecialRuntimeType || bound.Type == InternalType.MethodGroup || bound.Type == InternalType.AnonymousMethod) return; @@ -3030,7 +3035,7 @@ namespace Mono.CSharp { if (pos == -1) return 0; - AddToBounds (new BoundInfo (u, BoundKind.Exact), pos); + AddToBounds (new BoundInfo (u, BoundKind.Exact), pos, false); return 1; } @@ -3361,7 +3366,7 @@ namespace Mono.CSharp { // If V is one of the unfixed type arguments int pos = IsUnfixed (v); if (pos != -1) { - AddToBounds (new BoundInfo (u, inversed ? BoundKind.Upper : BoundKind.Lower), pos); + AddToBounds (new BoundInfo (u, inversed ? BoundKind.Upper : BoundKind.Lower), pos, false); return 1; } diff --git a/mcs/mcs/statement.cs b/mcs/mcs/statement.cs index 9b0b7daafe0..b3bd8861cef 100644 --- a/mcs/mcs/statement.cs +++ b/mcs/mcs/statement.cs @@ -902,7 +902,7 @@ namespace Mono.CSharp { var async_type = storey.ReturnType; if (async_type == null && async_block.ReturnTypeInference != null) { - async_block.ReturnTypeInference.AddCommonTypeBound (expr.Type); + async_block.ReturnTypeInference.AddCommonTypeBoundAsync (expr.Type); return true; } diff --git a/mcs/tests/test-async-52.cs b/mcs/tests/test-async-52.cs new file mode 100644 index 00000000000..36230acd776 --- /dev/null +++ b/mcs/tests/test-async-52.cs @@ -0,0 +1,23 @@ +using System; +using System.Threading.Tasks; + +public delegate T ActualValueDelegate (); + +class X +{ + public static void Main () + { + Matches (async () => await Throw()); + } + + static bool Matches(ActualValueDelegate del) where T : Task + { + del ().Wait (); + return true; + } + + static async Task Throw() + { + await Task.Delay (1); + } +} \ No newline at end of file diff --git a/mcs/tests/ver-il-net_4_5.xml b/mcs/tests/ver-il-net_4_5.xml index 148f940ecbe..9f34ffa4574 100644 --- a/mcs/tests/ver-il-net_4_5.xml +++ b/mcs/tests/ver-il-net_4_5.xml @@ -59671,6 +59671,55 @@ + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + 37 + + + 30 + + + 33 + + + 33 + + + 7 + + + + + 157 + + + 13 + + + + + 160 + + + 13 + + +