Skip to content

Bad diagnostic for ref struct using as awaiter #75025

Open
@huoyaoyuan

Description

@huoyaoyuan

Version Used: VS17.11.2, LangVersion preview

Steps to Reproduce:

    public ref struct RefAwaitable
    {
        public ref struct Awaiter : ICriticalNotifyCompletion, INotifyCompletion
        {
            public void UnsafeOnCompleted(Action continuation) => ThreadPool.UnsafeQueueUserWorkItem(_ => continuation(), null);
            public void OnCompleted(Action continuation) => ThreadPool.UnsafeQueueUserWorkItem(_ => continuation(), null);

            public bool IsCompleted => false;

            public int GetResult() => 114514;
        }

        public Awaiter GetAwaiter() => default;
    }

    internal class Program
    {
        static async Task Main(string[] args)
        {
            RefAwaitable r = default;
            Console.WriteLine(await r);
        }
    }

The code snippets errors for "RefAwaitable.Awaiter doesn't implement INotifyCompletion", though it can implement from C# 13. Since awaiter needs to be saved in state machine, it can't be ref struct.
Also, the ECMA C# 7.x spec doesn't call out that awaiter can't be ref struct. The spec may need to be updated together.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions