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

Incorrect nullable annotations on most MemoryExtensions methods #73457

Closed
stephentoub opened this issue Aug 5, 2022 · 1 comment · Fixed by #73495
Closed

Incorrect nullable annotations on most MemoryExtensions methods #73457

stephentoub opened this issue Aug 5, 2022 · 1 comment · Fixed by #73495
Assignees
Milestone

Comments

@stephentoub
Copy link
Member

stephentoub commented Aug 5, 2022

Most of our MemoryExtensions methods have a shape like:

public static bool Contains<T>(this Span<T> span, T value) where T : IEquatable<T>

where the T is constrained to be IEquatable<T>. The questionable nature of the constraint in general aside (we can use EqualityComparer<T>.Default), the constraint is too strict with regards to nullable reference types. The constraint should be where T : IEquatable<T>? rather than where T : IEquatable<T>; otherwise, it's prohibiting nullable Ts, e.g. this erroneously warns:

Span<string?> objects = new string?[] { "hello", null, "world" };
Console.WriteLine(objects.Contains(null));

with:

warning CS8631: The type 'string?' cannot be used as type parameter 'T' in the generic type or method 'MemoryExtensions.Contains<T>(Span<T>, T)'. Nullability of type argument 'string?' doesn't match constraint type 'System.IEquatable<string?>'.

We need to audit all such constraints and fix them.

cc: @terrajobst, @bartonjs, @jeffhandley

@stephentoub stephentoub added this to the 7.0.0 milestone Aug 5, 2022
@ghost
Copy link

ghost commented Aug 5, 2022

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

Issue Details

Most of our MemoryExtensions methods have a shape like:

public static bool Contains<T>(this Span<T> span, T value) where T : IEquatable<T>

where the T is constrained to be IEquatable<T>. The questionable nature of the constraint in general aside (I don't see a good reason to constrain to IEquatable<T> here), the constraint is too strict with regards to nullable reference types. The constraint should be where T : IEquatable<T>? rather than where T : IEquatable<T>; otherwise, it's prohibiting nullable Ts, e.g. this erroneously warns:

Span<string?> objects = new string?[] { "hello", null, "world" };
Console.WriteLine(objects.Contains(null));

with:

warning CS8631: The type 'string?' cannot be used as type parameter 'T' in the generic type or method 'MemoryExtensions.Contains<T>(Span<T>, T)'. Nullability of type argument 'string?' doesn't match constraint type 'System.IEquatable<string?>'.

We need to audit all such constraints and fix them.

cc: @terrajobst, @bartonjs, @jeffhandley

Author: stephentoub
Assignees: -
Labels:

area-System.Memory

Milestone: 7.0.0

@stephentoub stephentoub added the bug label Aug 5, 2022
@stephentoub stephentoub self-assigned this Aug 5, 2022
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 5, 2022
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 8, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Sep 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant