-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Conversation
Looking for early feedback if people want to have this done something differently, before I start changing provider code. |
There was a problem hiding this 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!, |
There was a problem hiding this comment.
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?)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But LazyInitializer is not mine.
https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.ExpressionVisitors.cs
Show resolved
Hide resolved
src/EFCore/Query/Internal/NavigationExpandingExpressionVisitor.Expressions.cs
Show resolved
Hide resolved
5d860d4
to
7bdb942
Compare
7bdb942
to
dc41dc2
Compare
Changing the world one file at time!
Part of #19007