[Version 13.0] Feature support for ref and unsafe in iterators and async - #1781
Draft
RexJaeschke wants to merge 3 commits into
Draft
[Version 13.0] Feature support for ref and unsafe in iterators and async#1781RexJaeschke wants to merge 3 commits into
RexJaeschke wants to merge 3 commits into
Conversation
RexJaeschke
marked this pull request as draft
August 13, 2026 10:48
| If *ref_kind* is `ref readonly`, the *identifier*s being declared are references to variables that are treated as read-only. Otherwise, if *ref_kind* is `ref`, the *identifier*s being declared are references to variables that shall be writable. | ||
|
|
||
| It is a compile-time error to declare a ref local variable, or a variable of a `ref struct` type, within a method declared with the *method_modifier* `async`, or within an iterator ([§15.15](classes.md#1515-synchronous-and-asynchronous-iterators)). | ||
| It is a compile-time error to declare and use (even implicitly in compiler-synthesized code) a ref local variable, or a variable of a `ref struct` type across `await` expressions or `yield return` statements. More precisely, the error is driven by the following mechanism: after an `await` expression or a `yield return` statement, all ref local variables and variables of a `ref struct` type in scope are considered definitely unassigned. |
Contributor
There was a problem hiding this comment.
OK, that means it is permissible to read a ref local variable in unreachable code.
using System.Collections.Generic;
public class C {
public IEnumerator<int> M() {
int i = 0;
ref int r = ref i;
yield return r; // OK
// r is definitely unassigned, because of the yield return above.
// yield return r; // error
yield break;
// r is again definitely assigned, because this point is unreachable.
yield return r; // OK
}
}| If *ref_kind* is `ref readonly`, the *identifier*s being declared are references to variables that are treated as read-only. Otherwise, if *ref_kind* is `ref`, the *identifier*s being declared are references to variables that shall be writable. | ||
|
|
||
| It is a compile-time error to declare a ref local variable, or a variable of a `ref struct` type, within a method declared with the *method_modifier* `async`, or within an iterator ([§15.15](classes.md#1515-synchronous-and-asynchronous-iterators)). | ||
| It is a compile-time error to declare and use (even implicitly in compiler-synthesized code) a ref local variable, or a variable of a `ref struct` type across `await` expressions or `yield return` statements. More precisely, the error is driven by the following mechanism: after an `await` expression or a `yield return` statement, all ref local variables and variables of a `ref struct` type in scope are considered definitely unassigned. |
Contributor
There was a problem hiding this comment.
It is not clear to me that this disallows the following:
using System.Threading.Tasks;
public class C {
public static async Task M(Task<int> t) {
int i = 0;
ref int r = ref i;
K1(ref r, await t); // error
}
private static void K1(ref int r, int i) {}
}One could claim that ref r is evaluated before the await expression and r is still definitely assigned at that point. So this depends on the "compiler-synthesized code" for await expressions but I'm not sure that is specified rigorously enough to say whether the above is allowed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is Rex's adaptation of the corresponding MS proposal.