Skip to content

Conversation

@jtschuster
Copy link
Member

@jtschuster jtschuster commented Dec 16, 2021

Adds warnings in the analyzer for IL2116 (RUC not allowed on static ctor) and adds IL2117 (RequiresDynamicCode not allowed on static ctor) and IL2118 (RequiresAssemblyFiles not allowed on static ctor).

Fixes ExpectedWarning not having effects on constructors by adding a Visit override in the compilation tree walker in the test infra.

Adds case in GetDisplayName to differentiate .cctor's and .ctor's.

Fixes #2446
Fixes #2347

Customer Impact

Analyzer correctly reports warnings due to RequiresUnreferencedCode, RequiresDynamicCode and RequiresAssemblyFiles on static constructors. For RequriesUnreferencedCode the warnings would be reported by trimmer during publish. But for RequiresDynamicCode and RequiresAssemblyFiles nothing would report these warnings.

Testing

Added targeted tests.

Risk

Low - added a check for new case (static .ctor) and the new diagnostics in the analyzer.

@tlakollo
Copy link
Contributor

There are some ranges for warnings described in DiagnosticDescriptors this along with currently registered warnings in DiagnosticIds means that the warning produced by RequiresDynamicCode should be IL3052 and by RequiresAssemblyFiles should be IL3004

sb.Insert (0, '<');
}

public static string GetDisplayName (this IMethodSymbol method)
Copy link
Contributor

Choose a reason for hiding this comment

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

The GetDisplayName method was made to match C# naming which is what Roslyn uses and most people are used to reading instead of IL namings. Therefore if there is a discrepancy between the way it's represented by the analyzer and linker is likely a fix in the GetDisplayName in the linker not an implementation on the analyzer

Copy link
Contributor

Choose a reason for hiding this comment

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

Worst case I think you can append directly the string ".cctor" at the end when displaying the diagnostic

Copy link
Member

Choose a reason for hiding this comment

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

I would expect most cases to already be covered by Roslyn. I could understand that we need to do something special for property getter/setter (and events), and possibly even .cctor, but other than that I would expect to simply call Roslyn to do this for us.

Copy link
Member Author

@jtschuster jtschuster Dec 16, 2021

Choose a reason for hiding this comment

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

For now, would it be best to use the ISymbolExtensions.GetDisplayName and have separate ExpectedWarning's for the analyzer and trimmer, and then later change the trimmer's GetDisplayName to match the Analyzer/Roslyn?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a check in the GetDisplayName in the linker that exists for ctors, but was missing cctors. Now they should look the same.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think I was wrong is not a fix on the linker side, because if you fix in the linker then constructor and static constructor are represented equally, there is no way to differentiate them. So in the cases like the BeforeInitField test, I think things can get confusing. Same for RequiresInCompilerGeneratedCode tests, they get more confusing although you don't see the changes because we don't check for the caller name only messages in the attribute.
But yeah I think the best place to fix it is ISymbolExtensions.GetDisplayName an if statement before printing the method name check if the method symbol is a static constructor

Copy link
Member

Choose a reason for hiding this comment

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

.cctors are weird - so we might need to add a special case unfortunately.

Copy link
Member Author

Choose a reason for hiding this comment

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

I recently pushed a couple commits with different formats for the diagnostic messages. The first one includes the static modifier (e.g. static Mono.Linker.Tests.Cases.RequiresCapability.RequiresCapability.StaticCtor.StaticCtor()). The second uses the ..cctor() notation (e.g. Mono.Linker.Tests.Cases.RequiresCapability.RequiresCapability.StaticCtor..cctor()).

I think the static modifier notation would be best since it still differentiates the constructors and is more familiar to most people.

Copy link
Contributor

@tlakollo tlakollo Dec 20, 2021

Choose a reason for hiding this comment

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

I just created something in SharpLab to see how a warning would be printed

public class Foo
{
    static Foo (int a) {}
}

This generates error CS0132: 'Foo.Foo(int)': a static constructor must be parameterless also inserting the [Obsolete] attribute will generate a message [deprecated] Foo.Foo() so the compiler warnings at this moment don't show the modifiers nor have a clear distinction between static and instance constructors, so whatever decision we make is going to deviate from whatever the compiler normally prints.

I think is key for the trimmer to have some differentiation but I don't have a preference for which one to choose

  • The first option I have the concern that not a lot of people will even see the static modifier, it's at the beginning of the string and no other member will print it. Although I agree, I like more the readability of it.
  • The second option is a weird notation, but static constructors are weird, we don't even allow to annotate them. Static constructors are handled in a special way. So the fact that the name is weird might even be beneficial. The problem here would be that we already don't name instance constructors as .ctor()

In general I think I feel more inclined for the first approach

CheckMember (node);
}

public override void VisitConstructorDeclaration (ConstructorDeclarationSyntax node)
Copy link
Member Author

@jtschuster jtschuster Jan 4, 2022

Choose a reason for hiding this comment

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

