-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Microsoft.CodeAnalysis appears to report false FlowState, when a switch handles null #57042
Comments
@hugener Could you provide a more contained repro, for example a code snippet on sharplab or some such? |
@jcouv Since the source code above has changed, the line numbers I posted no longer match up. I created a branch so the code will not change as I make other changes and extracted the test here: https://github.com/sundews/Sundew.DiscriminatedUnions/blob/feature/RoslynNullabilityProblem/Source/Sundew.DiscriminatedUnions.Test/ContradictingTests.cs#L69-L125 I'm using the compiler API semanticModel.GetTypeInfo here: Here is a screenshot of that: I use the same code for various other cases, but in this particular test, an instance is returned by the method "Compute" and the return type is a non-nullable Option (nullable is enabled). I think this is somehow significant because I have another test here, where there instance is passed in as a parameter. And that test works: https://github.com/sundews/Sundew.DiscriminatedUnions/blob/feature/RoslynNullabilityProblem/Source/Sundew.DiscriminatedUnions.Test/ContradictingTests.cs#L19-L67 The only difference in the two tests is that the value is coming from the Compute method vs being passed in as a parameter. Not sure if it's related, but I noticed something similar in VS when the value comes from a method. As I hover the "result" here, it's reporting it as nullable. Again, nullable is enabled and the return value of Compute is non-nullable: |
I have put a snippet on sharplab here. It's the same two examples I use in my contradicting tests above. That's not the case with the option passed in as a parameter |
Thanks for the additional information. I think I see what's going on. The part that may be surprising is that arguably the effect of the pure null test should be after the
|
Thanks for confirming what's going on. That also explains why it conflicts with my intention as I'm writing an analyzer that should report an error about unreachable null cases. Come to think about it, I guess pure null tests are necessary because of null! |
Thanks for confirming. |
Any updates on this? |
@hugener We've placed this into to Backlog and I do not expect we'll get to it given the priority of other issues. The impact of this issue is relatively limited: the compilation produces correct diagnostics and codegen, but the information reported by the semantic model to the IDE has incorrect nullability. |
Version Used:
Microsoft Visual Studio Enterprise 2019 Version 16.11.3
Microsoft.CodeAnalysis Version 3.11.0
Steps to Reproduce:
Solution that shows the problem in VS:
https://github.com/dotnet/roslyn/files/7303368/DUTester.zip
Test that reproduces it when running the analyzer:
https://github.com/sundews/Sundew.DiscriminatedUnions/blob/main/Source/Sundew.DiscriminatedUnions.Test/GenericSwitchExpressionAnalyzerTests.cs
Given_SwitchExpressionInEnabledNullableContext_When_ValueIsNotNullAndAllCasesAndNullCaseAreHandled_Then_HasUnreachableNullCaseIsReported2
In the following file at line 273: TypeInfo.Nullability.FlowState reports MaybeNull. Removing the null case in the test above changes the FlowState to NotNull.
https://github.com/sundews/Sundew.DiscriminatedUnions/blob/main/Source/Sundew.DiscriminatedUnions.Analyzer/DiscriminatedUnionSwitchAnalyzer.cs
Expected Behavior:
DUTester solution: A diagnostic reported on line 18, that the null case should not be handled.
Test: TypeInfo.Nullability.FlowState reported as NotNull
Actual Behavior:
DUTester solution: No diagnostic.
Test: TypeInfo.Nullability.FlowState reported as MaybeNull
The text was updated successfully, but these errors were encountered: