-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
base: main
Are you sure you want to change the base?
Conversation
src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureGenericParameterType.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureHasElementType.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureConstructedGenericType.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Reflection/TypeDelegator.cs
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureConstructedGenericType.cs
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Reflection/TypeDelegator.cs
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Outdated
Show resolved
Hide resolved
@@ -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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 }; |
There was a problem hiding this comment.
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 };
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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 hardcodedfalse
, 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 }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dtto
Fixes #112155
There may be other related edge cases related to "signature types"; this PR only fixes the case mentioned.