-
Notifications
You must be signed in to change notification settings - Fork 151
BadImageFormatException when using Contract.Ensures in async method, that returns task without any await #235
Comments
I believe |
BTW, please note, that switching to "async" does not help either: in this case there is no For this code: class Program
{
public static async Task<T> Foo<T>(T source)
where T : class
{
Contract.Ensures(Contract.Result<T>() != null);
await Task.Yield();
return source;
//return Task.FromResult(source);
}
static void Main(string[] args)
{
Foo("foo").Wait();
}
} ccrewrite generates following code: public static Task<T> Foo<T>(T source) where T : class
{
Program.<Foo>d__0<T> <Foo>d__;
<Foo>d__.source = source;
<Foo>d__.<>t__builder = AsyncTaskMethodBuilder<T>.Create();
<Foo>d__.<>1__state = -1;
AsyncTaskMethodBuilder<T> <>t__builder = <Foo>d__.<>t__builder;
<>t__builder.Start<Program.<Foo>d__0<T>>(ref <Foo>d__);
Task<T> task = <Foo>d__.<>t__builder.Task;
Task<T> task2 = task;
if (__ContractsRuntime.insideContractEvaluation <= 4)
{
try
{
__ContractsRuntime.insideContractEvaluation++;
__ContractsRuntime.Ensures(task2.Result != null, null, "Contract.Result<T>() != null");
}
finally
{
__ContractsRuntime.insideContractEvaluation--;
}
}
return task2;
} I.e. the postcondition is synchronous and not asynchronous! |
Add type mapping to change generic type reference in async closure that is called from generic async methods.
@Dennis-Petrov I've posted PR (#278) with a fix. Will appreciate some feedback:) |
@SergeyTeplyakov, sorry, I've lost sight of this issue.
P.S. I've installed fresh build from master branch (built using VS2013) - the problem still persists. |
Using 1.10.20606.1 |
Hi. This code:
leads to
BadImageFormatException
, while this one - does not:The only difference is awaiting the task in the second sample, which is totally unnecessary here.
It seems to me, that using
Contract.Result<T>()
inFoo
is logically incorrect too .We're not awaiting a task result, we're just returning a task. Though,BadImageFormatException
shouldn't be thrown, and I need a way to point CC to check task result here.Is there any workarounds instead of
await
?The text was updated successfully, but these errors were encountered: