Skip to content

System.Memory: C# 14 new[] changes cause breaking changes to SequenceEqual #268

@mgravell

Description

@mgravell

The span new[] usage in C# 14 can cause overload-resolution changes that have catastrophic breaking changes, as an example when using MemoryExtensions.SequenceEqual on down-level .NET Framework - which (in the released binaries) does not have full support for null values in the ReadOnlySpan<T> overload, but does have null support in the more common overloads.

This issue has already been fixed in source (thanks @ViveliDuCh), but because no new release has occurred, unexpected breaks can occur when updating <LangVersion> (typically in multi-targeting projects)

Request: can haz new bits?

Repro example:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <!-- fine on modern .NET; fails on down-level -->
    <TargetFrameworks>net472;net10.0</TargetFrameworks>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <!-- works with 13 or below, fails with 14 or higher -->
    <LangVersion>14.0</LangVersion>
  </PropertyGroup>
  <ItemGroup>
    <!-- latest at time of writing -->
    <PackageReference Include="System.Memory" Version="4.6.3" />
  </ItemGroup>
</Project>

with

string?[] arr = ["abc", null, "def"];

IList<string?> list = arr;
// this is not impacted, as no overload change
var x = list.SequenceEqual(new[] {"abc", null, "def"});
Console.WriteLine($"List: {x}");

// this new[] here is the bit that makes a difference
var y = arr.SequenceEqual(new[] {"abc", null, "def"});
Console.WriteLine($"Array: {y}");

Specifically: with C# 14 interpreting the new[] via spans, this causes the overload resolution to jump to the (ReadOnlySpan<T>, ReadOnlySpan<T>) version, which fails with a NullReferenceException.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions