-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Add handling of generic attributes to CustomAttributeDecoder #57466
Add handling of generic attributes to CustomAttributeDecoder #57466
Conversation
If the attribute constructor refers to the generic T in its signature, we would throw `BadImageFormatException`. This adds handling by capturing the generic context and interpreting the signature variables when needed.
.../System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/CustomAttributeDecoder.cs
Show resolved
Hide resolved
Named argument can't by of generic type argument type? class A<S, T>
{
S[] P { get; }
A(T arg) {}
}
[A<int, bool>(1, P = new[] { true })]
class C {} Refers to: src/libraries/System.Reflection.Metadata/src/System/Reflection/Metadata/Ecma335/CustomAttributeDecoder.cs:230 in 5204378. [](commit_id = 5204378, deletion_comment = 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.
We should definitely add unit tests for this.
You can use MetadataBuilder to construct the metadata image in the test that has generic attributes and then read them back.
Named arguments encode their type inline - we don't need to resolve the members to find their signature. |
Never mind, the types used there will be specific instantiations, not generic type parameters. |
|
||
int parameterIndex = signatureReader.ReadCompressedInteger(); | ||
int numGenericParameters = genericContextReader.ReadCompressedInteger(); | ||
if (parameterIndex >= numGenericParameters) |
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.
consider also checking for negative parameterIndex and numGenericParameters
|
||
switch (typeCode) | ||
{ | ||
case (int)SignatureTypeCode.Boolean: |
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.
isn't there any existing subroutine for checking simple 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.
considering it's not valid in C# to create generic attributes I'm ok with no extra tests and just unblocking scenarios for problematic assemblies. If they're ever supported then we should add more tests and fix any rough edges if there are any
@krwq Generic attributes are going to be supported in C# 10 or 11. We need tests. |
Generic attributes are shipping in preview in .NET 6/C# 10, and will hopefully ship stable in C# 11. |
I thought I would be able to get to the tests, but it probably won't happen anytime soon. I filed #58073. It's better if it's handled by someone else. The fix I have works and I can just fork the file where I need this. I have test coverage through other CoreCLR tests from that. |
@tmat @RikkiGibson if that's the case then agreed we need tests |
If the attribute constructor refers to the generic T in its signature, we would throw
BadImageFormatException
. This adds handling by capturing the generic context and interpreting the signature variables when needed.I've hit this in the src\tests\reflection\GenericAttribute tests with NativeAOT since the NativeAOT compiler uses S.R.Metadata to read custom attributes.
No tests yet because I would like to discuss testing strategy.
CustomAttributeDecoderTests
.I tested it with the
src\tests\reflection\GenericAttribute
test and NativeAOT, but that test is not much more than a smoke test, really.Cc @tmat @RikkiGibson