Skip to content

Commit

Permalink
re sync nullability code
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp committed Oct 1, 2023
1 parent aca8304 commit 7eaea5e
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
10 changes: 5 additions & 5 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryformat)


### Dictionary<TKey,TValue>

* `Boolean Remove<TKey, TValue>(TKey, TValue&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)


### IEnumerable<TSource>

* `IEnumerable<TSource[]> Chunk<TSource>(Int32)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.chunk)
Expand Down Expand Up @@ -54,11 +59,6 @@
* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat)


### Dictionary<TKey, TValue>

* `Boolean Remove(TKey, out TValue)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)


### Double

* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat)
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,11 @@ The class `PolyfillExtensions` includes the following extension methods:
* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.byte.tryformat)


### Dictionary<TKey,TValue>

* `Boolean Remove<TKey, TValue>(TKey, TValue&)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)


### IEnumerable<TSource>

* `IEnumerable<TSource[]> Chunk<TSource>(Int32)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.chunk)
Expand Down Expand Up @@ -409,11 +414,6 @@ The class `PolyfillExtensions` includes the following extension methods:
* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.decimal.tryformat)


### Dictionary<TKey, TValue>

* `Boolean Remove(TKey, out TValue)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2.remove)


### Double

* `Boolean TryFormat(Span<Char>, Int32&, ReadOnlySpan<Char>, IFormatProvider)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.double.tryformat)
Expand Down
62 changes: 36 additions & 26 deletions src/Polyfill/Nullability/NullabilityInfoContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,35 +97,47 @@ public NullabilityInfo Create(ParameterInfo parameterInfo)

private void CheckParameterMetadataType(ParameterInfo parameter, NullabilityInfo nullability)
{
if (parameter.Member is MethodInfo method)
ParameterInfo? metaParameter;
MemberInfo metaMember;

switch (parameter.Member)
{
MethodInfo metaMethod = GetMethodMetadataDefinition(method);
ParameterInfo? metaParameter = null;
if (string.IsNullOrEmpty(parameter.Name))
{
metaParameter = metaMethod.ReturnParameter;
}
else
{
ParameterInfo[] parameters = metaMethod.GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
if (parameter.Position == i &&
parameter.Name == parameters[i].Name)
{
metaParameter = parameters[i];
break;
}
}
}
case ConstructorInfo ctor:
var metaCtor = (ConstructorInfo)GetMemberMetadataDefinition(ctor);
metaMember = metaCtor;
metaParameter = GetMetaParameter(metaCtor, parameter);
break;

case MethodInfo method:
MethodInfo metaMethod = GetMethodMetadataDefinition(method);
metaMember = metaMethod;
metaParameter = string.IsNullOrEmpty(parameter.Name) ? metaMethod.ReturnParameter : GetMetaParameter(metaMethod, parameter);
break;

default:
return;
}

if (metaParameter != null)
if (metaParameter != null)
{
CheckGenericParameters(nullability, metaMember, metaParameter.ParameterType, parameter.Member.ReflectedType);
}
}

private static ParameterInfo? GetMetaParameter(MethodBase metaMethod, ParameterInfo parameter)
{
var parameters = metaMethod.GetParameters();
for (int i = 0; i < parameters.Length; i++)
{
if (parameter.Position == i &&
parameter.Name == parameters[i].Name)
{
CheckGenericParameters(nullability, metaMethod, metaParameter.ParameterType, parameter.Member.ReflectedType);
return parameters[i];
}
}
}

return null;
}
private static MethodInfo GetMethodMetadataDefinition(MethodInfo method)
{
if (method.IsGenericMethod && !method.IsGenericMethodDefinition)
Expand Down Expand Up @@ -210,9 +222,7 @@ public NullabilityInfo Create(PropertyInfo propertyInfo)

if (setter != null)
{
var parameters = setter.GetParameters();

CheckNullabilityAttributes(nullability, parameters[parameters.Length-1].GetCustomAttributesData());
CheckNullabilityAttributes(nullability, setter.GetParameters()[^1].GetCustomAttributesData());
}
else
{
Expand Down
4 changes: 3 additions & 1 deletion src/Tests/NullabilitySync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public async Task Run()
"""
var parameters = setter.GetParameters();
CheckNullabilityAttributes(nullability, parameters[parameters.Length-1].GetCustomAttributesData());
""");
""")
.Replace("ReadOnlySpan<ParameterInfo> parameters = metaMethod.GetParametersAsSpan();", "var parameters = metaMethod.GetParameters();")
.Replace(".GetParametersAsSpan()", ".GetParameters()");

var lines = infoContext.Split('\r', '\n');
infoContext = string.Join(Environment.NewLine, lines.Where(_ => !_.Contains("ArgumentNullException.ThrowIfNull")));
Expand Down

0 comments on commit 7eaea5e

Please sign in to comment.