Open shuebner opened 2 years ago
Related, maybe even manifestation of the same bug: Given the code:
namespace ClassLibrary1ForNuget
{
public class Class1
{
private void MethodName1()
{
Class1? instance = null;
instance.MethodName2();
}
private void MethodName2()
{
}
}
}
Assuming I have instance of IdentifierNameSyntax called "name" representing the "instance" local variable:
semanticModel.GetSymbolInfo(name).Symbol
returns ILocalSymbol with the the type properly annotated with nullable annotation
semanticModel.GetTypeInfo(name).Type
returns INamedTypeSymbol which represents the ClassLibrary1ForNuget.Class1 type, but it is not annotated with nullable annotation
I believe TypeInfo should properly carry the nullable annotation
I can reproduce the bug. I currently use the following code to work around it:
private static NullableFlowState GetExpressionNullability(SyntaxNodeAnalysisContext context, ExpressionSyntax expression)
{
var flowState = context.SemanticModel.GetTypeInfo(expression).Nullability.FlowState;
if (flowState != NullableFlowState.None)
{
return flowState;
}
// Ideally, `flowState` should have been all we needed. Sadly, Roslyn thinks otherwise... :)
// See: https://github.com/dotnet/roslyn/issues/59875
return context.SemanticModel.GetSymbolInfo(expression).Symbol switch
{
IParameterSymbol s => AnnotationToFlowState(s.NullableAnnotation),
ILocalSymbol s => AnnotationToFlowState(s.NullableAnnotation),
IPropertySymbol s => AnnotationToFlowState(s.NullableAnnotation),
IFieldSymbol s => AnnotationToFlowState(s.NullableAnnotation),
_ => NullableFlowState.None,
};
static NullableFlowState AnnotationToFlowState(NullableAnnotation annotation) => annotation switch
{
NullableAnnotation.NotAnnotated => NullableFlowState.NotNull,
NullableAnnotation.Annotated => NullableFlowState.MaybeNull,
_ => NullableFlowState.None,
};
}
Version Used: SDK 6.0.200 Microsoft.CodeAnalysis.CSharp.Workspaces 3.11
Steps to Reproduce:
This is not a truly minimal example, but the smallest use-case with which I can readily confirm the behavior:
Expected Behavior: TypeInfo of
either
reports nullability as Annotation Annotated and FlowState NotNull in both cases. TypeInfo ofeither
reports nullability consistently with its Symbol.Actual Behavior: TypeInfo of
either
reports nullability differently wheneither
is deconstructed. TypeInfo ofeither
reports nullability differently from SymbolInfo.