-
Notifications
You must be signed in to change notification settings - Fork 128
Analyzer warns when Requires... Attribute is on a static constructor #2455
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
Conversation
|
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
a430e76 to
a19ffb6
Compare
| CheckMember (node); | ||
| } | ||
|
|
||
| public override void VisitConstructorDeclaration (ConstructorDeclarationSyntax node) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.
tlakollo
left a comment
There was a problem hiding this 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
|
Does another person want to sign off on these changes so they can be merged? |
| [DiagnosticAnalyzer (LanguageNames.CSharp)] | ||
| public sealed class RequiresDynamicCodeAnalyzer : RequiresAnalyzerBase | ||
| { | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: extra line
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.
…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
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
Visitoverride 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,RequiresDynamicCodeandRequiresAssemblyFileson static constructors. ForRequriesUnreferencedCodethe warnings would be reported by trimmer during publish. But forRequiresDynamicCodeandRequiresAssemblyFilesnothing 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.