Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static void CheckNullabilityAttributes(NullabilityInfo nullability, ILis
else if ((attribute.AttributeType.Name == "MaybeNullAttribute" ||
attribute.AttributeType.Name == "MaybeNullWhenAttribute") &&
codeAnalysisReadState == NullabilityState.Unknown &&
!nullability.Type.IsValueType)
!IsValueTypeOrValueTypeByRef(nullability.Type))
{
codeAnalysisReadState = NullabilityState.Nullable;
}
Expand All @@ -151,7 +151,7 @@ private static void CheckNullabilityAttributes(NullabilityInfo nullability, ILis
}
else if (attribute.AttributeType.Name == "AllowNullAttribute" &&
codeAnalysisWriteState == NullabilityState.Unknown &&
!nullability.Type.IsValueType)
!IsValueTypeOrValueTypeByRef(nullability.Type))
{
codeAnalysisWriteState = NullabilityState.Nullable;
}
Expand Down Expand Up @@ -327,7 +327,7 @@ private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, Nul
int index = 0;
NullabilityInfo nullability = GetNullabilityInfo(memberInfo, type, parser, ref index);

if (!type.IsValueType && nullability.ReadState != NullabilityState.Unknown)
if (nullability.ReadState != NullabilityState.Unknown)
{
TryLoadGenericMetaTypeNullability(memberInfo, nullability);
}
Expand All @@ -340,19 +340,22 @@ private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, Nul
NullabilityState state = NullabilityState.Unknown;
NullabilityInfo? elementState = null;
NullabilityInfo[] genericArgumentsState = Array.Empty<NullabilityInfo>();
Type? underlyingType = type;
Type underlyingType = type;

if (type.IsValueType)
if (underlyingType.IsByRef || underlyingType.IsPointer)
{
underlyingType = Nullable.GetUnderlyingType(type);
underlyingType = underlyingType.GetElementType()!;
}

if (underlyingType != null)
if (underlyingType.IsValueType)
{
if (Nullable.GetUnderlyingType(underlyingType) is { } nullableUnderlyingType)
{
underlyingType = nullableUnderlyingType;
state = NullabilityState.Nullable;
}
else
{
underlyingType = type;
state = NullabilityState.NotNull;
}

Expand All @@ -369,9 +372,9 @@ private NullabilityInfo GetNullabilityInfo(MemberInfo memberInfo, Type type, Nul
state = contextState;
}

if (type.IsArray)
if (underlyingType.IsArray)
{
elementState = GetNullabilityInfo(memberInfo, type.GetElementType()!, parser, ref index);
elementState = GetNullabilityInfo(memberInfo, underlyingType.GetElementType()!, parser, ref index);
}
}

Expand Down Expand Up @@ -468,6 +471,12 @@ private void CheckGenericParameters(NullabilityInfo nullability, MemberInfo meta
{
CheckGenericParameters(elementNullability, metaMember, metaType.GetElementType()!, reflectedType);
}
// We could also follow this branch for metaType.IsPointer, but since pointers must be unmanaged this
// will be a no-op regardless
else if (metaType.IsByRef)
{
CheckGenericParameters(nullability, metaMember, metaType.GetElementType()!, reflectedType);
}
}
}

Expand All @@ -482,6 +491,11 @@ private bool TryUpdateGenericParameterNullability(NullabilityInfo nullability, T
return true;
}

if (IsValueTypeOrValueTypeByRef(nullability.Type))
{
return true;
}

var state = NullabilityState.Unknown;
if (CreateParser(genericParameter.GetCustomAttributesData()).ParseNullableState(0, ref state))
{
Expand Down Expand Up @@ -549,9 +563,10 @@ static int CountNullabilityStates(Type type)
}
return count;
}
if (underlyingType.IsArray)

if (underlyingType.HasElementType)
{
return 1 + CountNullabilityStates(underlyingType.GetElementType()!);
return (underlyingType.IsArray ? 1 : 0) + CountNullabilityStates(underlyingType.GetElementType()!);
}

return type.IsValueType ? 0 : 1;
Expand All @@ -560,7 +575,7 @@ static int CountNullabilityStates(Type type)

private bool TryPopulateNullabilityInfo(NullabilityInfo nullability, NullableAttributeStateParser parser, ref int index)
{
bool isValueType = nullability.Type.IsValueType;
bool isValueType = IsValueTypeOrValueTypeByRef(nullability.Type);
if (!isValueType)
{
var state = NullabilityState.Unknown;
Expand Down Expand Up @@ -606,6 +621,9 @@ private static NullabilityState TranslateByte(byte b) =>
_ => NullabilityState.Unknown
};

private static bool IsValueTypeOrValueTypeByRef(Type type) =>
type.IsValueType || ((type.IsByRef || type.IsPointer) && type.GetElementType()!.IsValueType);

private readonly struct NullableAttributeStateParser
{
private static readonly object UnknownByte = (byte)0;
Expand Down
Loading