Skip to content

Wrong height and width of empty spans obtained from Span<T>.AsSpan2D. #1101

@KBoenisch

Description

@KBoenisch

Describe the bug

Two-dimensional spans like Span<T>.Empty.AsSpan2D(n, 0) and Span<T>.Empty.AsSpan2D(0, n) always have Height = 0 and Width = 0. This is caused by the special case https://github.com/CommunityToolkit/dotnet/blob/main/src/CommunityToolkit.HighPerformance/Memory/Span2D%7BT%7D.cs#L534.

Regression

No response

Steps to reproduce

// Examples of unexpected behavior:
Console.WriteLine(Span<bool>.Empty.AsSpan2D(1, 0).Height); // Prints 0.
Console.WriteLine(Span<bool>.Empty.AsSpan2D(0, 1).Width); // Prints 0.

// Examples of expected behavior:
Console.WriteLine(new bool[1, 0].AsSpan2D().Height); // Prints 1.
Console.WriteLine(new bool[0, 1].AsSpan2D().Width); // Prints 1.

Expected behavior

I expect Span<T>.Empty.AsSpan2D(n, 0) to have Height = n and Span<T>.Empty.AsSpan2D(0, n) to have Width = n. This is the behavior for two-dimensional arrays, i.e. new T[n, 0].AsSpan2D() and new T[0, n].AsSpan2D() have the correct widths and heights.

Screenshots

No response

IDE and version

Rider

IDE version

No response

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.4.0

Additional context

No response

Help us help you

No, just wanted to report this

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛An unexpected issue that highlights incorrect behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions