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
Comment on lines 90 to 92
private static void MyGenericValueTypeMethod<T>() where T : struct { }
private static void MyGenericRefTypeMethod<T>() where T : class { }

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
private static void MyGenericValueTypeMethod<T>() where T : struct { }
private static void MyGenericRefTypeMethod<T>() where T : class { }

@@ -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.

{
// 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