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

Awaiter GetResult returning by reference #1152

Open
KalleOlaviNiemitalo opened this issue Jul 19, 2024 · 0 comments
Open

Awaiter GetResult returning by reference #1152

KalleOlaviNiemitalo opened this issue Jul 19, 2024 · 0 comments

Comments

@KalleOlaviNiemitalo
Copy link
Contributor

KalleOlaviNiemitalo commented Jul 19, 2024

I was playing with this code in SharpLab:

using System;
public class C {
    public async void M() {
        N(ref await new RT());
    }
    private void N(ref int r) {}
}

public class RT {
    public Awaiter GetAwaiter() {
        return new Awaiter();
    }
    public struct Awaiter : System.Runtime.CompilerServices.INotifyCompletion {
        private static int k;
        public bool IsCompleted => true;
        public void OnCompleted(Action action) {
            action();
        }
        public ref int GetResult() {
            return ref k;
        }
    }
}

The ref await syntax causes an error:

error CS1510: A ref or out value must be an assignable variable

That seems to be covered by the existing wording:

12.9.8.3 Classification of await expressions

The expression await t is classified the same way as the expression (t).GetAwaiter().GetResult(). Thus, if the return type of GetResult is void, the await_expression is classified as nothing. If it has a non-void return type T, the await_expression is classified as a value of type T.

GetResult here returns int by reference, but the await_expression is classified as a value. So the compiler works as specified. Still, it might be good to add a note about this case.

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

No branches or pull requests

1 participant