Skip to content

Instantly share code, notes, and snippets.

@jaredpar
Created November 5, 2019 00:27
Show Gist options
  • Save jaredpar/de522f659963bf50e0fd9f24d3b00768 to your computer and use it in GitHub Desktop.
Save jaredpar/de522f659963bf50e0fd9f24d3b00768 to your computer and use it in GitHub Desktop.
Evil async and await C# code
using System;
using System.Runtime.CompilerServices;
public class C {
async async await(await async) => await async;
}
[AsyncMethodBuilder(typeof(builder))]
class async {
public awaiter GetAwaiter() => throw null;
}
class await {
public awaiter GetAwaiter() => throw null;
}
class awaiter : INotifyCompletion {
public bool IsCompleted => true;
public void GetResult() { }
public void OnCompleted(Action continuation) { }
}
class builder
{
public builder() { }
public static builder Create() => throw null;
public void SetResult() { }
public void SetException(Exception e) { }
public void Start<TStateMachine>(ref TStateMachine stateMachine)
where TStateMachine : IAsyncStateMachine => throw null;
public async Task => throw null;
public void AwaitOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : INotifyCompletion
where TStateMachine : IAsyncStateMachine => throw null;
public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(
ref TAwaiter awaiter, ref TStateMachine stateMachine)
where TAwaiter : ICriticalNotifyCompletion
where TStateMachine : IAsyncStateMachine => throw null;
public void SetStateMachine(IAsyncStateMachine stateMachine) => throw null;
}
@fedeAlterio
Copy link

We can add more await!

using System.Runtime.CompilerServices;

public class C
{
    async async await(await async) => await await await async;
}

[AsyncMethodBuilder(typeof(builder))]
class async
{
    public awaiter GetAwaiter() => throw null;
}
class await
{
    public awaiter GetAwaiter() => throw null;
}

class awaiter : INotifyCompletion
{
    public bool IsCompleted => true;
    public async GetResult() => null;
    public void OnCompleted(Action continuation) { }
}

class builder
{
    public builder() { }
    public static builder Create() => throw null;
    public void SetResult() { }
    public void SetException(Exception e) { }
    public void Start<TStateMachine>(ref TStateMachine stateMachine)
        where TStateMachine : IAsyncStateMachine => throw null;
    public async Task => throw null;
    public void AwaitOnCompleted<TAwaiter, TStateMachine>(
        ref TAwaiter awaiter, ref TStateMachine stateMachine)
        where TAwaiter : INotifyCompletion
        where TStateMachine : IAsyncStateMachine => throw null;
    public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(
        ref TAwaiter awaiter, ref TStateMachine stateMachine)
        where TAwaiter : ICriticalNotifyCompletion
        where TStateMachine : IAsyncStateMachine => throw null;
    public void SetStateMachine(IAsyncStateMachine stateMachine) => throw null;
}

@jaredpar
Copy link
Author

Indeed, you can get to a point where you can arbitrarily chain await.

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