-
Notifications
You must be signed in to change notification settings - Fork 128
Warn on RUC annotated ctors invoked through new()
constraint in analyzer
#2254
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
test/Mono.Linker.Tests.Cases/RequiresCapability/RequiresUnreferencedCodeCapability.cs
Outdated
Show resolved
Hide resolved
if (operationContext.Operation is IObjectCreationOperation objectCreationOperation && | ||
objectCreationOperation.Type is INamedTypeSymbol objectType && objectType.IsGenericType) { | ||
typeParams = objectType.TypeParameters; | ||
typeArgs = objectType.TypeArguments; |
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.
This only reacts to calling a constructor on a generic type.
Code like this should still warn though:
static class MyTest<T> where T : new ()
{
static void DoNothing() {}
}
void Test()
{
// IL2026
MyTest.DoNothing<ClassWithRUCOnCtor>();
}
I must admit that I don't know enough about analyzers to tell what type of solution would be the best here. Linker will warn anywhere the generic is instantiated. Even cases like typeof(MyType<ClassWithRUCOnCtor>)
will generate the warning.
This is obviously not that common, so it's OK to solve this later...
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 moved the analysis context used here to a SyntaxNodeAnalysisContext
; this way we look for syntax nodes that represent generic names and check, for their type parameters, if these have any constructor constraints. I think this would cover up all cases. I've added the scenario you outline above (and the analyzer warns now).
[SuppressMessage ("MicrosoftCodeAnalysisPerformance", "RS1008", | ||
Justification = "This action is registered through a compilation start action, so that the instances which " + | ||
"can register this operation action will not outlive a compilation's lifetime, avoiding the possibility of " + | ||
"this data causing stale compilations to remain in memory.")] |
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 please explain why this action causes the warning and the one above doesn't? I don't see how they're really different...
…lyzer (dotnet#2254) * Add support in RUC analyzer for new() constraint on generics * Support `new()` constraint on types * Use SyntaxNodeAnalysisContext for constructor constraints * Lint * Update justification
…lyzer (dotnet/linker#2254) * Add support in RUC analyzer for new() constraint on generics * Support `new()` constraint on types * Use SyntaxNodeAnalysisContext for constructor constraints * Lint * Update justification Commit migrated from dotnet/linker@4f28a65
Fixes #2165.