-
Notifications
You must be signed in to change notification settings - Fork 127
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
Generic type array causes InvalidOperationException in ReflectionMethodBodyScanner.GetValueNodeFromGenericArgument #1559
Comments
This is a bug we should fix. It's somewhat related to #1500 as it fails in exactly the same spot, but for different reason:
This uncovers an aspect of data flow we didn't test - handling of more complex types - so typically array types, but same would probably apply to all Current behavior:
Given the existing behavior I think we should fix this by treating The downside is that linker will end up marking more than necessary - in theory this should only mark members of the A separate issue should be filed to solve the array problem all up. |
/cc @MichalStrehovsky for ideas and opinions on the proposed fix. |
How difficult would it be to fix this in a way where would simply reduce all arrays we see into Console.WriteLine(typeof(object[]).GetProperty("LongLength").GetValue(new object[0])); which will probably fail right now. |
Side-note: I tried the sample and it works, but there could be many reasons why it happens to work. I do agree that marking the But also going forward, if we were to fully support arrays, that is correctly handle things like: object InitializeArray(Type arrayType, int count)
{
IList array = (IList)Activator.CreateInstance(arrayType);
for (int i = 0; i < count; i++)
{
array.Add(Activator.CreateInstance(arrayType.GetElementType());
}
return array;
} This would mean we would have to support double annotations to be perfectly precise. Or alternatively we could "live with the existing behavior" and apply the annotation to both the array and the element type. So in the above case just adding |
In our model, unknowingly relying on something being kept should mean there's a warning in that spot. We could e.g. have a change in the libraries in 6.0 that results in a different set of members in the app being kept (e.g. System.Text.Json stops requiring reflection access to all properties because we switch it to a source generated model). That would be an acceptable change in our model because there must be a warning in the spot that unwittingly relies on things being kept. The fact that the spot used to work by accident is secondary - we're allowed to break code if there's a warning. Warning doesn't mean the code won't work - it means it might not work.
This will be marked using the default linker rules - the T/int is statically present within the app and linker will mark it. The worst that can happen is that the type will be marked hollowed out (only the type definition is kept). But you can still make an array of it at runtime. If you want to reflection set an element to something, you'll need to get at it either statically (in which case this is not even a reflection analysis problem), or through some other reflection that will either be properly annotated, or warn.
Yes, we'll need to model things precisely for that, but going that route starts to smell a lot like the .NET Native analyzer. Such detailed analysis didn't buy us anything in the end. I would be wary of going there again. Nit: Activator.CreateInstance cannot be used with arrays :). |
The test is currently failing due to dotnet#1559
Once this is fixed we should enable test added in #1564. |
The test is currently failing due to #1559
I think I came around on this one - thanks @MichalStrehovsky . We should definitely mark the In theory this problem applies to multiple things, but in reality it's really only arrays and only cases where somebody has a very general reflection based implementation of handling elements in the array. The more common case is generics, but for those we already have a solution for. |
I'm seeing the same error in FSharp.SystemTextJson, specifically in the converter for F# lists (this line). |
Thanks. I could successfully link FSharp.SystemTextJson with a master branch linker. This issue was fixed here: #1566. We should probably take that PR to release/5.0. /cc @marek-safar @agocke |
Awesome, thanks! |
@mateoatr could you backport the fix to 5.0? |
I am having the same issues with FSharp.SystemTextJson as before again with the latest dotnetcore 5.0 sdk release. :-( |
I get a similar exception but with field instead of method. Here is the stack trace. Hope this helps.
|
Thanks for the report @CyberSinh - the fix should work for fields as well, the interesting bit in the fix is the type of the field, or method parameter, not where it came from. |
Thanks @vitek-karas. Hope this fix will be released soon. |
#1637 should fix at least some occurrences of this problem (hard to be 100% without a specific repro, but the problem with arrays is solved there). That fix is now in 5.0 release branch, so should go out with the next servicing release of 5.0 SDK (I think it's 5.0.1, but I could be wrong). Unfortunately the exact same error is produced by a different root cause: #1634. We're working on a fix for that. |
@vitek-karas, I have a consistent repro while trying to bump up to .NET 5, if it helps:
|
The test is currently failing due to dotnet/linker#1559 Commit migrated from dotnet/linker@2bcc466
Repro copied from dotnet/runtime#43222
Steps To Reproduce
Install .NET 5 RC1
Create some project:
Exception
Further technical details
Originally created by @SergeySeleznyov
The text was updated successfully, but these errors were encountered: