Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
Fixed async lambda resolve bug.
Browse files Browse the repository at this point in the history
Would be nice to have a newer c# language spec ...
  • Loading branch information
mkrueger committed Feb 25, 2013
1 parent efda0c5 commit 916654c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ICSharpCode.NRefactory.CSharp/Resolver/ResolveVisitor.cs
Expand Up @@ -2601,6 +2601,14 @@ void AnalyzeLambda(AstNode body, bool isAsync, out bool isValidAsVoidMethod, out
for (int i = 0; i < returnValues.Count; i++) {
returnValues[i] = resolveResultCache[returnExpressions[i]];
}

// async lambdas without return statements are resolved as Task return types.
if (returnExpressions.Count == 0 && isAsync) {
inferredReturnType = resolver.Compilation.FindType(KnownTypeCode.Task);
Log.WriteLine("Lambda return type was inferred to: " + inferredReturnType);
return;
}

TypeInference ti = new TypeInference(resolver.Compilation, resolver.conversions);
bool tiSuccess;
inferredReturnType = ti.GetBestCommonType(returnValues, out tiSuccess);
Expand Down
33 changes: 33 additions & 0 deletions ICSharpCode.NRefactory.Tests/CSharp/Resolver/LambdaTests.cs
Expand Up @@ -635,5 +635,38 @@ public void MultipleOverloadsWithImplicitLambda3()
Assert.IsFalse(mrr.IsError);
Assert.AreEqual("System.Int32", mrr.Type.ReflectionName);
}

[Test]
public void AsyncLambdaWithAwait()
{
string program = @"
using System;
using System.Threading.Tasks;
class A
{
public Task OpenAsync ()
{
return null;
}
}
class C
{
async void Foo ()
{
await $Test (async () => { await new A().OpenAsync (); })$;
}
T Test<T> (Func<T> func)
{
return default (T);
}
}
";
var mrr = Resolve<CSharpInvocationResolveResult>(program);
Assert.IsFalse(mrr.IsError);
Assert.AreEqual("System.Threading.Tasks.Task", mrr.Type.ReflectionName);
}
}
}

0 comments on commit 916654c

Please sign in to comment.