Skip to content

Do not warn when accessing fields on T constrained to be enum #100814

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 1 commit into from
Apr 9, 2024
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 @@ -15,6 +15,17 @@ internal readonly partial struct GenericParameterProxy

internal partial bool HasDefaultConstructorConstraint() => GenericParameter.HasDefaultConstructorConstraint;

internal partial bool HasEnumConstraint()
{
foreach (TypeDesc constraint in GenericParameter.TypeConstraints)
{
if (constraint.IsWellKnownType(Internal.TypeSystem.WellKnownType.Enum))
return true;
}

return false;
}

public readonly GenericParameterDesc GenericParameter;

public override string ToString() => GenericParameter.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ internal partial bool HasDefaultConstructorConstraint () =>
TypeParameterSymbol.HasValueTypeConstraint |
TypeParameterSymbol.HasUnmanagedTypeConstraint;

internal partial bool HasEnumConstraint ()
{
foreach (ITypeSymbol constraintType in TypeParameterSymbol.ConstraintTypes) {
if (constraintType.SpecialType == SpecialType.System_Enum)
return true;
}

return false;
}

public readonly ITypeParameterSymbol TypeParameterSymbol;

public override string ToString () => TypeParameterSymbol.ToString ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public void Invoke (in MultiValue value, ValueWithDynamicallyAccessedMembers tar
&& uniqueValue is GenericParameterValue genericParam
&& genericParam.GenericParameter.HasDefaultConstructorConstraint ()) {
// We allow a new() constraint on a generic parameter to satisfy DynamicallyAccessedMemberTypes.PublicParameterlessConstructor
} else if (targetValue.DynamicallyAccessedMemberTypes == DynamicallyAccessedMemberTypes.PublicFields
&& uniqueValue is GenericParameterValue maybeEnumConstrainedGenericParam
&& maybeEnumConstrainedGenericParam.GenericParameter.HasEnumConstraint ()) {
// We allow a System.Enum constraint on a generic parameter to satisfy DynamicallyAccessedMemberTypes.PublicFields
} else if (uniqueValue is ValueWithDynamicallyAccessedMembers valueWithDynamicallyAccessedMembers) {
if (uniqueValue is NullableValueWithDynamicallyAccessedMembers nullableValue) {
MarkTypeForDynamicallyAccessedMembers (nullableValue.NullableType, nullableValue.DynamicallyAccessedMemberTypes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ namespace ILLink.Shared.TypeSystemProxy
internal readonly partial struct GenericParameterProxy
{
internal partial bool HasDefaultConstructorConstraint ();

internal partial bool HasEnumConstraint ();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ internal readonly partial struct GenericParameterProxy

internal partial bool HasDefaultConstructorConstraint () => GenericParameter.HasDefaultConstructorConstraint;

internal partial bool HasEnumConstraint ()
{
if (GenericParameter.HasConstraints) {
foreach (GenericParameterConstraint? constraint in GenericParameter.Constraints) {
if (constraint.ConstraintType.Name == "Enum" && constraint.ConstraintType.Namespace == "System")
return true;
}
}

return false;
}

public readonly GenericParameter GenericParameter;

public override string ToString () => GenericParameter.ToString ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public static void Main ()
TestStructConstraintSatisfiesParameterlessConstructor<TestStruct> ();
TestUnmanagedConstraintSatisfiesParameterlessConstructor<byte> ();

TestEnumConstraintSatisfiesPublicFields<Enum> ();

TestGenericParameterFlowsToField ();
TestGenericParameterFlowsToReturnValue ();

Expand Down Expand Up @@ -798,6 +800,11 @@ static void TestUnmanagedConstraintSatisfiesParameterlessConstructor<T> () where
{
}

static void TestEnumConstraintSatisfiesPublicFields<T> () where T : Enum
{
typeof (T).RequiresPublicFields ();
}

// Warn about calls to static methods:
[ExpectedWarning ("IL2026", "TypeRequiresPublicFields", "RUCTest()", "message")]
[ExpectedWarning ("IL2026", "RUCMethodRequiresPublicMethods", "message")]
Expand Down