Skip to content

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

Merged
merged 5 commits into from
Sep 8, 2021

Conversation

mateoatr
Copy link
Contributor

@mateoatr mateoatr commented Sep 3, 2021

Fixes #2165.

Comment on lines 47 to 50
if (operationContext.Operation is IObjectCreationOperation objectCreationOperation &&
objectCreationOperation.Type is INamedTypeSymbol objectType && objectType.IsGenericType) {
typeParams = objectType.TypeParameters;
typeArgs = objectType.TypeArguments;
Copy link
Member

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

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 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).

Comment on lines 36 to 39
[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.")]
Copy link
Member

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

@mateoatr mateoatr merged commit 4f28a65 into dotnet:main Sep 8, 2021
mateoatr added a commit to mateoatr/linker that referenced this pull request Sep 8, 2021
…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
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RUC analyzer doesn't produce a warning for type substitution with new() annotation
2 participants