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

Warn on RUC annotated attribute ctors (analyzer) - #2201

Merged
mateoatr merged 10 commits into
dotnet:mainfrom
mateoatr:rucOnAttributeCtor
Sep 16, 2021
Merged

mateoatr merged 10 commits into
dotnet:mainfrom
mateoatr:rucOnAttributeCtor

Conversation

@mateoatr

Copy link
Copy Markdown
Contributor

Fix #2094.

@MichalStrehovsky

Copy link
Copy Markdown
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 { }

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

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 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

foreach (var extraSyntaxNodeAction in ExtraSyntaxNodeActions)
context.RegisterSyntaxNodeAction (extraSyntaxNodeAction.Action, extraSyntaxNodeAction.SyntaxKind);

void CheckAttributeCtor (

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.

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
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
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 subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RUC analyzer doesn't handle attribute .ctors

4 participants