Repository navigation
Track attributes through Roslyn-generated types - #2786
Conversation
Type parameters for Roslyn-generated types are all alpha renames, so there should be a unique original type parameter to map back to, which can be used to get the original user attributes applied to the type parameter. This change tries to build a map back from types to constructor calls, which should map to the original type parameter annotations.
| // Initially fill the dictionary with all null type args. After we have fully filled out | ||
| // user methods, we'll fill in the type args |
There was a problem hiding this comment.
I think this is stale - the comment couple lines below ways basically the same thing - I would remove the one here.
| } | ||
| var method = typeInfo.CreatingMethod; | ||
| if (method.Body is { } body) { | ||
| var typeArgs = new ICustomAttributeProvider[stateMachineType.GenericParameters.Count]; |
There was a problem hiding this comment.
Nit: Move this after the ScanForInit and if - since it only needs to be allocated when we're actually going to use it.
| case Code.Initobj: | ||
| case Code.Newobj: | ||
| if (instr.Operand is MethodReference { DeclaringType: GenericInstanceType typeRef } | ||
| && stateMachineType.MetadataToken == _context.TryResolve (typeRef)?.MetadataToken) { |
There was a problem hiding this comment.
I think you can compare the TypeDefinition by reference - I think Cecil guarantees uniqueness. Note that we don't use special comparers when using TypeDefinition as a key in a Dictionary.
| return new TypeAnnotations (type, typeAnnotation, annotatedMethods.ToArray (), annotatedFields.ToArray (), typeGenericParameterAnnotations); | ||
| } | ||
|
|
||
| private IReadOnlyList<ICustomAttributeProvider>? TryGetGeneratedTypeAttributes (TypeDefinition typeDef) |
There was a problem hiding this comment.
Nit: The way I know the rules if methods starts with "TryGet", it should return a bool and probably use out parameter to return the actual value. On the other hand "Get" method can return null if it makes sense as a missing value.
There was a problem hiding this comment.
Yeah, I admit that this is a personal thing -- I prefix with Try any time there's a potential for "no result." Happy to change it if that doesn't seem helpful to you, though.
There was a problem hiding this comment.
If the "no result" is an important bit I would go with TryGet returning a boolean - but again it's a subjective thing. That said - as is it would not pass FX API review ;-)
| [SkipKeptItemsValidation] | ||
| class CompilerGeneratedStateMachine | ||
| { | ||
| public static void Main () |
There was a problem hiding this comment.
Nit: I would add couple more negative tests (which produce diagnostics) - especially in analyzers it's important since we give up on analysis if we don't understand something and won't produce any diagnostics. So the fact that a success test passed doesn't necessarily mean the analyzer worked.
Linker is the other way round, so success tests are much more "reliable".
sbomer
left a comment
There was a problem hiding this comment.
Sorry for the late review, I didn't have any feedback on my first pass and just looked at it again today.
|
|
||
| // Already warned above if multiple methods map to the same type | ||
| // Fill in null for argument providers now, the real providers will be filled in later | ||
| _ = _generatedTypeToTypeArgumentInfo.TryAdd (stateMachineType, new TypeArgumentInfo (method, null)); |
There was a problem hiding this comment.
I wonder, would it make sense to do this above by detecting calls to state machine ctors, like you do for display classes? Then you could do ScanForInit inline, and maybe change this to an assert that the state machine type from the attribute matches the one we saw constructed.
There was a problem hiding this comment.
Happy to look into this but I think it's mostly equivalent to what I have now. I'm going to check this in to make Preview 5, but we can fix it up if necessary.
|
Tested this change locally against dotnet/runtime and it didn't seem to have any problems. |
Type parameters for Roslyn-generated types are all alpha renames, so there should be a unique original type parameter to map back to, which can be used to get the original user attributes applied to the type parameter. This change tries to build a map back from types to constructor calls, which should map to the original type parameter annotations. Only handles async and iterator state machines fully right now. Full support for lambdas and local functions is left as future work. Commit migrated from dotnet/linker@2543618
Type parameters for Roslyn-generated types are all alpha renames, so
there should be a unique original type parameter to map back to, which
can be used to get the original user attributes applied to the type
parameter. This change tries to build a map back from types to
constructor calls, which should map to the original type parameter annotations.
Fixes #2785