Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.6k
[STJ] Add support for nullable reference annotations on properties#102499
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
97a3275650b53e12cb92ec22a5676be27d90be6dfe1b0d9543c74fb83656b29dcd54a9080a35356015c7f82556c22dee5c653053089e6cfd1e4388fe2a37914d63f59File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -50,6 +50,12 @@ public static ITypeSymbol EraseCompileTimeMetadata(this Compilation compilation, | ||
| type = type.WithNullableAnnotation(NullableAnnotation.None); | ||
| } | ||
| if (type is IArrayTypeSymbol arrayType) | ||
| { | ||
| ITypeSymbol elementType = compilation.EraseCompileTimeMetadata(arrayType.ElementType); | ||
| return compilation.CreateArrayTypeSymbol(elementType, arrayType.Rank); | ||
| } | ||
| if (type is INamedTypeSymbol namedType) | ||
| { | ||
| if (namedType.IsTupleType) | ||
| @@ -189,6 +195,9 @@ SpecialType.System_Byte or SpecialType.System_UInt16 or SpecialType.System_UInt3 | ||
| SpecialType.System_Single or SpecialType.System_Double or SpecialType.System_Decimal; | ||
| } | ||
| public static bool IsNullableType(this ITypeSymbol type) | ||
| => !type.IsValueType || type.OriginalDefinition.SpecialType is SpecialType.System_Nullable_T; | ||
| public static bool IsNullableValueType(this ITypeSymbol type, [NotNullWhen(true)] out ITypeSymbol? elementType) | ||
| { | ||
| if (type.IsValueType && type is INamedTypeSymbol { OriginalDefinition.SpecialType: SpecialType.System_Nullable_T }) | ||
| @@ -269,5 +278,107 @@ public static string GetTypeKindKeyword(this TypeDeclarationSyntax typeDeclarati | ||
| return null; | ||
| } | ||
| } | ||
| public static void ResolveNullabilityAnnotations(this IFieldSymbol field, out bool isGetterNonNullable, out bool isSetterNonNullable) | ||
eiriktsarpalis marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| { | ||
| if (field.Type.IsNullableType()) | ||
| { | ||
| // Because System.Text.Json cannot distinguish between nullable and non-nullable type parameters, | ||
| // (e.g. the same metadata is being used for both KeyValuePair<string, string?> and KeyValuePair<string, string>), | ||
| // we derive nullability annotations from the original definition of the field and not its instantiation. | ||
| // This preserves compatibility with the capabilities of the reflection-based NullabilityInfo reader. | ||
| field = field.OriginalDefinition; | ||
| isGetterNonNullable = IsOutputTypeNonNullable(field, field.Type); | ||
| isSetterNonNullable = IsInputTypeNonNullable(field, field.Type); | ||
| } | ||
| else | ||
| { | ||
| isGetterNonNullable = isSetterNonNullable = false; | ||
| } | ||
| } | ||
| public static void ResolveNullabilityAnnotations(this IPropertySymbol property, out bool isGetterNonNullable, out bool isSetterNonNullable) | ||
| { | ||
| if (property.Type.IsNullableType()) | ||
| { | ||
| // Because System.Text.Json cannot distinguish between nullable and non-nullable type parameters, | ||
| // (e.g. the same metadata is being used for both KeyValuePair<string, string?> and KeyValuePair<string, string>), | ||
| // we derive nullability annotations from the original definition of the field and not its instantiation. | ||
| // This preserves compatibility with the capabilities of the reflection-based NullabilityInfo reader. | ||
| property = property.OriginalDefinition; | ||
| isGetterNonNullable = property.GetMethod != null && IsOutputTypeNonNullable(property, property.Type); | ||
| isSetterNonNullable = property.SetMethod != null && IsInputTypeNonNullable(property, property.Type); | ||
| } | ||
| else | ||
| { | ||
| isGetterNonNullable = isSetterNonNullable = false; | ||
| } | ||
| } | ||
| public static bool IsNullable(this IParameterSymbol parameter) | ||
| { | ||
| if (parameter.Type.IsNullableType()) | ||
| { | ||
| // Because System.Text.Json cannot distinguish between nullable and non-nullable type parameters, | ||
| // (e.g. the same metadata is being used for both KeyValuePair<string, string?> and KeyValuePair<string, string>), | ||
| // we derive nullability annotations from the original definition of the field and not instation. | ||
| // This preserves compatibility with the capabilities of the reflection-based NullabilityInfo reader. | ||
| parameter = parameter.OriginalDefinition; | ||
| return !IsInputTypeNonNullable(parameter, parameter.Type); | ||
| } | ||
| return false; | ||
| } | ||
| private static bool IsOutputTypeNonNullable(this ISymbol symbol, ITypeSymbol returnType) | ||
| { | ||
| if (symbol.HasCodeAnalysisAttribute("MaybeNullAttribute")) | ||
| { | ||
| return false; | ||
| } | ||
| if (symbol.HasCodeAnalysisAttribute("NotNullAttribute")) | ||
| { | ||
| return true; | ||
| } | ||
| if (returnType is ITypeParameterSymbol { HasNotNullConstraint: false }) | ||
| { | ||
| return false; | ||
| } | ||
| return returnType.NullableAnnotation is NullableAnnotation.NotAnnotated; | ||
| } | ||
| private static bool IsInputTypeNonNullable(this ISymbol symbol, ITypeSymbol inputType) | ||
| { | ||
| Debug.Assert(inputType.IsNullableType()); | ||
| if (symbol.HasCodeAnalysisAttribute("AllowNullAttribute")) | ||
| { | ||
| return false; | ||
| } | ||
| if (symbol.HasCodeAnalysisAttribute("DisallowNullAttribute")) | ||
| { | ||
| return true; | ||
| } | ||
| if (inputType is ITypeParameterSymbol { HasNotNullConstraint: false }) | ||
| { | ||
| return false; | ||
| } | ||
| return inputType.NullableAnnotation is NullableAnnotation.NotAnnotated; | ||
| } | ||
| private static bool HasCodeAnalysisAttribute(this ISymbol symbol, string attributeName) | ||
| { | ||
| return symbol.GetAttributes().Any(attr => | ||
eiriktsarpalis marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| attr.AttributeClass?.Name == attributeName && | ||
| attr.AttributeClass.ContainingNamespace.ToDisplayString() == "System.Diagnostics.CodeAnalysis"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -30,6 +30,9 @@ private static class ExceptionMessages | ||
| public const string InvalidSerializablePropertyConfiguration = | ||
| "Invalid serializable-property configuration specified for type '{0}'. For more information, see 'JsonSourceGenerationMode.Serialization'."; | ||
| public const string PropertyGetterDisallowNull = | ||
| "The property or field '{0}' on type '{1}' doesn't allow getting null values. Consider updating its nullability annotation."; | ||
jozkee marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| }; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.