roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

CS8344 error even with C# 13 (ref in iterators/async)

Open ufcpp opened this issue 1 year ago • 1 comments
trafficstars

Version Used:

VS 17.12.0 Preview 1.0 C# Tools 4.12.0-1.24379.11+cf82d399c36008e7936d545cde24141f8d3790fa

Steps to Reproduce:

static async Task M1()
{
    // CS8344.
    foreach (var r in new E1()) ;

    await Task.Yield();

    // No error with C# 13.
    E1 e1 = new();
    while (e1.MoveNext()) { var x = e1.Current; }

    await Task.Yield();

    // No error with C# 13.
    foreach (ref var x in new E2()) ;

    await Task.Yield();

    // No error with C# 13.
    E2 e2 = new();
    while (e2.MoveNext()) { ref var x = ref e2.Current; }
}

static IEnumerable<object?> M2()
{
    // CS8344.
    foreach (var r in new E1()) ;

    yield return null;

    // No error.
    E1 e1 = new();
    while (e1.MoveNext()) { var x = e1.Current; }

    yield return null;

    // No error with C# 13.
    foreach (ref var x in new E2()) ;

    yield return null;

    // No error with C# 13.
    E2 e2 = new();
    while (e2.MoveNext()) { ref var x = ref e2.Current; }
}

ref struct E1
{
    public E1 GetEnumerator() => this;
    public bool MoveNext() => false;
    public int Current => 0;
}

struct E2
{
    public E2 GetEnumerator() => this;
    public bool MoveNext() => false;
    public ref int Current => ref System.Runtime.CompilerServices.Unsafe.NullRef<int>();
}

Expected Behavior:

No error with the ref/unsafe in iterators/async feature.

Actual Behavior:

CS8344 error on the foreach.

I am not sure if this is a bug as it is not on the proposal document, but I would consider this to be a specification omission.

ufcpp avatar Aug 17 '24 04:08 ufcpp