Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Track attributes through Roslyn-generated types - #2786

Merged
agocke merged 9 commits into
dotnet:mainfrom
agocke:track-attributes-through-generated-types
May 24, 2022
Merged

agocke merged 9 commits into
dotnet:mainfrom
agocke:track-attributes-through-generated-types

Conversation

@agocke

@agocke agocke commented May 9, 2022

Copy link
Copy Markdown
Member

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

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.
@agocke
agocke requested review from sbomer and vitek-karas May 9, 2022 06:00
@agocke
agocke requested a review from marek-safar as a code owner May 9, 2022 06:00
@agocke
agocke marked this pull request as draft May 9, 2022 06:01
Comment on lines +124 to +125
// Initially fill the dictionary with all null type args. After we have fully filled out
// user methods, we'll fill in the type args

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 ()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 sbomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@agocke
agocke marked this pull request as ready for review May 24, 2022 04:21
@agocke

agocke commented May 24, 2022

Copy link
Copy Markdown
Member Author

Tested this change locally against dotnet/runtime and it didn't seem to have any problems.

@agocke
agocke merged commit 2543618 into dotnet:main May 24, 2022
@agocke
agocke deleted the track-attributes-through-generated-types branch May 24, 2022 05:25
agocke added a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Attributes on async/iterator type parameters are lost by linker

3 participants