Skip to content

Add RequiresAssemblyFiles CodeFix#2015

Merged
tlakollo merged 9 commits intodotnet:mainfrom
tlakollo:RAFCodeFix
May 12, 2021
Merged

Add RequiresAssemblyFiles CodeFix#2015
tlakollo merged 9 commits intodotnet:mainfrom
tlakollo:RAFCodeFix

Conversation

@tlakollo
Copy link
Contributor

@tlakollo tlakollo commented May 5, 2021

Add RequiresAssemblyFiles CodeFix
Add tests
Fixes #2005
Fixes #1986
Add other fixes that exist in RequiresUnreferencedCode but weren't on RequiresAssemblyFiles

Add tests
Fix dotnet#2005
Add other fixes that exist in RequiresUnreferencedCode but werent on
RequiresAssemblyFiles
@tlakollo tlakollo requested review from agocke and mateoatr May 5, 2021 22:37
@tlakollo tlakollo self-assigned this May 5, 2021
@tlakollo tlakollo requested a review from marek-safar as a code owner May 5, 2021 22:37
ImmutableArray<ISymbol> dangerousPatterns)
{
// Find containing symbol
ISymbol? containingSymbol = null;
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this FindAttributableParent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think they are the same FindAttributableParent search for a Method/Property/Event/Field to put the attribute on, this function only looks for local functions and lambdas to warn and otherwise falls back to operationContext.ContainingSymbol

Copy link
Member

Choose a reason for hiding this comment

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

Lambdas can't have attributes so I'm not clear on what this is supposed to be doing.

var fieldAccess = (IFieldReferenceOperation) operationContext.Operation;
if (fieldAccess.Field.ContainingType is INamedTypeSymbol { StaticConstructors: var ctors } &&
!SymbolEqualityComparer.Default.Equals (operationContext.ContainingSymbol.ContainingType, fieldAccess.Field.ContainingType)) {
CheckStaticConstructors (operationContext, ctors);
Copy link
Member

Choose a reason for hiding this comment

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

Why are we only checking for static constructors on field access?

ImmutableArray<ISymbol> dangerousPatterns)
{
// Find containing symbol
ISymbol? containingSymbol = null;
Copy link
Member

Choose a reason for hiding this comment

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

Lambdas can't have attributes so I'm not clear on what this is supposed to be doing.

tlakollo added 3 commits May 6, 2021 19:23
Add resources files
PR comments
Use context.Options instead of context
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../Resources.resx">
<body>
<trans-unit id="RequiresAssemblyFilesCodeFixTittle">
Copy link
Member

Choose a reason for hiding this comment

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

We don't have localization so you can probably turn this off (I think there's a property in the analyzer project that's set that does this, something like <EnableXlfLocalization>false

Copy link
Member

Choose a reason for hiding this comment

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

These files should be deleted.

if (assemblyType != null) {
// properties
ImmutableArrayOperations.AddIfNotNull (dangerousPatternsBuilder, ImmutableArrayOperations.TryGetSingleSymbol<IPropertySymbol> (assemblyType.GetMembers ("Location")));
protected override ImmutableArray<ISymbol> GetDangerousPatterns (Compilation compilation)
Copy link
Member

Choose a reason for hiding this comment

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

What does Pattern mean here? Members? Do we want to use the word Dangerous here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could rename it to SpecialIncompatibleMembers


protected abstract bool TryGetRequiresAttribute (ISymbol member, out AttributeData? requiresAttribute);

protected abstract bool ReportDangerousPatternDiagnostic (OperationAnalysisContext operationContext, ImmutableArray<ISymbol> dangerousPatterns, ISymbol member);
Copy link
Member

Choose a reason for hiding this comment

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

Some of these probably deserve doc comments. It's not clear to me what each method is supposed to do.

<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.2" xsi:schemaLocation="urn:oasis:names:tc:xliff:document:1.2 xliff-core-1.2-transitional.xsd">
<file datatype="xml" source-language="en" target-language="cs" original="../Resources.resx">
<body>
<trans-unit id="RequiresAssemblyFilesCodeFixTittle">
Copy link
Member

Choose a reason for hiding this comment

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

These files should be deleted.

public partial class CSharpCodeFixVerifier<TAnalyzer, TCodeFix>
where TAnalyzer : DiagnosticAnalyzer, new()
where TCodeFix : CodeFixProvider, new()
where TCodeFix : Microsoft.CodeAnalysis.CodeFixes.CodeFixProvider, new()
Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By inserting a resource file in ILLink.CodeFixProvider I needed to add it as part of the using statements, creating ambiguity on what is a CodeFixProvider

@tlakollo
Copy link
Contributor Author

If we remove the static constructor handling some of the tests in RequiresCapability will fail, but I just noticed that Marek disabled those test for being unreliable

@agocke
Copy link
Member

agocke commented May 12, 2021

Ah, yes, we should re-enable those. What test fails though? Does the linker already check this functionality?

@tlakollo
Copy link
Contributor Author

tlakollo commented May 12, 2021

I'm not sure which test was failing on CI, but if I remove the static constructor code it will fail on 3 of the RequiresCapability tests
TestStaticCctorRequiresUnreferencedCode
TestStaticCtorMarkingIsTriggeredByFieldAccess
TestStaticCtorTriggeredByMethodCall
Those were added around the same time the support was added in the analyzer, @mateoatr made the change to support these scenarios in the linker

@agocke
Copy link
Member

agocke commented May 12, 2021

Awesome -- didn't realize this had already been implemented in the linker. Yeah, then we should keep this and add test coverage in the analyzer tests.

I forgot the linker tests were disabled though, I'll look at that.

Add document comments
Re-enable RequiresCapability tests
Copy link
Member

@agocke agocke left a comment

Choose a reason for hiding this comment

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

LGTM!

Nice work, looks like a lot of cleaner sharing between the analyzers now

@tlakollo tlakollo merged commit 2add126 into dotnet:main May 12, 2021
@tlakollo tlakollo deleted the RAFCodeFix branch May 12, 2021 23:13
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
Add RequiresAssemblyFiles CodeFix
Add tests
Fix dotnet/linker#2005
Refactor analyzer code. Fixes dotnet/linker#1986
Add resources files to codefixer

Commit migrated from dotnet/linker@2add126
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding RequiresAssemblyFiles in an Event does not suppress the warning Refactor Requires Analyzer Code

3 participants