Skip to content

Warn on RUC annotated attribute ctors (analyzer) #2201

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

Merged
merged 10 commits into from
Sep 16, 2021

Conversation

mateoatr
Copy link
Contributor

Fix #2094.

@MichalStrehovsky
Copy link
Member

Might be worth fixing the RUC-annotated-property case as well while you're at it:

class AAttribute : Attribute
{
    public Type SomeProperty
    {
        [RUC]
        set { ... }
    }
}

[A(SomeProperty = typeof(Foo))]
class Foo { }

}, SymbolKind.Method);

context.RegisterSymbolAction (symbolAnalysisContext => {
var typeSymbol = (INamedTypeSymbol) symbolAnalysisContext.Symbol;
CheckMatchingAttributesInInterfaces (symbolAnalysisContext, typeSymbol);
CheckAttributeCtor (symbolAnalysisContext, typeSymbol);
Copy link
Member

Choose a reason for hiding this comment

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

If I read this correctly it will only work if the attribute is on a method or type(which doesn't seem to be covered by the test BTW). What if I put such attribute on anything else (field, property, method parameter, ...) - ideally we should generate the warning in all cases where the attribute is instantiated (liker will).

@mateoatr mateoatr force-pushed the rucOnAttributeCtor branch from dd42258 to 8ff9cc8 Compare August 17, 2021 17:42
@mateoatr
Copy link
Contributor Author

This takes some of the changes from the test restructure PR (#2113). In particular, it grabs the ProducedBy enum and updates ExpectedWarningAttribute to use it instead of GlobalAnalysisOnly.
I also added support for SetupCompileBefore in the analyzer tests: we check for this attribute and, if found, we grab the sourcefile pointed by the attribute and use its syntax tree for the test compilation in the analyzer infra.
Previously, the analyzer tests only validated the ExpectedWarning, LogContains and LogDoesNotContain annotations when placed on method declarations; this PR also changes this so that it will now validate these attributes when put on any member kind.

Mateo Torres Ruiz added 3 commits September 13, 2021 11:07
Add ProducedBy from Test Restructure
Add support for SetupCompileBefore in the analyzer
Run analyzer tests on all members (not only methods)
Move tests to RequiresCapability
}
}

CheckAttributeCtor (symbolAnalysisContext, propertySymbol);
Copy link
Member

Choose a reason for hiding this comment

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

Don't we need to do this for getter/setter as well (similar to what we do for events with add/remove).

Copy link
Member

Choose a reason for hiding this comment

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

In fact, RUC is not allowed on the property itself, only on the methods...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is needed for RequiresAssemblyFilesAttribute, which can be put on properties.

@@ -125,6 +149,21 @@ public override void Initialize (AnalysisContext context)
foreach (var extraSyntaxNodeAction in ExtraSyntaxNodeActions)
context.RegisterSyntaxNodeAction (extraSyntaxNodeAction.Action, extraSyntaxNodeAction.SyntaxKind);

void CheckAttributeCtor (
Copy link
Member

Choose a reason for hiding this comment

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

In a separate change:
This should be called CheckAttributeInstantiation and it should also handle the case where the instantiation sets a property on the attribute - as in that case the property setter is called and it can have RUC on it.

@agocke agocke self-assigned this Sep 14, 2021
Mateo Torres Ruiz and others added 4 commits September 14, 2021 15:30
Check for instantiations that set annotated properties
Co-authored-by: Andy Gocke <angocke@microsoft.com>
Rename Linker => Trimmer in ProducedBy
@mateoatr
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

No pipelines are associated with this pull request.

@mateoatr mateoatr closed this Sep 16, 2021
@mateoatr mateoatr reopened this Sep 16, 2021
@mateoatr
Copy link
Contributor Author

/azp run

@azure-pipelines
Copy link

Pull request contains merge conflicts.

agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
* Warn on usage of attributes with annotated ctors

* PR feedback
Add ProducedBy from Test Restructure
Add support for SetupCompileBefore in the analyzer
Run analyzer tests on all members (not only methods)
Move tests to RequiresCapability

* Enable analyzer tests for attributes which use RUC annotated properties

* Lint

* Rename CheckAttributeCtor
Check for instantiations that set annotated properties

* Apply suggestions from code review

Co-authored-by: Andy Gocke <angocke@microsoft.com>

* Update comment
Rename Linker => Trimmer in ProducedBy

* Fix applied suggestions

* Lint

Co-authored-by: Andy Gocke <angocke@microsoft.com>

Commit migrated from dotnet/linker@6f237bb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RUC analyzer doesn't handle attribute .ctors
4 participants