Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

BadImageFormatException when using Contract.Ensures in async method, that returns task without any await #235

@Dennis-Petrov

Description

@Dennis-Petrov

Hi. This code:

class Program
{
    static Task<T> Foo<T>(T source)
        where T : class
    {
        Contract.Ensures(Contract.Result<T>() != null);

        return Task.FromResult(source);
    }

    static void Main(string[] args)
    {
        Foo(new object()).Wait();
    }
}

leads to BadImageFormatException, while this one - does not:

    static async Task<T> Foo<T>(T source)
        where T : class
    {
        Contract.Ensures(Contract.Result<T>() != null);

        return await Task.FromResult(source);
    }

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>() in Foo 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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions