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

Unsafe.NullRef<T> and IsNullRef<T> are missing from the public APIs #38785

Closed
Sergio0694 opened this issue Jul 4, 2020 · 6 comments
Closed
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged New issue has not been triaged by the area owner

Comments

@Sergio0694
Copy link
Contributor

Summary

There are two new Unsafe APIs that have been added to CoreCLR recently: Unsafe.NullRef<T> and Unsafe.IsNullRef<T>, but they're not publicly available (tested this both on just .NET 5 Preview 6, and from the System.Runtime.CompilerServices.Unsafe packages, latest version as well). The comments say this file should be kept in sync with the public Unsafe.il file with the APIs that are available outside of CoreCLR, but these two are currently missing. Is this just an oversight or was it on purpose?

These are the two APIs in question:

/// <summary>
/// Returns a by-ref to type <typeparamref name="T"/> that is a null reference.
/// </summary>
[Intrinsic]
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ref T NullRef<T>()
{
return ref Unsafe.AsRef<T>(null);
// ldc.i4.0
// conv.u
// ret
}

/// <summary>
/// Returns if a given by-ref to type <typeparamref name="T"/> is a null reference.
/// </summary>
/// <remarks>
/// This check is conceptually similar to "(void*)(&amp;source) == nullptr".
/// </remarks>
[Intrinsic]
[NonVersionable]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullRef<T>(ref T source)
{
return Unsafe.AsPointer(ref source) == null;
// ldarg.0
// ldc.i4.0
// conv.u
// ceq
// ret
}

They'd extremely useful to have, especially because working around them is quite verbose (in particular IsNullRef), and most importantly because it forces you to have an unsafe block, since you have to go through a pointer to implement these two.

Having these two publicly available would be super handy! 😄

@Sergio0694 Sergio0694 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jul 4, 2020
@Dotnet-GitSync-Bot
Copy link
Collaborator

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Jul 4, 2020
@stephentoub
Copy link
Member

stephentoub commented Jul 4, 2020

The implementation in corelib is for internal use only in corelib. It's often augmented first, as it doesn't have the same bar as for public API. Exposing these publicly is tracked by #31170. Thanks.

@Sergio0694
Copy link
Contributor Author

Oof, my bad, completely missed that duplicate issue. Thanks! 😊

@stephentoub
Copy link
Member

No worries :)

@jkotas
Copy link
Member

jkotas commented Jul 28, 2020

API proposal #31170 was approved. @Sergio0694 Are you interested in submitting PR with the implementation?

@Sergio0694
Copy link
Contributor Author

Sergio0694 commented Jul 28, 2020

@jkotas Absolutely, I'd be happy to! 😊

EDIT: see #40008.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 8, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

4 participants