roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

'ref safety' analysis is wrong for collection expressions targeting ref-types that implement IEnumerable.

Open CyrusNajmabadi opened this issue 1 month ago • 1 comments

The compiler hard-codes in that a ref-struct collection expression that implements IEnumerable has 'local scope'. This was hardcoded in because originally this was just an error (it used to not be legal to have a ref-struct implementing an interface).

For example, the following is legal:

ref struct R : IEnumerable
{
    public void Add(int x) { }

    // implementation of IEnumerable...
}

Now that that's legal we need to properly do the analysis here. The analysis of R r = [with(...), x, y, .. z] is complex. Basically, it should be hte intersection of the ref-safety of hte call to the constructor that with(...) defers to, along with the ref-safety of all the .Add calls that are made for .Add(x), .Add(y), foreach (var v in z) .Add(v).

This is challenging as the determination of what .Add methods are called is deferred till lowering.

CyrusNajmabadi avatar Dec 02 '25 19:12 CyrusNajmabadi

I'm a bot. Here are possible related and/or duplicate issues (I may be wrong):

  • https://github.com/dotnet/roslyn/issues/76374
  • https://github.com/dotnet/roslyn/issues/80750

MihuBot avatar Dec 02 '25 19:12 MihuBot