Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Span<T> == null throws ArgumentNullException #20457

Closed
felipepessoto opened this issue Mar 5, 2017 · 7 comments · Fixed by dotnet/corefx#25257
Closed

Span<T> == null throws ArgumentNullException #20457

felipepessoto opened this issue Mar 5, 2017 · 7 comments · Fixed by dotnet/corefx#25257
Labels
area-System.Memory bug help wanted [up-for-grabs] Good issue for external contributors
Milestone

Comments

@felipepessoto
Copy link
Contributor

I know it doesn't make sense to compare it to null since it is a struct. But it is allowed since we have a implicit cast operator that get called, op_Implicit(T[] array).

And that operator throws the exceptions when calling the constructor:

    public static implicit operator Span<T>(T[] array)
    {
        return new Span<T>(array);
    }

    public Span(T[] array)
    {
        if (array == null)
        {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
        }

Repro:

    int[] src = { 1, 2, 3 };
    Span<int> srcSpan = new Span<int>(src);
    var t = srcSpan == null;

at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Span`1.op_Implicit(T[] array)
at ConsoleApp1.Program.Main(String[] args)

@KrzysztofCwalina
Copy link
Member

@jaredpar, this is a bit strange. Is there anything we can do about it?

@jaredpar
Copy link
Member

jaredpar commented Mar 9, 2017

CC @gafter who can discuss the best pattern to use here.

@KrzysztofCwalina
Copy link
Member

@gafter, any pointers?

@ahsonkhan
Copy link
Contributor

ping @gafter

@gafter
Copy link
Member

gafter commented Oct 30, 2017

I suggest adding

    [Obsolete("Incorrect equality test; Span<T> is a value type", true)]
    public static bool operator ==(Span<T> left, T[] right) => true;
    [Obsolete("Incorrect equality test; Span<T> is a value type", true)]
    public static bool operator !=(Span<T> left, T[] right) => false;

@ahsonkhan
Copy link
Contributor

This PR fixes the issue (by adding a null check to the implicit cast): dotnet/corefx#25257

I suggest adding

Should we still add those equality overloads?

@gafter
Copy link
Member

gafter commented Nov 15, 2017

Should we still add those equality overloads?

I don't think so. However, it is slightly strange that you can convert a null T[] to a Span<T> by the conversion but not by construction.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Memory bug help wanted [up-for-grabs] Good issue for external contributors
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants