Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pattern based async disposal does not work on lifted nullable structs #34701

Open
chsienki opened this issue Apr 2, 2019 · 1 comment
Open

Comments

@chsienki
Copy link
Contributor

chsienki commented Apr 2, 2019

When implementing async disposal via pattern, you should be able to use a nullable version of the struct in the await using statement:

Repro:

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

namespace System
{
    public interface IAsyncDisposable
    {
        ValueTask DisposeAsync();
    }
}

public class C 
{
    public static async Task M()
    {
        StructDisposer? a = null;
        await using (a) { } // DisposeAsync is not invoked
        
        StructDisposer? b = new StructDisposer();
        await using (b) { } // DisposeAsync is invoked
    }
}

public struct StructDisposer /*: IAsyncDisposable*/
{
    public async ValueTask DisposeAsync() => Console.WriteLine("DisposeAsync");
}

Expected Behaviour:

Compiles successfully.

Actual Behavior:

error CS8410: 'StructDisposer?': type used in an async using statement must be implicitly convertible to 'System.IAsyncDisposable' or implement a suitable 'DisposeAsync' method.

Workaround:

Explicitly implement the IAsyncDisposable interface, and the code will compile and run successfully.

[jcouv update:] this issue is reference in the design doc. Should update the design doc when closing this issue.

@gafter
Copy link
Member

gafter commented Jul 16, 2019

The specification does not support pattern-based disposal of nullable structs. In fact, there is no specification for async using as a feature at all. See dotnet/csharplang#43 and https://github.com/dotnet/csharplang/blob/master/proposals/csharp-8.0/async-streams.md

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

No branches or pull requests

4 participants