-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expose nullability info #54985
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
Merged
Merged
Expose nullability info #54985
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2f7d8e6
Expose nullability info from Reflection
buyaa-n 364948a
Merge branch 'main' of https://github.com/dotnet/runtime into expose_…
buyaa-n d88b817
Expose nullability info from Reflection, latest updates
buyaa-n 2c9e791
Update generic array nullability T[]
buyaa-n df9304c
Add doc
buyaa-n bbc21a6
Apply feedback
buyaa-n e190c91
Use GetMemberWithSameMetadataDefinitionAs instead of Module.ResolveMe…
buyaa-n 47fdafe
Address more feedback
buyaa-n 8fec701
Merge conflict
buyaa-n bfd9f1b
Temoporarily disable tests on mono
buyaa-n 86a287d
Read only property WriteState=Unknown, disable tests on mono
buyaa-n 4c6e667
Add RequiresUnreferencedCode attribute to warn about trimmer
buyaa-n File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Expose nullability info from Reflection
- Loading branch information
commit 2f7d8e6a86a19a0195ff02b3e4dc146e49811131
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/libraries/System.Private.CoreLib/src/System/Reflection/Nullability/NullabilityInfo.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace System.Reflection | ||
| { | ||
| public sealed class NullabilityInfo | ||
| { | ||
| /*internal NullabilityInfo(Type type, NullableState state) | ||
| { | ||
| Type = type; | ||
| ReadState = state; | ||
| WriteState = state; | ||
| } | ||
|
|
||
| internal NullabilityInfo(Type type, NullableState readState, NullableState writeState) | ||
| { | ||
| Type = type; | ||
| ReadState = readState; | ||
| WriteState = writeState; | ||
| }*/ | ||
|
|
||
| internal NullabilityInfo(Type type, NullableState readState, NullableState writeState, | ||
| ReadOnlyCollection<NullableState>? arrayElements, ReadOnlyCollection<NullableState>? typeArguments) | ||
| { | ||
| Type = type; | ||
| ReadState = readState; | ||
| WriteState = writeState; | ||
| ArrayElements = arrayElements; | ||
| TypeArguments = typeArguments; | ||
| } | ||
|
|
||
| public Type Type { get; } | ||
| public NullableState ReadState { get; internal set; } | ||
| public NullableState WriteState { get; internal set; } | ||
| public ReadOnlyCollection<NullableState>? ArrayElements { get; } | ||
| public ReadOnlyCollection<NullableState>? TypeArguments { get; } | ||
| } | ||
|
|
||
| public enum NullableState | ||
| { | ||
| Unknown, | ||
| NonNullable, | ||
| Nullable, | ||
| NotNullableWhen, // Has NotNullWhenAttribute or NotNullIfNotNullAttribute, check CustomAttributes for the attribute and value | ||
| NullableWhen // Has MaybeNullWhenAttribute check CustomAttributes for the value | ||
| } | ||
| } | ||
254 changes: 254 additions & 0 deletions
254
...raries/System.Private.CoreLib/src/System/Reflection/Nullability/NullabilityInfoContext.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Collections.ObjectModel; | ||
|
|
||
| namespace System.Reflection | ||
| { | ||
| public sealed class NullabilityInfoContext | ||
| { | ||
| private static NullableState GetNullableContext(MemberInfo? memberInfo) | ||
| { | ||
| while (memberInfo != null) | ||
| { | ||
| var attributes = memberInfo.GetCustomAttributesData(); | ||
| foreach (var attribute in attributes) | ||
| { | ||
| if (attribute.AttributeType.Name == "NullableContextAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Runtime.CompilerServices" && | ||
| attribute.ConstructorArguments.Count == 1) | ||
| { | ||
| return TranslateByte(attribute.ConstructorArguments[0].Value); | ||
| } | ||
| } | ||
|
|
||
| memberInfo = memberInfo.DeclaringType; | ||
| } | ||
|
|
||
| return NullableState.Unknown; | ||
| } | ||
|
|
||
| public NullabilityInfo Create(ParameterInfo parameterInfo) | ||
| { | ||
| return GetNullabilityInfo(parameterInfo.Member, parameterInfo.ParameterType, parameterInfo.GetCustomAttributesData()); | ||
| } | ||
|
|
||
| public NullabilityInfo Create(PropertyInfo propertyInfo) | ||
| { | ||
| var nullability = GetNullabilityInfo(propertyInfo, propertyInfo.PropertyType, propertyInfo.GetCustomAttributesData()); | ||
| var getterAttributes = propertyInfo.GetGetMethod()?.ReturnParameter.GetCustomAttributesData(); | ||
| var setterAttributes = propertyInfo.GetSetMethod()?.GetParameters()[0].GetCustomAttributesData(); | ||
|
|
||
| if (getterAttributes != null) | ||
| { | ||
| foreach (var attribute in getterAttributes) | ||
| { | ||
| if (nullability.ReadState == NullableState.Nullable) | ||
| { | ||
| if (attribute.AttributeType.Name == "NotNullAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") | ||
| { | ||
| nullability.ReadState = NullableState.NonNullable; | ||
| break; | ||
| } | ||
| } | ||
| else if (nullability.ReadState == NullableState.NonNullable) | ||
| { | ||
| if (attribute.AttributeType.Name == "MaybeNullAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") | ||
| { | ||
| nullability.ReadState = NullableState.Nullable; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (setterAttributes != null) | ||
| { | ||
| foreach (var attribute in setterAttributes) | ||
| { | ||
| if (nullability.WriteState == NullableState.Nullable) | ||
| { | ||
| if (attribute.AttributeType.Name == "DisallowNullAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") | ||
| { | ||
| nullability.WriteState = NullableState.NonNullable; | ||
| break; | ||
| } | ||
| } | ||
| else if (nullability.WriteState == NullableState.NonNullable) | ||
| { | ||
| if (attribute.AttributeType.Name == "AllowNullAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") | ||
| { | ||
| nullability.WriteState = NullableState.Nullable; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nullability; | ||
| } | ||
|
|
||
| public NullabilityInfo Create(EventInfo eventInfo) | ||
| { | ||
| return GetNullabilityInfo(eventInfo, eventInfo.EventHandlerType!, eventInfo.GetCustomAttributesData()); | ||
| } | ||
|
|
||
| public NullabilityInfo Create(FieldInfo fieldInfo) | ||
| { | ||
| var attributes = fieldInfo.GetCustomAttributesData(); | ||
| var nullability = GetNullabilityInfo(fieldInfo, fieldInfo.FieldType, attributes); | ||
|
|
||
| if (attributes != null) | ||
| { | ||
| foreach (var attribute in attributes) | ||
| { | ||
| if (nullability.ReadState == NullableState.Nullable) | ||
| { | ||
| if (attribute.AttributeType.Name == "NotNullAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") | ||
| { | ||
| nullability.ReadState = NullableState.NonNullable; | ||
| break; | ||
| } | ||
| } | ||
| else if (nullability.ReadState == NullableState.NonNullable) | ||
| { | ||
| if (attribute.AttributeType.Name == "MaybeNullAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") | ||
| { | ||
| nullability.ReadState = NullableState.Nullable; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (nullability.WriteState == NullableState.Nullable) | ||
| { | ||
| if (attribute.AttributeType.Name == "DisallowNullAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") | ||
| { | ||
| nullability.WriteState = NullableState.NonNullable; | ||
| break; | ||
| } | ||
| } | ||
| else if (nullability.WriteState == NullableState.NonNullable) | ||
| { | ||
| if (attribute.AttributeType.Name == "AllowNullAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Diagnostics.CodeAnalysis") | ||
| { | ||
| nullability.WriteState = NullableState.Nullable; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return nullability; | ||
| } | ||
|
|
||
| private static NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, IList<CustomAttributeData> customAttributes) | ||
| { | ||
| var offset = 0; | ||
| return GetNullabilityInfo(memberInfo, type, customAttributes, ref offset); | ||
| } | ||
|
|
||
| private static NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, IList<CustomAttributeData> customAttributes, ref int offset) | ||
| { | ||
| NullableState state = NullableState.Unknown; | ||
| bool found = false; | ||
| if (type.IsValueType) | ||
| { | ||
| var underlyingType = Nullable.GetUnderlyingType(type); | ||
| if ( underlyingType != null) | ||
| { | ||
| type = underlyingType; | ||
buyaa-n marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| state = NullableState.Nullable; | ||
| found = true; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| foreach (var attribute in customAttributes) | ||
| { | ||
| if (attribute.AttributeType.Name == "NullableAttribute" && | ||
| attribute.AttributeType.Namespace == "System.Runtime.CompilerServices" && | ||
| attribute.ConstructorArguments.Count == 1) | ||
| { | ||
| var o = attribute.ConstructorArguments[0].Value; | ||
|
|
||
| if (o is byte b) | ||
| { | ||
| found = true; | ||
| state = TranslateByte(b); | ||
| } | ||
| else if (o is ReadOnlyCollection<CustomAttributeTypedArgument> args) | ||
| if (offset < args.Count && | ||
| args[offset].Value is byte elementB) | ||
| state = TranslateByte(elementB); | ||
|
|
||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (!found) | ||
| { | ||
| state = GetNullableContext(memberInfo); | ||
| } | ||
| } | ||
|
|
||
| // We consumed one element in the nullable array. | ||
| offset++; | ||
|
|
||
| ReadOnlyCollection<NullableState>? elementsState = null; | ||
| ReadOnlyCollection<NullableState>? genericArgumentsState = null; | ||
|
|
||
| if (type.IsArray) | ||
| { | ||
| var elements = new List<NullableState>(); | ||
| var elementType = type.GetElementType()!; | ||
| int i = 0; | ||
| do | ||
| { | ||
| // add to elements | ||
| var n = GetNullabilityInfo(memberInfo, elementType, elementType.GetCustomAttributesData(), ref offset); | ||
| elementType = type.GetElementType()!; | ||
| elements[i++] = n.ReadState; | ||
| } while (type.IsArray); | ||
|
|
||
| elementsState = elements.AsReadOnly(); | ||
| } | ||
| else if (type.IsGenericType) | ||
| { | ||
| var genericArguments = type.GetGenericArguments(); | ||
| var argumentsState = new List<NullableState>(genericArguments.Length); | ||
|
|
||
| for (int i = 0; i < genericArguments.Length; i++) | ||
| { | ||
| var n = GetNullabilityInfo(memberInfo, genericArguments[i], genericArguments[i].GetCustomAttributesData(), ref offset); | ||
| argumentsState[i] = n.ReadState; | ||
| } | ||
|
|
||
| genericArgumentsState = argumentsState.AsReadOnly(); | ||
| } | ||
|
|
||
| return new NullabilityInfo(type, state, state, elementsState, genericArgumentsState); | ||
| } | ||
|
|
||
| private static NullableState TranslateByte(object? singleValue) | ||
| { | ||
| return singleValue is byte b ? TranslateByte(b) : NullableState.Unknown; | ||
| } | ||
|
|
||
| private static NullableState TranslateByte(byte b) => | ||
| b switch | ||
| { | ||
| 1 => NullableState.NonNullable, | ||
| 2 => NullableState.Nullable, | ||
| _ => NullableState.Unknown | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.