Fix for #2446 to make sure we account for ExpectedWarning on constructors.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a test for this? Something that Inside the static constructor calls a method annotated with RUC and just verify that we can use the ExpectedWarning attribute

Copy link
Member Author

Choose a reason for hiding this comment

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

Good thing you mentioned this, I think I found a bug that the linker doesn't check in constructors for calls to methods with RUC. The test class for this is WarningsInCtor in RequiresCapability.cs. It calls a method annotated with RUC inside the constructors, but the linker doesn't seem to be producing a warning. I filed #2484 to track the issue.

Copy link
Contributor

@tlakollo tlakollo Jan 6, 2022

Choose a reason for hiding this comment

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

I think it's because nothing references that code in the main method, since it's not referenced Trimmer will get rid of that piece of code. Notice that one of the differences between analyzer and trimmer is that analyzer will produce diagnostics even for code that will be trimmed.

@vitek-karas vitek-karas changed the title Analyzer warns when Requires... Attribute is on a static constructor [release/6.0.2xx] Analyzer warns when Requires... Attribute is on a static constructor Jan 6, 2022
Linker doesn't seem to be checking ctors for calls to methods with RUC.
This commit removes the check for the expected warnings in the linker
until the bug is fixed.
Copy link
Contributor

@tlakollo tlakollo left a comment

Choose a reason for hiding this comment

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

Other than lint, looks good to me. Thanks :D

@tlakollo
Copy link
Contributor

Does another person want to sign off on these changes so they can be merged?

@jtschuster jtschuster changed the base branch from release/6.0.2xx to main January 20, 2022 00:13
[DiagnosticAnalyzer (LanguageNames.CSharp)]
public sealed class RequiresDynamicCodeAnalyzer : RequiresAnalyzerBase
{

Copy link
Contributor

Choose a reason for hiding this comment

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

nit: extra line

@jtschuster jtschuster changed the title [release/6.0.2xx] Analyzer warns when Requires... Attribute is on a static constructor Analyzer warns when Requires... Attribute is on a static constructor Jan 21, 2022
@jtschuster jtschuster merged commit 2999a6a into dotnet:main Jan 24, 2022
jtschuster added a commit to jtschuster/linker that referenced this pull request Jan 24, 2022
Design proposal for DAM-on-type and RUC interactions (dotnet#2168)

Co-authored-by: Sven Boemer <[email protected]>

Fix analyzer nullref on assembly attribute (dotnet#2534)

The analyzer runs for property assignment operations, including those in
attributes. To check whether the property assignment should warn, we
look for RUC on the containing symbol. However, assembly-level attributes
are contained in the global namespace, which has a null containing type.

Create a schema for the LinkAttribute XML files (dotnet#2500)

Adds xml schema for ILLink.LinkAttributes.xml, and adds relative paths to the schema in xml used in unit tests to enable linting.

[main] Update dependencies from dotnet/runtime (dotnet#2539)

[main] Update dependencies from dotnet/runtime

Title and message resources should be enforced to exist to prevent printing empty messages (dotnet#2538)

Currently, missing resources will default to an empty string printing an empty message for the diagnostics. Code should not try to gracefully handle errors.

Included in this PR:
- DiagnosticString should be able to locate a title and message resource strings otherwise it throws
- Add title resource strings for all missing diagnostic Ids

Update dependencies from https://github.com/dotnet/runtime build 20220123.5 (dotnet#2542)

[main] Update dependencies from dotnet/runtime

Update dependencies from https://github.com/dotnet/arcade build 20220121.6 (dotnet#2541)

[main] Update dependencies from dotnet/arcade

Analyzer warns when Requires... Attribute is on a static constructor (dotnet#2455)

Adds warnings in the analyzer for IL2116 (RUC not allowed on static ctor) and adds IL2117 (RequiresDynamicCode not allowed on static ctor) and IL2118 (RequiresAssemblyFiles not allowed on static ctor).

Fixes ExpectedWarning not having effects on constructors by adding a Visit override in the compilation tree walker in the test infra.

Adds case in GetDisplayName to differentiate .cctor's and .ctor's.

Linker doesn't seem to be checking ctors for calls to methods with RUC.
This commit removes the check for the expected warnings in the linker
until the bug is fixed.
@jtschuster jtschuster deleted the RequiresOnCctorWarns branch February 17, 2022 17:25
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…otnet/linker#2455)

Adds warnings in the analyzer for IL2116 (RUC not allowed on static ctor) and adds IL2117 (RequiresDynamicCode not allowed on static ctor) and IL2118 (RequiresAssemblyFiles not allowed on static ctor).

Fixes ExpectedWarning not having effects on constructors by adding a Visit override in the compilation tree walker in the test infra.

Adds case in GetDisplayName to differentiate .cctor's and .ctor's.

Linker doesn't seem to be checking ctors for calls to methods with RUC.
This commit removes the check for the expected warnings in the linker
until the bug is fixed.

Commit migrated from dotnet/linker@2999a6a
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.

ExpectedWarning doesn't check for warnings on constructors Analyzer allows RUC/RAF in static constructor

3 participants