Repository navigation
Don't depend on marking order in DAM type hierarchy - #2777
Conversation
vitek-karas
left a comment
There was a problem hiding this comment.
I "think" this is correct - this code still hurts my brain every time I look at it :-)
| DynamicallyAccessedMemberTypes annotation = _context.Annotations.FlowAnnotations.GetTypeAnnotation (type); | ||
| bool apply = false; | ||
|
|
||
| // We'll use the cache also as a way to detect and avoid recursion |
There was a problem hiding this comment.
This comment should move above the very first thing in the method (which is what sort of checks for recursion). It will need to be updated as it now acts on all types, not just interfaces.
There was a problem hiding this comment.
Thanks, I fixed the comment.
One thing I wanted to note is that it will still not put unannotated base types into the cache (I didn't want to introduce another cache of every marked type) - but it means that now we need to walk up the unannotated base types. Whereas before, relying on marking order meant that a type not in the cache could be assumed to have None annotations.
| } | ||
|
|
||
| void ApplyDynamicallyAccessedMembersToType (ref ReflectionMarker reflectionMarker, in MessageOrigin origin, TypeDefinition type, DynamicallyAccessedMemberTypes annotation) | ||
| void ApplyDynamicallyAccessedMembersToType (in ReflectionMarker reflectionMarker, in MessageOrigin origin, TypeDefinition type, DynamicallyAccessedMemberTypes annotation) |
There was a problem hiding this comment.
Is it correct that we should use the RequireDynamicallyAccessedMembersAction.MarkTypeForDynamicallyAccessedMembers instead of the ReflectionMarker once we share this code with the analyzer?
(Maybe you thought about this... if not, no matter, somebody will look into it once we implement sharing of this).
There was a problem hiding this comment.
This gave me some trouble which is actually what led me to make the changes in this PR. :) My understanding is that this can continue using ReflectionMarker - it's ok to always mark and produce warnings, because the warnings are already deduplicated by the type cache, no need to run with marking/warnings disabled during the analysis.
If we wanted ApplyDynamicallyAccessedMembersToTypeHierarchy to work with marking disabled the first time (which might be nice in theory, just for consistency), that can be done too, it just takes a few more changes to this logic. We'd probably just need to pass down a separate bool to indicate whether it should mark or just retrieve annotations.
There was a problem hiding this comment.
I actually didn't even think about that. I was thinking what it would take to share this code with the analyzer, which doesn't have a ReflectionMarker.
There was a problem hiding this comment.
Sorry, I misinterpreted the question. I think the analyzer will need a similar cache per type, to avoid duplicate warnings for multiple object.GetType calls on the same type - and the logic for ApplyDynamicallyAccessedMembersToType would need to be duplicated or shared.
Then instead of MarkTypeForDynamicallyAccessedMembers it would probably call ReflectionAccessAnalyzer.GetReflectionAccessDiagnostics which is already pretty similar to the code in ReflectionMarker (or maybe that code could be shared too).
Fix comment
Yes, I spent about a day trying to figure this out... Thanks for the review! |
This will allow ApplyDynamicallyAccessedMembersToTypeHierarchy to be called from the scanner when it sees object.GetType to get annotations on the type, without requiring that the type has been marked yet. (It will be marked during the final pass after the flow analysis has converged.) This will still not put unannotated base types into the cache (I didn't want to introduce another cache of every marked type) - but it means that now we need to walk up the unannotated base types. Whereas before, relying on marking order meant that a type not in the cache could be assumed to have None annotations. Commit migrated from dotnet/linker@9eb23e4
This will allow
ApplyDynamicallyAccessedMembersToTypeHierarchyto be called from the scanner when it seesobject.GetTypeto get annotations on the type, without requiring that the type has been marked yet. (It will be marked during the final pass after the flow analysis has converged.)