Skip to content

ReadOnlyMemory<T> rewrites to ReadOnlySpan<T> across yield #113

@PetarIsakovic

Description

@PetarIsakovic

[Zomp.SyncMethodGenerator.CreateSyncVersion] generates invalid sync code for an async iterator method that uses ReadOnlyMemory<T> and yield return.

The generated method rewrites a ReadOnlyMemory<T> local into ReadOnlySpan<T> inside an iterator and emits assignments that don’t type-check.

Minimal repro

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;

public static partial class Repo
{
    [Zomp.SyncMethodGenerator.CreateSyncVersion]
    public static async IAsyncEnumerable<int> Foo(IAsyncEnumerable<ReadOnlyMemory<bool>> input, [EnumeratorCancellation] CancellationToken ct = default)
    {
        ReadOnlyMemory<bool> prev = default;
        var hasPrev = false;

        await foreach (var col in input.WithCancellation(ct))
        {
            if (hasPrev)
            {
                Helper(prev, col);
            }

            prev = col;
            hasPrev = true;

            yield return 1;
        }
    }

    private static void Helper(ReadOnlyMemory<bool> a, ReadOnlyMemory<bool> b) { }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions