Update null state for add/remove on field-like events#46477
Update null state for add/remove on field-like events#46477RikkiGibson merged 5 commits intodotnet:masterfrom
Conversation
|
Please take a look @dotnet/roslyn-compiler |
1 similar comment
|
Please take a look @dotnet/roslyn-compiler |
|
Please review @dotnet/roslyn-compiler |
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
Outdated
Show resolved
Hide resolved
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
Show resolved
Hide resolved
| { | ||
| if (ResultType.IsNotNull) | ||
| { | ||
| LearnFromNonNullTest(MakeMemberSlot(receiverOpt, @event), ref State); |
There was a problem hiding this comment.
MakeMemberSlot(receiverOpt, @event) [](start = 45, length = 35)
Does LearnFromNonNullTest() handle an invalid slot? For instance, F().E += () => { };
There was a problem hiding this comment.
It seems like no, not really, and we don't have any debug asserts preventing e.g. this.State[-1] = .... When we write it through to the BitVector, it ends up getting bit-shifted into something arbitrary and undesirable.
There was a problem hiding this comment.
have made the change to only try and update state if we have a valid member slot.
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
Show resolved
Hide resolved
| } | ||
| else | ||
| { | ||
| this.State[memberSlot] = NullableFlowState.MaybeNull; |
There was a problem hiding this comment.
I decided to change these to more direct state modifications because event assignment is more limited in what it can operate on. For example the LHS of an event assignment must be an lvalue, so it can't be a conditional-access expression like x?.y += SomeDelegate.
If this feels wrong from a coding patterns POV we can change this to use the LearnFromNullTest/LearnFromNonNullTest or some other more appropriate helpers that I may have overlooked in NullableWalker.
| { | ||
| if (node.IsAddition) | ||
| { | ||
| this.State[memberSlot] = this.State[memberSlot].Meet(ResultType.State); |
There was a problem hiding this comment.
I believe we should using SetResult to set these, rather than updating the state directly like this. Please add some tests that observe the change in field-like event's nullability from the public API.
There was a problem hiding this comment.
Discussed offline, and the difficulty is that it's not clear what bound node would be passed to SetResult. Will add the test.
src/Compilers/CSharp/Test/Semantic/Semantics/NullableReferenceTypesTests.cs
Show resolved
Hide resolved
| VisitRvalue(node.Argument); | ||
| // https://github.com/dotnet/roslyn/issues/31018: Check for delegate mismatch. | ||
| if (node.Argument.ConstantValue?.IsNull != true | ||
| && MakeMemberSlot(receiverOpt, @event) is not -1 and var memberSlot) |
There was a problem hiding this comment.
is not -1 [](start = 55, length = 9)
Consider using is > 0 for consistency with other checks.
| else | ||
| { | ||
| this.State[memberSlot] = NullableFlowState.MaybeNull; | ||
| } |
There was a problem hiding this comment.
Perhaps use conditional: this.State[memberSlot] = node.IsAddition ? ... : ...;
|
|
||
| var lhs = root.DescendantNodes().OfType<AssignmentExpressionSyntax>().Single().Left; | ||
| typeInfo = model.GetTypeInfo(lhs); | ||
| Assert.Equal(PublicNullableAnnotation.None, typeInfo.Type.NullableAnnotation); |
There was a problem hiding this comment.
This seems like a bug, can you file to follow up?
|
I have the required sign offs and the CI failure is due to a known flaky test, so I will just merge the PR. |
…features/function-pointers * upstream/master: (465 commits) Stop specifying Rich Nav's version of dotnet Fix nullable reference warning Fix up some calls to SymbolKey.Resolve now that it's annotated Remove try/catch of ArgumentException Null annotate SymbolKey Annotate Location.IsInSource as ensuring SourceTree is non-null Fix nullable annotation of Compilation.CreateErrorTypeSymbol Update null state for add/remove on field-like events (#46477) Throw IndexOutOfRangeException from BitVector.this[int] if index < 0 (#46627) Delete some dead support for changing legacy options Delete our use of Microsoft.VisualStudio.CodingConventions Fix crash in the preview of a code action that modified an .editorconfig Move null check above where the variable is used (#46558) Add integration test to configure diagnostic severity via editorconfig Disable a few Mac tests Consider nullability in conversion to constraint type (#46405) Added interpolated strings and tests Introduce request context (#46557) Simplify NativeIntegerAttribute encoding (#46522) Update extension getenumerator status. (#46607) ...
Fixes #42557
This work supports the new nullable constructor analysis, since we need to be able to tell that an event no longer contains 'null' after a non-null delegate is added to it.
🦔