roslyn: CS9210 regression in .NET 8.0.200 SDK

Version Used: .NET 8.0.200

Steps to Reproduce:

  1. Create a .NET 6.0 or .NET 7.0 project.
  2. Define a local ImmutableCollectionsMarshal.AsImmutableArray() polyfill.
  3. Build.
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

</Project>
using System.Collections.Immutable;
using System.Runtime.CompilerServices;

namespace System.Runtime.InteropServices;

internal static class ImmutableCollectionsMarshal
{
    public static ImmutableArray<T> AsImmutableArray<T>(T[] array) => Unsafe.As<T[], ImmutableArray<T>>(ref array);
}
using System.Collections.Immutable;

ImmutableArray<string> array = ["abc"];

Diagnostic Id: CS9210

Expected Behavior: builds successfully.

Actual Behavior:

Error CS9210 This version of 'ImmutableArray' cannot be used with collection expressions.

This is a serious regression: this worked fine on .NET 8.0.101/8.0.102 but is now broken.

This regression breaks OpenIddict (that uses an ImmutableCollectionsMarshal polyfil to use the collection expressions syntax with ImmutableArray<T> on older TFMs) very badly: https://github.com/openiddict/openiddict-core/actions/runs/7963331151/job/21738679005?pr=1988

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20 (10 by maintainers)

Most upvoted comments

@juharris .NET 8.0’s ImmutableArray version is decorated with [CollectionBuilder] so it indeed works… but this thread was about using that syntax with older versions, that are not decorated with [CollectionBuilder].

We don’t go out of our way just to break. However, if the compiler is out of spec with the language, we very much do break in a short interim period right after shipping as we discover bugs.

Indeed that is the case. The year after we ship a feature we’re free to break new features in order to fix bugs, get in compliance with the intended behavior or correct design issues that customers find when using the feature in the wild. We very much try to avoid breaks whenever possible but they do happen from time to time and are often an unavoidable consequence.

That first year also exists on a sliding scale:

  • Months 0-3: we have no qualms changing behavior. Feature is brand new, there will be bugs and we have to address them.
  • Months 3-6: similar to Months 0-3 but we usually push back harder on taking the change because we realize there will be greater customer adoption hence greater impact. At the same time though, if it’s a meaningful bug we’ll generally push forward with a fix.
  • Months 6-9: we will still break behavior but there needs to be a compelling reason to do so. The bug needs to be significant. The more core the scenario the more significant the bug needs to be.
  • Months 9-12: it needs to be a very compelling bug for us to break scenarios at this point because adoption is likely quite significant. Taking breaks at this window in a core scenario is exceedingly rare.

This is the policy we’ve operated on since the start of C#. Even then these breaks tend to impact a small number of customers because we go to great lengths to ensure breaks are targeted and minimal.

This requires mixing TFMs/package versions (e.g .NET 6.0 TFM + .NET 8.0 NuGet package), which isn’t ideal (e.g it has implications regarding support policies).

Using collection expressions on net6.0 is already in not officially supported territory. If it works that’s great but if it doesn’t we’re not going out of our way to fix it because it’s unsupported. I understand a number of customers get frustrated with this policy but it is the policy that we operate on.

For this case in particular ImmutableArray<T> is not a type recognized by the compiler. It’s no different than any other 3rd party collection type. The versions prior to 8.0 don’t fully meet the requirements necessary for use in collection expressions hence there will be friction. The advice to customers, for ImmutableArray<T> and any other collection type, is that if you want to have versions that work well with collection expressions then you’ll need to update to versions of the libraries that fully support the necessary patterns.

Closing this as “By Design”. The compiler is in compliance with the spec here and there aren’t plans at the moment to change the spec to loosen these requirements.