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

Annotate query with nullable reference types #23126

Merged
merged 1 commit into from
Oct 30, 2020
Merged

Conversation

smitpatel
Copy link
Contributor

Changing the world one file at time!

Part of #19007

@smitpatel smitpatel marked this pull request as draft October 28, 2020 23:34
@smitpatel smitpatel requested a review from a team October 28, 2020 23:34
@smitpatel
Copy link
Contributor Author

Looking for early feedback if people want to have this done something differently, before I start changing provider code.

Copy link
Member

@roji roji left a comment

Choose a reason for hiding this comment

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

Looks great, not much work for so many files :)

@@ -149,7 +151,8 @@ static Expression CreateMemberAssignment(Expression parameter, MemberInfo member

private ConcurrentDictionary<IEntityType, Func<MaterializationContext, object>> Materializers
=> LazyInitializer.EnsureInitialized(
ref _materializers,
// TODO: Even though we should be able to pass nullable here for some reason it is inferring generic type incorrectly
ref _materializers!,
Copy link
Member

Choose a reason for hiding this comment

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

_materializers can just be be nullable.

BTW I've just annotated NonCapturingLazyInitializer as part of #23133, you can annotate LazyInitializer in the exact same way (do we really need two?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

LazyInitializer comes from above. Make it just nullable infers the generic type as nullable even though the method definition in the source uses ref T?

Copy link
Member

Choose a reason for hiding this comment

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

I think that if in LazyInitializer you annotate TValue without question mark (#23126 (comment)), and you annotate _materializers as nullable here, it should be fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Ah sorry, missed that. In any case, the following does seem to work (with _materializers being nullable):

private ConcurrentDictionary<IEntityType, Func<MaterializationContext, object>> Materializers
    => LazyInitializer.EnsureInitialized(
        ref _materializers,
        () => new ConcurrentDictionary<IEntityType, Func<MaterializationContext, object>>())!;

So there's a bang at the end of the expression (because the compiler doesn't know EnsureInitialized sets to non-null), but the annotation of _materializers is more correct, i.e. in case someone accesses it directly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@roji volunteered to follow-up with runtime team why there is mismatch here.
Specifying generic type arg to be non-null does not fix issue either.
But putting bang as in code or in above example make it work.

Copy link
Member

Choose a reason for hiding this comment

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

The actual reason is simply that we're currently targeting netstandard2.1, where LazyInitializer isn't yet null-annotated. In net5.0 it is and the bang is no longer necessary.

Will add a TODO-NULLABLE as a reminder for this.

@smitpatel smitpatel marked this pull request as ready for review October 29, 2020 21:08
@smitpatel smitpatel requested a review from a team October 29, 2020 21:08
@smitpatel smitpatel merged commit 4699660 into main Oct 30, 2020
@smitpatel smitpatel deleted the smit/nullablequery branch October 30, 2020 07:29
@roji roji linked an issue Oct 30, 2020 that may be closed by this pull request
23 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Annotate EF Core for nullable reference types
2 participants