public void Equality() { var c = CreateCompilation(""); var obj = c.GetSpecialType(SpecialType.System_Object).GetPublicSymbol(); var int32 = c.GetSpecialType(SpecialType.System_Int32).GetPublicSymbol(); var notNullable = new NullabilityInfo(CodeAnalysis.NullableAnnotation.NotAnnotated, CodeAnalysis.NullableFlowState.NotNull); var nullable = new NullabilityInfo(CodeAnalysis.NullableAnnotation.Annotated, CodeAnalysis.NullableFlowState.MaybeNull); EqualityTesting.AssertEqual(default(TypeInfo), default(TypeInfo)); EqualityTesting.AssertEqual(new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), int32, nullable, notNullable), new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), int32, nullable, notNullable)); EqualityTesting.AssertNotEqual(new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), nullable, nullable), new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), int32.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), nullable, nullable)); EqualityTesting.AssertNotEqual(new TypeInfo(int32.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), nullable, nullable), new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), nullable, nullable)); EqualityTesting.AssertNotEqual(new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), int32.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), nullable, nullable), new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.NotAnnotated), int32.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), notNullable, nullable)); EqualityTesting.AssertNotEqual(new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), int32.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), nullable, nullable), new TypeInfo(obj.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.Annotated), int32, nullable, notNullable)); EqualityTesting.AssertEqual(new TypeInfo(int32.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.None), int32.WithNullableAnnotation(CodeAnalysis.NullableAnnotation.None), default, default),
public static string GetMemberNullableDevelopName(NullabilityInfo nullInfo) { var type = nullInfo.Type; if (nullInfo.ReadState != NullabilityState.Nullable) { if (nullInfo.ElementType != null) { return($"{GetMemberNullableDevelopName(nullInfo.ElementType)}[]"); } else if (nullInfo.GenericTypeArguments.Length > 0) { StringBuilder typeStringBuilder = new StringBuilder(); var typeString = (!string.IsNullOrEmpty(type.Namespace) && !string.IsNullOrEmpty(type.FullName)) ? $"{type.Namespace}.{type.Name.Split('`')[0]}" : type.Name.Split('`')[0]; typeStringBuilder.Append($"{typeString}<{GetMemberNullableDevelopName(nullInfo.GenericTypeArguments[0])}"); for (int i = 1; i < nullInfo.GenericTypeArguments.Length; i++) { typeStringBuilder.Append($",{GetMemberNullableDevelopName(nullInfo.GenericTypeArguments[i])}"); } typeStringBuilder.Append('>'); return(typeStringBuilder.ToString()); } return(type.GetDevelopName()); } if (type.IsValueType && type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { return(type.GetDevelopName()); } return(type.GetDevelopName() + "?"); }
public void ConditionalOperator_InvalidType() { var source = @" class C { void M() { var x = new Undefined() ? new object() : null; } }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue(), parseOptions: TestOptions.Regular8WithNullableAnalysis); comp.VerifyDiagnostics( // (6,21): error CS0246: The type or namespace name 'Undefined' could not be found (are you missing a using directive or an assembly reference?) // var x = new Undefined() ? new object() : null; Diagnostic(ErrorCode.ERR_SingleTypeNameNotFound, "Undefined").WithArguments("Undefined").WithLocation(6, 21)); var syntaxTree = comp.SyntaxTrees[0]; var root = syntaxTree.GetRoot(); var model = comp.GetSemanticModel(syntaxTree); var conditional = root.DescendantNodes().OfType <ConditionalExpressionSyntax>().Single(); var notNull = new NullabilityInfo(PublicNullableAnnotation.NotAnnotated, PublicNullableFlowState.NotNull); var @null = new NullabilityInfo(PublicNullableAnnotation.Annotated, PublicNullableFlowState.MaybeNull); var leftInfo = model.GetTypeInfo(conditional.WhenTrue); var rightInfo = model.GetTypeInfo(conditional.WhenFalse); Assert.Equal(notNull, leftInfo.Nullability); Assert.Equal(notNull, leftInfo.ConvertedNullability); Assert.Equal(@null, rightInfo.Nullability); Assert.Equal(notNull, rightInfo.ConvertedNullability); }
public void Equality() { var c = CreateCompilation(""); var obj = c.GetSpecialType(SpecialType.System_Object); var int32 = c.GetSpecialType(SpecialType.System_Int32); var notNullable = new NullabilityInfo(CodeAnalysis.NullableAnnotation.NotAnnotated, CodeAnalysis.NullableFlowState.NotNull); var nullable = new NullabilityInfo(CodeAnalysis.NullableAnnotation.Annotated, CodeAnalysis.NullableFlowState.MaybeNull); EqualityTesting.AssertEqual(default(TypeInfo), default(TypeInfo)); EqualityTesting.AssertEqual(new TypeInfo(obj, int32, nullable, notNullable), new TypeInfo(obj, int32, nullable, notNullable)); #pragma warning disable IDE0055 // Fix formatting: spacing is intentional to allow for visual field comparison EqualityTesting.AssertNotEqual(new TypeInfo(obj, obj, nullable, nullable), new TypeInfo(obj, int32, nullable, nullable)); EqualityTesting.AssertNotEqual(new TypeInfo(int32, obj, nullable, nullable), new TypeInfo(obj, obj, nullable, nullable)); EqualityTesting.AssertNotEqual(new TypeInfo(obj, int32, nullable, nullable), new TypeInfo(obj, int32, notNullable, nullable)); EqualityTesting.AssertNotEqual(new TypeInfo(obj, int32, nullable, nullable), new TypeInfo(obj, int32, nullable, notNullable)); #pragma warning restore IDE0055 // Fix formatting EqualityTesting.AssertEqual(new TypeInfo(int32, int32, default, default),
public void MultipleConversions() { var source = @" class A { public static explicit operator C(A a) => new D(); } class B : A { } class C { } class D : C { } class E { void M() { var d = (D)(C?)new B(); } }"; var comp = CreateCompilation(source, options: WithNonNullTypesTrue(), parseOptions: TestOptions.Regular8WithNullableAnalysis); comp.VerifyDiagnostics( // (10,17): warning CS8600: Converting null literal or possible null value to non-nullable type. // var d = (D)(C?)new B(); Diagnostic(ErrorCode.WRN_ConvertingNullableToNonNullable, "(D)(C?)new B()").WithLocation(10, 17)); var syntaxTree = comp.SyntaxTrees[0]; var root = syntaxTree.GetRoot(); var model = comp.GetSemanticModel(syntaxTree); var aType = comp.GetTypeByMetadataName("A"); var bType = comp.GetTypeByMetadataName("B"); var cType = comp.GetTypeByMetadataName("C"); var dType = comp.GetTypeByMetadataName("D"); var nullable = new NullabilityInfo(PublicNullableAnnotation.Annotated, PublicNullableFlowState.MaybeNull); var notNullable = new NullabilityInfo(PublicNullableAnnotation.NotAnnotated, PublicNullableFlowState.NotNull); var dCast = (CastExpressionSyntax)root.DescendantNodes().OfType <EqualsValueClauseSyntax>().Single().Value; var dInfo = model.GetTypeInfo(dCast); Assert.Equal(dType, dInfo.Type); Assert.Equal(dType, dInfo.ConvertedType); Assert.Equal(nullable, dInfo.Nullability); Assert.Equal(nullable, dInfo.ConvertedNullability); var cCast = (CastExpressionSyntax)dCast.Expression; var cInfo = model.GetTypeInfo(cCast); Assert.Equal(cType, cInfo.Type); Assert.Equal(cType, cInfo.ConvertedType); Assert.Equal(nullable, cInfo.Nullability); Assert.Equal(nullable, cInfo.ConvertedNullability); var objectCreation = cCast.Expression; var creationInfo = model.GetTypeInfo(objectCreation); Assert.Equal(bType, creationInfo.Type); Assert.Equal(aType, creationInfo.ConvertedType); Assert.Equal(notNullable, creationInfo.Nullability); Assert.Equal(nullable, creationInfo.ConvertedNullability); }
static int Main(string[] args) { MethodInfo testMethod = typeof(TestType).GetMethod("TestMethod") !; NullabilityInfoContext nullabilityContext = new NullabilityInfoContext(); NullabilityInfo nullability = nullabilityContext.Create(testMethod.ReturnParameter); if (nullability.ReadState != NullabilityState.Nullable) { return(-1); } return(100); }
public static bool IsContainsNullable(NullabilityInfo nullInfo) { if (nullInfo.ReadState != NullabilityState.Nullable) { if (nullInfo.ElementType != null) { return(IsContainsNullable(nullInfo.ElementType)); } else if (nullInfo.GenericTypeArguments.Length > 0) { return(nullInfo.GenericTypeArguments.Any(item => IsContainsNullable(item))); } return(false); } return(true); }
private static bool isNullableInfo(NullabilityInfo info) => info.WriteState == NullabilityState.Nullable || info.ReadState == NullabilityState.Nullable;
public static bool IsContainsNullable(this EventInfo eventInfo) { NullabilityInfo nullInfo = _nullableContextHandler.Create(eventInfo); return(IsContainsNullable(nullInfo)); }
public static bool IsContainsNullable(this ParameterInfo parameterInfo) { NullabilityInfo nullInfo = _nullableContextHandler.Create(parameterInfo); return(IsContainsNullable(nullInfo)); }
public static bool IsContainsNullable(this FieldInfo fieldInfo) { NullabilityInfo nullInfo = _nullableContextHandler.Create(fieldInfo); return(IsContainsNullable(nullInfo)); }
public static string GetMemberNullableDevelopName(this EventInfo eventInfo) { NullabilityInfo nullInfo = (new NullabilityInfoContext()).Create(eventInfo); return(GetMemberNullableDevelopName(nullInfo)); }