roslyn: bug either in Roslyn or in the C# language spec — example with `ReadOnlySpan` in a `foreach` expansion

This is an issue I originally reported in the C# lang. spec repo. My original report was for the C# specification, as my assumption was that the specified foreach expansion was inaccurate. Consider the program below, which is compiled without errors.

public class C
{
    public async Task M()
    {
        IReadOnlyList<string> o = null;
        foreach (ReadOnlySpan<char> c in o) {}        
        return;
    }
}

Expanding its foreach as according to the specification introduces a diagnostic:

error CS4012: Parameters or locals of type ‘ReadOnlySpan<char>’ cannot be declared in async methods or async lambda expressions.

But it turns out that, according to @CyrusNajmabadi and @333fred , the real issue is that my original program is ilegal, even accepted by Roslyn. So I was told to open an issue here.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (9 by maintainers)

Commits related to this issue

Most upvoted comments

prevent people from accidentally expanding things differently than how you’re expanding htings

@CyrusNajmabadi , how I’m (or anyone else is) expanding it is circumstantial—the expansion is governed by the language specification. But here it is, as you ask:

public class C
{
    public async Task M()
    {
        IReadOnlyList<string> o = null;
        {
              var e_L10C8 = ((IEnumerable<string>)o).GetEnumerator();
              while (e_L10C8.MoveNext())
              {
                  ReadOnlySpan<char> c =(ReadOnlySpan<char>)(string)e_L10C8.Current;
                  {}
              }
        }
        return;
    }
}

Note: The _L10C8 suffix is just a convention to indicate that the (original) expression occurs at Line 10 and Column 8.