roslyn: CS9210 regression in .NET 8.0.200 SDK
Version Used: .NET 8.0.200
Steps to Reproduce:
- Create a .NET 6.0 or .NET 7.0 project.
- Define a local
ImmutableCollectionsMarshal.AsImmutableArray()polyfill. - 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)
@juharris .NET 8.0’s
ImmutableArrayversion 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].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:
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.
Using collection expressions on
net6.0is 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, forImmutableArray<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.