Skip to content

Annotate ObjectPool with nullable #22823

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

Merged
2 commits merged into from
Jun 16, 2020
Merged

Annotate ObjectPool with nullable #22823

2 commits merged into from
Jun 16, 2020

Conversation

pranavkm
Copy link
Contributor

@pranavkm pranavkm commented Jun 11, 2020

Contributes to #5680

@pranavkm pranavkm force-pushed the prkrishn/nullable-objectpool branch from 49a9bf2 to 42963fb Compare June 11, 2020 16:31
@pranavkm pranavkm marked this pull request as ready for review June 11, 2020 18:35
@pranavkm pranavkm requested review from JunTaoLuo and JamesNK June 11, 2020 18:41
@pranavkm pranavkm added the api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews label Jun 11, 2020
@ghost
Copy link

ghost commented Jun 11, 2020

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.

@@ -13,6 +13,7 @@ public partial class DefaultObjectPool<T> : Microsoft.Extensions.ObjectPool.Obje
{
public DefaultObjectPool(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy) { }
public DefaultObjectPool(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy, int maximumRetained) { }
[return: System.Diagnostics.CodeAnalysis.NotNullAttribute]
public override T Get() { throw null; }
public override void Return(T obj) { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can null be passed to Return? If T is not null everywhere then the notnull generic constraint could be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

T already has a class constraint. The compiler requires that both notnull and class to be the first generic constraint so you're unable to apply both simultaneously. That said, there aren't any null checks here. You could return a null value, but that would likely corrupt the pool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I discovered, T : class indicates that T is non-null. This removes the need to specify a NotNullAttribute, and we get the right behavior

@@ -13,6 +13,7 @@ public partial class DefaultObjectPool<T> : Microsoft.Extensions.ObjectPool.Obje
{
public DefaultObjectPool(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy) { }
public DefaultObjectPool(Microsoft.Extensions.ObjectPool.IPooledObjectPolicy<T> policy, int maximumRetained) { }
[return: System.Diagnostics.CodeAnalysis.NotNullAttribute]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should NotNull also be on the base class? Would it ever make sense to return null from ObjectPool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already on the base class too.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see it -

public abstract partial class ObjectPool<T> where T : class
{
protected ObjectPool() { }
public abstract T Get();
public abstract void Return(T obj);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I think I meant to put this on the base type.

@davidfowl
Copy link
Member

Are all the other critical APIs done?

@Pilchie Pilchie added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Jun 12, 2020
@ghost
Copy link

ghost commented Jun 12, 2020

Hello @pranavkm!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@pranavkm pranavkm linked an issue Jun 14, 2020 that may be closed by this pull request
@pranavkm
Copy link
Contributor Author

@JamesNK could I sign off on this?

@ghost ghost merged commit b6bde6c into master Jun 16, 2020
@ghost ghost deleted the prkrishn/nullable-objectpool branch June 16, 2020 19:43
@JamesNK
Copy link
Member

JamesNK commented Jun 16, 2020

Double approved for extra effectiveness.

@pranavkm pranavkm removed the api-ready-for-review API is ready for formal API review - https://github.com/dotnet/apireviews label Mar 22, 2021
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Update ASP.NET Core to use C# 8's nullable reference types
4 participants