Skip to content
This repository was archived by the owner on Aug 24, 2022. It is now read-only.
This repository was archived by the owner on Aug 24, 2022. It is now read-only.

Async state machine incorrect translation #1041

Description

@iskiselev

We have one more issue with incorrect goto translation in Roslyn compiled state machine. Issue only reproducible within assemblies compiled with optimizations.
Test case:

//@useroslyn
//@compileroption /optimize

using System;
using System.Threading;
using System.Threading.Tasks;

public static class Program
{
    public static void Main(string[] args)
    {
        var signal = new ManualResetEventSlim(false);

        var task = AsyncMethod(signal);

        signal.Wait();
    }

    public static async Task AsyncMethod(ManualResetEventSlim resetEvent)
    {
        await new Test().FailedMethod();
        resetEvent.Set();
    }
}

public class Test
{
    public async Task FailedMethod () {
        using (await GetLockObject()) {
        }

        using (await GetLockObject()) {
            await DoWork();
        }
    }


    private async Task DoWork()
    {
        Console.WriteLine("work");
    }

    public async Task<IDisposable> GetLockObject()
    {
        Console.WriteLine("Lock");
        return new LockObject();
    }
}

public class LockObject : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("Dispose");
    }
}

Method that translated incorrect:

    public async Task FailedMethod () {
        using (await GetLockObject()) {
        }

        using (await GetLockObject()) {
            await DoWork();
        }
    }

Expected output: Lock\nDispose\nLock\nwork\nDispose
Actualoutput: Lock\nwork\nDispose

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions