Skip to content
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

Support generic type parameter when created with TypeBuilder #112372

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

steveharter
Copy link
Member

Fixes #112155

There may be other related edge cases related to "signature types"; this PR only fixes the case mentioned.

@steveharter steveharter added this to the 10.0.0 milestone Feb 10, 2025
@steveharter steveharter requested a review from jkotas February 10, 2025 21:57
@steveharter steveharter self-assigned this Feb 10, 2025
@@ -312,6 +355,8 @@ public static void MakeSignatureConstructedGenericType(Type genericTypeDefinitio
Assert.False(et.IsGenericTypeParameter);
Assert.True(et.IsGenericMethodParameter);
Assert.Equal(5, et.GenericParameterPosition);
Assert.Throws<NotSupportedException>(() => et.IsValueType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move up before Type et = t.GenericTypeArguments[0]; line and change to Assert.False.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, I wanted to make sure the new code that was added for IsValueType\IsEnum throws when the generic parameter is a signature type.

If I compare t with t.IsValueType that is just testing normal reflection and will return true for Span<sigtype> and false for the List<sigtype>.

{
// These have normal generic argument types, not SignatureTypes. Using a SignatureType from MakeGenericMethodParameter()
// as the generic type argument throws NotSupportedException which is verified in MakeSignatureConstructedGenericType().
yield return new object[] { Type.MakeGenericSignatureType(typeof(List<>), typeof(int)).GetGenericArguments()[0], true };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Type.MakeGenericSignatureType(typeof(List<>), typeof(int)).GetGenericArguments()[0] is going to be typeof(int), so this test does not seem to be actually testing IsValueType property on signature types.

Should this rather be something like:

yield return new object[] { Type.MakeGenericSignatureType(typeof(List<>), typeof(int)), false };
yield return new object[] { Type.MakeGenericSignatureType(typeof(KeyValuePair<,>), typeof(string), typeof(object)), true };

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct this is not verifying signature types but is verifying that when a non-signature type is used as a generic parameter then IsValueType works as expected. The test with GetGenericArguments() is done in MakeSignatureConstructedGenericType() as mentioned in the comment.

I'm not sure if your feedback here came before or after I pushed a new commit that changed this and added the comment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

verifying that when a non-signature type is used as a generic parameter then IsValueType works as expected.

Would it be better to verify that GetGenericArguments() returns the exact same Type instances as was passed into MakeGenericSignatureType?

If we do that, we do not need to worry about testing individual properties of the types returned by GetGenericArguments. We can depend on that testing done elsewhere (e.g. as part of runtime Type testing)

Copy link
Member

@jkotas jkotas Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My higher level concerns in both this and the other comment are:

  • Missing test coverage for the code introduced in this PR. For example, if SignatureConstructedGenericType.IsEnum implementation is changed to return hardcoded false, no test is going to fail. These test gaps should be fixed. They are kind of hard to see because they are muddied by the unnecessary tests.
  • Unnecessary test coverage for Types that are not signature types. It would be best to delete most of it and assume that the non-signature Types are tested elsewhere. This is pre-existing problem in these tests. I would not mind if you delete tests from this file that are not relevant to testing of signature types.

{
// These have normal generic argument types, not SignatureTypes. Using a SignatureType from MakeGenericMethodParameter()
// as the generic type argument throws NotSupportedException which is verified in MakeSignatureConstructedGenericType().
yield return new object[] { Type.MakeGenericSignatureType(typeof(List<>), typeof(MyEnum)).GetGenericArguments()[0], true };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dtto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants