Skip to content

Public API for runtime async #79818

@333fred

Description

@333fred

Background and Motivation

Runtime async introduces a new lowering for async methods, which can have one of two patterns:

  1. await expr has a directly supported type, and we just emit:
System.Runtime.CompilerServices.AsyncHelpers.Await(expr);
  1. await expr has a not supported type, and we emit:
var awaiter = expr.GetAwaiterMethod();
if (!awaiter.IsCompletedProperty)
{
    System.Runtime.CompilerServices.AwaitAwaiter(awaiter);
    // or
    System.Runtime.CompilerServices.UnsafeAwaitAwaiter(awaiter);
}
awaiter.GetResultMethod()

Consumers of AwaitExpressionInfo, such as the preview features analyzer, will need to be able to see this new method symbol and take action on it.

Proposed API

namespace Microsoft.CodeAnalysis.CSharp;

public readonly struct AwaitExpressionInfo
{
    public IMethodSymbol? GetAwaiterMethod { get; }

    public IPropertySymbol? IsCompletedProperty { get; }

    public IMethodSymbol? GetResultMethod { get; }

    public bool IsDynamic { get; }

+    /// <summary>
+    /// When runtime async is enabled for this await expression, this represents either:
+    /// <list type="bullet">
+    /// <item>
+    /// A call to <c>System.Runtime.CompilerServices.AsyncHelpers.Await</c>, if this is a
+    /// supported task type. In such cases, <see cref="GetAwaiterMethod" />,
+    /// <see cref="IsCompletedProperty" />, and <see cref="GetResultMethod" /> will be
+    /// <see langword="null" />.
+    /// </item>
+    /// <item>
+    /// A call to <c>System.Runtime.CompilerServices.AsyncHelpers.AwaitAwaiter|UnsafeAwaitAwaiter</c>.
+    /// In these cases, the other properties may be non-<see langword="null" /> if the
+    /// the rest of the await expression is successfully bound.
+    /// </item>
+    /// </list>
+    /// </summary>
+    public IMethodSymbol? RuntimeAwaitMethod { get; }
}

Usage Examples

var awaitInfo = semanticModel.GetAwaitExpressionInfo(expr);
if (awaitInfo.RuntimeAwaitMethod is IMethodSymbol)
{
    // Ensure that `EnablePreviewFeatures` is on.
}

Alternative Designs

Risks

Metadata

Metadata

Assignees

Labels

Area-CompilersConcept-APIThis issue involves adding, removing, clarification, or modification of an API.Feature - Runtime AsyncFeature Requestapi-approvedAPI was approved in API review, it can be implementedblockingAPI needs to reviewed with priority to unblock work

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions