-
Notifications
You must be signed in to change notification settings - Fork 5k
Support generic type parameter when created with TypeBuilder #112372
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
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
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Outdated
Show resolved
Hide resolved
{ | ||
// 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.
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.
I did remove newly added code that did not directly verify sig types.
Added tests for IsEnum
for both true and false.
Also I found that the validation in MakeGenericSignatureType() did not check it the incoming type was generic or not, so I updated that and compared against MakeGenericType() which I also updated since the parameter names were different in the exception text (the internal override
changed the parameter name from the public abstract
). So in this case, I did add tests that are not sig-specific but it did catch this issue plus it's nice to see in one location.
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.
Also I found that the validation in MakeGenericSignatureType() did not check it the incoming type was generic or not, so I updated that
This is technically a breaking change. Just pointing it out, I do not see a problem with it.
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.
I wonder if this breaking change meets the threshold to document it; I'll mark this breaking change for now and re-evaluate later as well as update the exception doc.
src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Reflection/SignatureTypes.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Reflection/SignatureConstructedGenericType.cs
Outdated
Show resolved
Hide resolved
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.
LGTM. Thanks!
{ | ||
Type instantiationElem = instantiation[i] ?? throw new ArgumentNullException(); | ||
Type instantiationElem = typeArguments[i] ?? throw new ArgumentNullException(); |
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.
Ideally, this should throw ArgumentException
instead of ArgumentNullException
with some string like "array element at offset X should not be null", but that is outside the scope of this PR.
Fixes #112155
There may be other related edge cases related to "signature types"; this PR only fixes the case mentioned.