Skip to content

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

Merged
merged 11 commits into from
Feb 21, 2025

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

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@steveharter steveharter added the breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. label Feb 20, 2025
@dotnet-policy-service dotnet-policy-service bot added the needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet label Feb 20, 2025
{
Type instantiationElem = instantiation[i] ?? throw new ArgumentNullException();
Type instantiationElem = typeArguments[i] ?? throw new ArgumentNullException();
Copy link
Member Author

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.

@steveharter steveharter merged commit 931d75f into dotnet:main Feb 21, 2025
135 of 140 checks passed
@steveharter steveharter deleted the Reflection-112155 branch February 21, 2025 10:47
@github-actions github-actions bot locked and limited conversation to collaborators Mar 24, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection.Emit breaking-change Issue or PR that represents a breaking API or functional change over a prerelease. needs-breaking-change-doc-created Breaking changes need an issue opened with https://github.com/dotnet/docs/issues/new?template=dotnet
Projects
None yet
2 participants